I'm making a Automator to jump from citation in Word to the reference software(Zotero). But I can't find a AppleScript to extract text of selected field code (the first step).
The field code in Word is
ADDIN ZOTERO_ITEM CSL_CITATION {"citationID":"AFUiwuqi","properties":{"formattedCitation":"[1]","plainCitation":"[1]","noteIndex":0},"citationItems":[{"id":9752,"uris":["http://zotero.org/users/6410528/items/YYTRWPHH"],"itemData":{"id":9752,"type":"article-journal","container-title":"Nature","DOI":"10.1038/s41586-019-1737-7","ISSN":"0028-0836, 1476-4687","issue":"7782","page":"324-329","title":"Controlled flight of a microrobot powered by soft artificial muscles","volume":"575","author":[{"family":"Chen","given":"Yufeng"},{"family":"Zhao","given":"Huichan"},{"family":"Mao","given":"Jie"},{"family":"Chirarattananon","given":"Pakpong"},{"family":"Helbling","given":"E. Farrell"},{"family":"Hyun","given":"Nak-seung Patrick"},{"family":"Clarke","given":"David R."},{"family":"Wood","given":"Robert J."}],"issued":{"date-parts":[["2019",11,14]]}}}],"schema":"https://github.com/citation-style-language/schema/raw/master/csl-citation.json"}
Here is the script process:
Extract text from selected field code in Word (This is the question)
Get the uris text(http://zotero.org/users/6410528/items/YYTRWPHH)
Get the item-codes (YYTRWPHH).
Open url (zotero://select/library/items?itemKey=YYTRWPHH)
Now I use VBA to extract field code text, see below. But in this way, the file will be changed. So I want to do this via AppleScript.
Sub GetFiledsCodes()
Dim myRange As Range, myCodes As String
Set myRange = Selection.Range
With myRange
If .Fields.Count = 0 Then
MsgBox "No Code!", vbInformation
Exit Sub
Else
.Fields.Update
.TextRetrievalMode.IncludeFieldCodes = True
.TextRetrievalMode.IncludeHiddenText = True
myCodes = .Text
myCodes = VBA.Replace(myCodes, Chr(19), "{")
myCodes = VBA.Replace(myCodes, Chr(21), "}")
.SetRange .End, .End
.InsertAfter myCodes
.Font.Name = "Times New Roman"
.Font.Size = 12
.Cut
End If
End With
End Sub
PS:
Here is my process in Automator(it can work but using VBA):
Run AppleScript
on run {input, parameters}
tell application "Microsoft Word" to activate
tell application "Microsoft Word"
run VB macro macro name "GetFiledsCodes"
delay 0.5
end tell
return input
end run
Get contents from clipboard
Extract URLs from Text
Filter Paragraphs begin with http://zotero.org/users/
Copy to Clipboard
Run AppleScript
set myStr to do shell script "pbpaste"
tell application "Zotero" to activate
set AppleScript's text item delimiters to "
"
set myList to every text item of myStr
set zoterocode to ""
set codes to ""
repeat with j from 1 to the length of myList
set itemValue to item j of myList
set zoterocode to (do shell script "sed -E 's#http://zotero.org/users/[0-9]+/items/##g' <<< " & itemValue)
if j = 1 then
set codes to zoterocode
else
set codes to codes & "," & zoterocode
end if
end repeat
tell application "System Events"
key code 18 using {command down, control down, option down}
delay 0.5
set collectionKey to do shell script "pbpaste"
if collectionKey = myStr then
set theurl to "zotero://select/library/items?itemKey=" & codes
else
set theurl to collectionKey & "/items?itemKey=" & codes
end if
open location theurl
end tell
That helps a lot. Okay, so this isn't a turnkey solution for your question but I don't think you really need that as you'd probably end up having to tell me more about how this app works than is really necessary. So this script focuses on your initial question about getting the field codes/result ranges from a merge document.
I put together a simple mail merge consisting of labels and a data file with 8 records, each of which have 5 fields: {"«LastName»", "«JobTitle»", "«Company»", "«City»", "«Web»"}. The latter is the key field.
Basically, the script runs through the data merge document and cycles first through its fields, then the web field, and finally the web addresses.
Based on your script, I can't really determine what you are doing with each address so it finishes by collecting just the final part of each address in a list. The obscure parts for me are the pbpastes, the codes and the whole System Events block. This area would need tweaking.
Incidentally, it's quite likely that you can avoid some of the shell scripts but I can't say how yet. Obviously the script has some redundancies and could be further refined but I think it demonstrates how to extract the information you need. Take a look at it and let me know what issues there are that need addressing.
tell application "Microsoft Word"
set d1 to document "cardo_labels.docx"
set fContents to {} -- list of mergefield
set fResRange to {} -- list of result range, i.e. field merge data
repeat with x from 1 to (count of fields of d1)
set fcs to content of field code of field x of d1 --> " MERGEFIELD LastName "
set frr to content of result range of field x of d1 --> "Smith"
if fcs is not " NEXT " then -- ignore «Next Record»
set end of fContents to fcs
set end of fResRange to frr
end if
end repeat
--> single record example
fContents --> {" MERGEFIELD LastName ", " MERGEFIELD JobTitle ", " MERGEFIELD Company ", " MERGEFIELD City ", " MERGEFIELD Web "}
fResRange --> {"Smith", "President", "Acme Screw & Gear", "Metz", "http://zotero.org/users/1234/items/smith-metz"}
-- NB when not displaying 'merged data', fResRange will appear thusly: {"«LastName»", "«JobTitle»", "«Company»", "«City»", "«Web»"}
set webList to {}
repeat with y from 1 to (count of fResRange)
if item y of fResRange begins with "http://zotero.org/users/" then
set end of webList to (item y of fResRange)
end if
end repeat
--> {"http://zotero.org/users/1234/items/smith-metz"}
--> {"http://zotero.org/users/1234/items/smith-metz", "http://zotero.org/users/4222/items/branson-metz", "http://zotero.org/users/3236/items/house-metz", "http://zotero.org/users/3342/items/kurtz-london", "http://zotero.org/users/12345/items/jones-london"}
set urlPiece to {}
set AppleScript's text item delimiters to "/"
repeat with z in webList
set end of urlPiece to last text item of z
end repeat
-- contents of z
--> "http://zotero.org/users/1234/items/smith-metz"
set AppleScript's text item delimiters to ""
urlPiece
--> {"smith-metz"}
--> {"smith-metz", "jones-saopaolo", "branson-metz", "house-metz", "kurtz-london", "jones-london"}
end tell
Thanks to ideas from #Mockman.
Combining with the selection, here is the way to extract text from selected field code via AppleScript:
tell application "Microsoft Word"
tell selection
set fcs to content of field code of field of text object
end tell
end tell
fcs
In the image below, I am trying to edit every file name in the cat directory so it only contains one "." For example, I need the first file name to be cat1.jpg and so forth for all of the other file names.
Do any of you all mind providing me some guidance on how I can implement an AppleScript (with Automator?) to rename all of the files in this cat directory?
Let's assume that all the files you want to rename are in a folder on your desktop named "Cat"
This following AppleScript code will remove the first instance of "." ... Leaving an empty space in its place. So, "cat.1.jpg" will be renamed to "cat 1.jpg" etc.
property theFolder : ((path to desktop) as text) & "Cat"
property originalFileName : missing value
property originalFileNameExtension : missing value
tell application "Finder" to set theFiles to (files of folder theFolder) as alias list
repeat with i from 1 to count of theFiles
set thisItem to item i of theFiles
tell application "System Events"
set fileInfo to thisItem's properties
set {originalFileName, originalFileNameExtension} to ¬
{name of fileInfo, name extension of fileInfo}
end tell
set theOffset to offset of originalFileNameExtension in originalFileName
set shortName to text 1 thru (theOffset - 2) of originalFileName
set text item delimiters to "."
set tempText to every text item of shortName
set text item delimiters to " "
set cleanedName to (tempText as text) & "." & originalFileNameExtension
tell application "System Events" to set name of thisItem to cleanedName
end repeat
This would be a full AppleScript solution to your question. But if this is only a one time event for you, mass renaming of the files through Finder, would be a quicker solution.
Answering the question in the title, once you cycle through strings in a repeat loop, to remove the first instance of a string from a string you can:
Use offset:
set s to "foo.bar.txt"
set o to offset of "." in s
set r to (text 1 thru (o - 1) of s) & (text (o + 1) thru -1 of s)
Use Applescript's Text Item Delimiters:
set AppleScript's text item delimiters to "."
set l to text items of s
set r to (item 1 of l) & (text items 2 thru -1 of l as string)
set AppleScript's text item delimiters to ""
I'm trying to make a folder that will automatically create folders then sort the files into them based on the first number. The files I have to sort all come in a similar format with a name like,Feb 4 2.3 U#03 (3).mrd. My intention was to write some AppleScript to create a folder based on the number (2.3) then put all the files with (2.3) into that folder, and do the same with all the other files.
I made a bit that sorts the files based on their number that seems to work,
set text item delimiters to {" "}
tell application "Finder"
set aList to every file in folder "Re-namer"
repeat with i from 1 to number of items in aList
set aFile to (item i of aList)
try
set fileName to name of aFile
set firstPart to text item 1 of fileName
set secondPart to text item 2 of fileName
set thirdPart to text item 3 of fileName
set fourthPart to text item 4 of fileName
set fifthPart to text item 5 of fileName
set newName to thirdPart & " " & secondPart & " " & firstPart & " " & fourthPart & " " & fifthPart
set name of aFile to newName
end try
end repeat
end tell
now I just need to create the new folders based on the first number and put the matching files in. I tried to make a script for this too (keep in mind I've never coded before and have no idea what I'm doing) and unsurprisingly it didn't work :(
tell application "Finder"
open folder "Re-namer"
set loc to folder "Re-namer"
set aList to every file in loc
repeat with i from 1 to number of items in aList
set aFile to (item i of aList)
if not (exists folder named "text item 1" in loc) then
make new folder in loc with properties {name:"text item 1"}
else
move aFile in folder "text item 1"
end if
end repeat
end tell
I've found a few similar questions but I still can't get it to work. If anyone has any ideas or resoures to help with this question I would greatly appreaate it.
Your are very close to correct script. However, instead of doing 2 loops, one for changing names and one to move files, it is quicker to do only 1 loop doing both at same time.
Also, the key thing is to understand that, in the loop, if you change the name, the Finder loose the reference to the file changed, then it fails when you try to move it after changing name. but since you know the folder and the new name, you can still reference to that "new" file.
set MyRenamer to choose folder "Select the folder"
set TPath to MyRenamer as string
set text item delimiters to {" "}
tell application "Finder"
set aList to every file in MyRenamer
repeat with F in aList
set fileName to name of F
set firstPart to text item 1 of fileName -- like "Feb"
set secondPart to text item 2 of fileName -- like "4"
set thirdPart to text item 3 of fileName -- like "2.3"
set fourthPart to text item 4 of fileName -- like "U#03"
set fifthPart to text item 5 of fileName -- like "(3).mrd"
set newName to thirdPart & " " & secondPart & " " & firstPart & " " & fourthPart & " " & fifthPart
set name of F to newName
if (not (exists folder thirdPart in MyRenamer)) then
make new folder in MyRenamer with properties {name:thirdPart}
end if
move (TPath & newName) as alias to folder (TPath & thirdPart & ":") -- rebuild the new file path as alias and move it.
end repeat
end tell
Tested and OK.
I would like to create an applescript that will create multiple folders with the same root name but the numbers change? or at least a repeating folder creation script until the person has enough folders. So something that makes folders like this: JOYR-15-0035-00, JOYR-15-0036-00, JOYR-15-0037-00 and so on. Is that at all possible? I am just learning this. I am normally a graphic designer but I feel like I can get a lot from applescript.
Currently I just have this basic script:
tell application "Finder"
set KDID to text returned of (display dialog "Enter the KDID ID:" default answer "JOYR-")
set loc to choose folder "Choose Parent Folder Location"
set newfoldername to {name:KDID}
set newfo to make new folder at loc with properties {name:KDID}
reveal newfo
end tell
Try this, it assumes that the KDID is just the number 15 in the example, the syntax is always JOYR-<KDID>-<consecutive number>-00 and the leading JOYR as well as the trailing double zero don't change.
The script asks for the parent folder, the KDID and the number of sequential folders. Then it checks the parent folder for the greatest existing number (the 0035 part) and creates folders starting with the greatest number plus 1 or – if no existing folders are found – with 1. The number has always four digits.
property letterPrefix : "JOYR"
property KDID : "15"
property parentFolder : missing value
set parentFolder to choose folder "Choose Parent Folder Location"
tell application "Finder"
activate
set KDID to text returned of (display dialog "Enter the KDID ID:" default answer "15")
repeat
set howManyFolders to text returned of (display dialog "Enter the Number of Folders to create:" default answer "1")
try
set howManyFolders to howManyFolders as integer
if howManyFolders < 1 then error
exit repeat
on error
display dialog "Please enter an integer value greater than 0" default answer "1"
end try
end repeat
set currentNumber to my getGreatestFolderNumber()
repeat howManyFolders times
set folderName to letterPrefix & "-" & KDID & "-" & my pad(currentNumber) & "-00"
make new folder at parentFolder with properties {name:folderName}
set currentNumber to currentNumber + 1
end repeat
open parentFolder
end tell
on getGreatestFolderNumber()
tell application "Finder"
set {ASTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "-"}
try
set folderNames to name of folders of parentFolder whose name starts with (letterPrefix & "-" & KDID & "-")
set maxNumber to 0
repeat with aName in folderNames
set curNumber to (text item 3 of aName) as integer
if curNumber > maxNumber then set maxNumber to curNumber
end repeat
set AppleScript's text item delimiters to ASTID
return maxNumber + 1
on error
set AppleScript's text item delimiters to ASTID
return 1
end try
end tell
end getGreatestFolderNumber
on pad(v)
return text -4 thru -1 of ("000" & v)
end pad
I'm trying to make an AppleScript droplet to rename a bunch of images annoyingly formatted, but I found out my AppleScript skills have become nonexistent and I'm getting nowhere. So if possible, full code, not just snippets.
The file setup is always the same, but there are many variations (ex: Yellowst.Nat.Park.D12P55.DMS.3248.jpg)
It starts with a place name, should be a find and replace for a bunch of different strings, ("Yellowst.Nat.Park" -> "Yellowstone National Park")
Then it is followed by two numbers that should be changed in format (D12P55 -> [12x55]). They're always set up in a "D" followed by two numbers, a "P" and again two numbers.
And it ends with a random string, can be numbers, letters etc, which all have to go. They differ in format and length, no pattern in them.
Basically I want to go from "Yellowst.Nat.Park.D12P55.DMS.3248.jpg" to "Yellowstone National Park [02x03] .jpg" I want to add text afterwards so want to end with a space.
The best way to do this seems to me a repetitive find and replace for the first part, Make a list for a bunch of terms wich have to be replaced by a bunch of respective terms. Followed by a detection of the number format and ending with deleting of the random string after it.
Here is another approach.
property pictureFolder : (alias "Mac OS X:Users:Sam:Pictures:test:")
property findList : {"Yellowst.Nat.Park", "Jellyst.Nat.Park"}
property replaceList : {"Yellowstone National Park", "Jellystone \\& National Park"}
tell application "System Events"
set nameList to (name of every file of pictureFolder whose visible = true)
repeat with i from 1 to count of (list folder pictureFolder without invisibles)
set fileName to item i of nameList
set fileExtension to (name extension of (file fileName of pictureFolder))
repeat with j from 1 to count of findList
if fileName contains item j of findList then
set tempName to do shell script "echo " & fileName & " | sed 's/.D\\([0-9][0-9]\\)P\\([0-9][0-9]\\).*/[\\1x\\2] " & i & "." & fileExtension & "/'"
set tempName to do shell script "echo " & tempName & " | sed 's/^" & item j of findList & "/" & item j of replaceList & " /'"
set name of (file fileName of pictureFolder) to tempName
exit repeat
else if j = (count of findList) then
set tempName to do shell script "echo " & fileName & " | sed 's/[.]/ /g'"
set tempName to do shell script "echo " & tempName & " | sed 's/.D\\([0-9][0-9]\\)P\\([0-9][0-9]\\).*/ [\\1x\\2] " & i & "." & fileExtension & "/'"
set name of (file fileName of pictureFolder) to tempName
end if
end repeat
end repeat
end tell
To avoid duplicate names, I added a counter to the end of the file name. If there are no duplicates, you can use this instead:
set tempName to do shell script "echo " & fileName & " | sed 's/.D\\([0-9][0-9]\\)P\\([0-9][0-9]\\).*/[\\1x\\2] " & "." & fileExtension & "/'"
I like small challenges like this Sam. They're fun to me... maybe I'm sick ;). Anyway, I wrote you a handler to clean the file name as you requested. It's not really hard to manipulate text in applescript if you're comfortable with text item delimiters and such. These small challenges keep my text skills sharp.
NOTE: in the nameList property the name must end with a period or whatever character is just before the letter D in the number sequence DxxPxx as you mentioned.
So give this a try. Plug in a variety of fileNames and ensure it works how you want. Of course you need to put more values into the nameList and nameReplaceList properties too.
property nameList : {"Yellowst.Nat.Park."}
property nameReplaceList : {"Yellowstone National Park"}
set fileName to "Yellowst.Nat.Park.D12P55.DMS.3248.jpg"
cleanFilename(fileName)
(*================ SUBROUTINES ================*)
on cleanFilename(fileName)
-- first find the base name and file extension of the file name
set tids to AppleScript's text item delimiters
set ext to ""
if fileName contains "." then
set AppleScript's text item delimiters to "."
set textItems to text items of fileName
set ext to "." & item -1 of textItems
set baseName to (items 1 thru -2 of textItems) as text
set text item delimiters to ""
else
set baseName to fileName
end if
-- next find the pattern D, 2 numbers, P, and 2 numbers in the baseName
set chars to characters of baseName
set theSequence to missing value
repeat with i from 1 to (count of chars) - 6
set thisChar to item i of chars
if thisChar is "d" and item (i + 3) of baseName is "p" then
try
set firstNum to text (i + 1) thru (i + 2) of baseName
firstNum as number
set secondNum to text (i + 4) thru (i + 5) of baseName
secondNum as number
set theSequence to text i through (i + 5) of baseName
exit repeat
end try
end if
end repeat
-- now make the changes
if theSequence is not missing value then
set AppleScript's text item delimiters to theSequence
set theParts to text items of baseName
set fixedFirstPart to item 1 of theParts
repeat with i from 1 to count of nameList
if item i of nameList is fixedFirstPart then
set fixedFirstPart to item i of nameReplaceList
exit repeat
end if
end repeat
set fixedName to fixedFirstPart & " [" & firstNum & "x" & secondNum & "]" & ext
else
set fixedName to fileName
end if
set AppleScript's text item delimiters to tids
return fixedName
end cleanFilename
Now if you want to automate this for a folder full of files you can use this code. Just replace lines 3 and 4 of the above script with this. I didn't check this code but it's simple enough it should work as-is.
NOTE: you don't need to worry if non-image files are in the folder you choose with this code because they won't (I'm assuming this) have the DxxPxx number sequence and thus this script will not change them in any way.
set theFolder to choose folder
tell application "Finder"
set theFiles to files of theFolder
repeat with aFile in theFiles
set thisName to name of aFile
set newName to my cleanFilename(thisName)
if newName is not thisName then
set name of aFile to newName
end if
end repeat
end tell