Hi All,
I’m currently working on a folder action script to file away pdfs on a server, changing there names once they’ve copied. The problem is i can’t figure out how to make a variable from each name of the pdfs, (hopefully there will be multiple pdfs being filed at once - well that’s what i’m aiming for). The script works when i give the script an exact name of the file, but i can’t get it to use variables for each pdf. each pdf has usually a code at the front and then a number eg. c8210_123456_123.pdf and i’m trying to strip each element of the word so it can be filed in the correct place.
Here’s my script…
on adding folder items to this_folder after receiving these_items
tell application "Finder"
try
mount volume "smb://nas3/PDF_Volumes/"
end try
end tell
end adding folder items to
tell application "Finder"
repeat
set the file_name to the name of this_item
set clientCode to first word of file_name
set jobno to third word of file_name
set U to fourth word of file_name
set artRefamRef to fifth word of file_name
set artRef to characters 1 thru 3 of artRefamRef as string
set fileName to clientCode & "_" & jobno & artworkref & amref & fileXTpdf
set jobFolder to jobno & U & artRef
set new_name to jobno & U & artRef & fileXTpdf as string
set new_nameAM to jobno & U & artRefamRef & fileXTpdf
try
make new folder at folder "PDF_Client_Folders" of folder "PDF_Client_Folders" of disk "PDF_Volumes" with properties {name:clientCode}
end try
try
make new folder at folder clientCode of folder "PDF_Client_Folders" of folder "PDF_Client_Folders" of disk "PDF_Volumes" with properties {name:jobFolder}
end try
try
make new folder at folder "PDF_Client_Folders" of folder "PDF_Client_Folders" of disk "PDF_Volumes" with properties {name:clientCode}
end try
try
make new folder at folder clientCode of folder "PDF_Client_Folders" of folder "PDF_Client_Folders" of disk "PDF_Volumes" with properties {name:jobFolder}
end try
set shell1 to "cp -r ~/Desktop/ToConvert/Original_Images/" & file_name & " /Volumes/PDF_Volumes/PDF_Client_Folders/PDF_Client_Folders/" & clientCode & "/" & jobFolder & "/" & new_name & ""
do shell script shell1
end repeat
end tell
Any pointers, or help would be great.
Thanks
Kyle.
Kyle;
For openers, I don’t see an exit to your repeat loop, and your first line in the loop has no reference to what “this_item” is so the script errors right away. On adding folder items after… produces a list and you want to process it as a list (even if only one item was added):
repeat with k from 1 to count these_items
– do stuff
end repeat
Or
repeat with anItem in these_items
– do stuff to contents of anItem – anItem is a reference to the element of the original list these_items, not the item itself.
end repeat
Then, taking c8210_123456_123.pdf as a typical file name, it has 6 “words”: “c8210” , “", “123456”, "”, “123”, “pdf”. Assuming your file names are always separated by underscores, the “generic” way to go is this (although what you have will work):
set f to "c8210_123456_123.pdf"
set U to "_"
set tid to AppleScript's text item delimiters -- save what they were
set AppleScript's text item delimiters to U -- what we want as the separation
set p to text items 1 thru -2 of f -- grab the first two pieces
set t to last text item of f -- the last one
set AppleScript's text item delimiters to "."
set p to p & text items of t
set AppleScript's text item delimiters to tid -- set them back as they were
p --> {"c8210", "123456", "123", "pdf"}
I didn’t go any further because I didn’t understand your objectives. You are making a bunch of folders, but suppose they already exist? Then I would think that all you wanted was the path to the correct one, and instead of making a bunch, you’d just build a path from the parts you’ve already identified of the file name, and since you intend to use a shell script (which I didn’t look at) why not a posix path (separated by /), like c8210/123456/123
set f to "c8210_123456_123.pdf"
set U to "_"
set tid to AppleScript's text item delimiters -- save what they were
set AppleScript's text item delimiters to U -- what we want as the separation
set b to text items of f
set AppleScript's text item delimiters to "/"
set p to b as string
set AppleScript's text item delimiters to "."
set p to first text item of p
set AppleScript's text item delimiters to tid
p --> "c8210/123456/123"
Edited for more direct version.
Hi Adam,
Thanks for your post.
With your apple script how would i make “f” (c8120_123456_123.pdf) into seperate variable’s that will change?
Like zo:
This is the “formal” but rather generic way to do it:
set this_file to "c8210_123456_123.pdf"
set codes to getCodes(this_file)
set client to item 1 of codes
set job to item 2 of codes
set art to item 3 of codes
set filePath to item 4 of codes
to getCodes(f)
set U to "_"
set tid to AppleScript's text item delimiters -- save what they were
set AppleScript's text item delimiters to U -- what we want as the separation
set b to text items of f
set AppleScript's text item delimiters to "."
set temp to text items of last item of b
set last item of b to first item of temp
set AppleScript's text item delimiters to "/"
set tPath to (text items of b) as string
set {CC, JN, ARef, tP} to (b & tPath)
return {CC, JN, ARef, tP}
end getCodes
Thanks for your help!!!
Finally got it working after a few hours of just staring at my screen!!!
I took into account what you said about the repeat issues…
Here’s the finished script…
on adding folder items to this_folder after receiving these_items
tell application "Finder"
set fileXTpdf to ".pdf"
set countitems to files of this_folder whose name contains "pdf"
set count1 to (count of countitems)
repeat with n from 1 to count1
set fileName to the name of file n of this_folder as string
set clientCode to first word of fileName as string
set jobno to third word of fileName as string
set U to fourth word of fileName as string
set artRefamRef to fifth word of fileName as string
set artRef to characters 1 thru 3 of artRefamRef as string
set jobFolder to jobno & U & artRef as string
set new_name to jobno & U & artRef & fileXTpdf as string
set new_nameAM to jobno & U & artRefamRef & fileXTpdf as string
try
mount volume "smb://nas3/PDF_Volumes/"
end try
try
make new folder at folder "PDF_Client_Folders" of folder "PDF_Client_Folders" of disk "PDF_Volumes" with properties {name:clientCode}
end try
try
make new folder at folder clientCode of folder "PDF_Client_Folders" of folder "PDF_Client_Folders" of disk "PDF_Volumes" with properties {name:jobFolder}
end try
if length of artRefamRef is equal to 11 then
set shell1 to "cp -r ~/Desktop/ToConvert/Original_Images/" & fileName & " /Volumes/PDF_Volumes/PDF_Client_Folders/PDF_Client_Folders/" & clientCode & "/" & jobFolder & "/" & new_nameAM & ""
do shell script shell1
else
set shell2 to "cp -r ~/Desktop/ToConvert/Original_Images/" & fileName & " /Volumes/PDF_Volumes/PDF_Client_Folders/PDF_Client_Folders/" & clientCode & "/" & jobFolder & "/" & new_name & ""
do shell script shell2
end if
end repeat
end tell
end adding folder items to