zim
January 18, 2005, 4:56am
#1
I’m trying to get a folder action to sort dropped files into folders based on the following critera:
All files names are in this form:
blah_blah_blah.pdf
Folders are derived from filenames, and contain everything from the start up to but not including the second underscore.
For example:
test2_orange_fig1.pdf
should be placed in a folder named
test2_orange
Now, I can handle everything up to and after this point, but I can’t get a variable in the right format to create the folder with the correct name, can anyone suggest a way in.
Thanks!
Well, this is what I tried (and it works!)
I created the new subfolders in the drop folder ,
but you could create them elsewhere of course.
on adding folder items to this_folder after receiving added_items
set basepath to this_folder as string
set olddelimiters to AppleScript ’s text item delimiters –save TIDs
set AppleScript ’s text item delimiters to {“:”} –to get parts of the file path
[color=blue][b]repeat[/b][/color] [color=blue][b]with[/b][/color] [color=green]theFile[/color] [color=blue][b]in[/b][/color] [color=green]added_items[/color]
[color=blue][b]set[/b][/color] [color=green]file_info[/color] [color=blue][b]to[/b][/color] [color=blue]info for[/color] [color=green]theFile[/color]
[color=blue][b]if[/b][/color] [color=blue][b]not[/b][/color] [color=blue]folder[/color] [color=blue][b]of[/b][/color] [color=green]file_info[/color] [color=blue][b]then[/b][/color] --[color=olive][i] the folder you create further on is also added to added_items !!! You don't want to do anything with it[/i][/color]
[color=blue][b]set[/b][/color] [color=green]filestring[/color] [color=blue][b]to[/b][/color] [color=blue]name[/color] [color=blue][b]of[/b][/color] [color=green]file_info[/color] --[color=olive][i]get the filename[/i][/color]
[color=blue][b]set[/b][/color] [color=blue]AppleScript[/color]'s [color=blue]text item delimiters[/color] [color=blue][b]to[/b][/color] {"_"}
[color=blue][b]set[/b][/color] [color=green]first2parts[/color] [color=blue][b]to[/b][/color] [color=blue][b]get[/b][/color] [color=blue]text items[/color] 1 [color=blue][b]thru[/b][/color] 2 [color=blue][b]of[/b][/color] [color=green]filestring[/color] --[color=olive][i]gets aaa and bbb from aaa_bbb_ccc and puts them in a list[/i][/color]
[color=blue][b]set[/b][/color] [color=green]newfoldername[/color] [color=blue][b]to[/b][/color] [color=green]first2parts[/color] [color=blue][b]as[/b][/color] [color=blue]string[/color] --[color=olive][i]turn the list into a string aaa_bbb[/i][/color]
[color=blue][b]set[/b][/color] [color=blue]AppleScript[/color]'s [color=blue]text item delimiters[/color] [color=blue][b]to[/b][/color] {":"} --[color=olive][i]to manipulate the path[/i][/color]
[color=blue][b]set[/b][/color] [color=green]fullpath[/color] [color=blue][b]to[/b][/color] [color=green]basepath[/color] & [color=green]newfoldername[/color]
[color=blue][b]try[/b][/color] --[color=olive][i]to move the file to the subfolder aaa_bbb[/i][/color]
[color=blue][b]tell[/b][/color] [color=blue]application[/color] "Finder" [color=blue][b]to[/b][/color] [color=blue]move[/color] [color=green]theFile[/color] [color=blue]to[/color] [color=blue]alias[/color] [color=green]fullpath[/color] [color=blue][b]with[/b][/color] [color=blue]replacing[/color]
[color=blue][b]on[/b][/color] [color=blue][b]error[/b][/color] [color=green]errormsg[/color] --[color=olive][i] if there is no folder, then make the folder and move the file into it[/i][/color]
[color=blue][b]tell[/b][/color] [color=blue]application[/color] "Finder"
[color=blue]make[/color] [color=blue]new[/color] [color=blue]folder[/color] [color=blue]at[/color] [color=green]this_folder[/color] [color=blue]with properties[/color] {[color=blue]name[/color]:[color=green]newfoldername[/color]}
[color=blue][b]end[/b][/color] [color=blue][b]tell[/b][/color]
[color=blue]move[/color] [color=green]theFile[/color] [color=blue]to[/color] [color=blue]alias[/color] [color=green]fullpath[/color]
[color=blue][b]end[/b][/color] [color=blue][b]try[/b][/color]
[color=blue][b]end[/b][/color] [color=blue][b]if[/b][/color]
[color=blue][b]end[/b][/color] [color=blue][b]repeat[/b][/color]
[color=blue][b]set[/b][/color] [color=blue]AppleScript[/color]'s [color=blue]text item delimiters[/color] [color=blue][b]to[/b][/color] [color=green]olddelimiters[/color] --[color=olive][i] reset TIDs[/i][/color]
end adding folder items to
[This script was automatically tagged for color coded syntax by Convert Script to Markup Code ]
zim
January 19, 2005, 2:32am
#3
Hi DutchVince
fistly, many thanks for taking the time to reply in such detail!
It works great on my local drive, but after a couple of hours of jiggering I still haven’t got it to work and send the files over to a mounted network drive. Here’s what I have so far …
–Open this script in a new Script Editor window.
on adding folder items to this_folder after receiving these_files
tell application “Finder”
--[i][color=olive] MOUNT NETWORK HARD DRIVE[/color][/i]
[b][color=blue]set[/color][/b] [color=green]ThisVolume[/color] [b][color=blue]to[/color][/b] "afp://netdrive:12345678@zim.local/netdrive"
[b][color=blue]try[/color][/b]
[b][color=blue]if[/color][/b] ([color=blue]list disks[/color]) [b][color=blue]does not[/color][/b] [b][color=blue]contain[/color][/b] "netdrive" [b][color=blue]then[/color][/b] [color=blue]mount volume[/color] [color=green]ThisVolume[/color]
[b][color=blue]end[/color][/b] [b][color=blue]try[/color][/b]
--[i][color=olive] PRELIMS[/color][/i]
[b][color=blue]set[/color][/b] [color=green]destination_folder[/color] [b][color=blue]to[/color][/b] "netdrive:Documents:SortedPDFs:" [b][color=blue]as[/color][/b] [color=blue]string[/color]
[b][color=blue]set[/color][/b] [color=green]olddelimiters[/color] [b][color=blue]to[/color][/b] [color=blue]AppleScript[/color]'s [color=blue]text item delimiters[/color] --[i][color=olive]save TIDs[/color][/i]
[b][color=blue]set[/color][/b] [color=blue]AppleScript[/color]'s [color=blue]text item delimiters[/color] [b][color=blue]to[/color][/b] {":"} --[i][color=olive]to get parts of the file path[/color][/i]
--[i][color=olive] LOOP, SORTING AND COPYING[/color][/i]
[b][color=blue]repeat[/color][/b] [b][color=blue]with[/color][/b] [color=green]theFile[/color] [b][color=blue]in[/color][/b] [color=green]these_files[/color]
[b][color=blue]set[/color][/b] [color=green]file_info[/color] [b][color=blue]to[/color][/b] [color=blue]info for[/color] [color=green]theFile[/color]
[b][color=blue]if[/color][/b] [b][color=blue]not[/color][/b] [color=blue]folder[/color] [b][color=blue]of[/color][/b] [color=green]file_info[/color] [b][color=blue]then[/color][/b] --[i][color=olive] the folder you create further on is also added to these_files !!! You don't want to do anything with it [/color][/i]
[b][color=blue]set[/color][/b] [color=green]filestring[/color] [b][color=blue]to[/color][/b] [color=blue]name[/color] [b][color=blue]of[/color][/b] [color=green]file_info[/color] --[i][color=olive]get the filename [/color][/i]
[b][color=blue]set[/color][/b] [color=blue]AppleScript[/color]'s [color=blue]text item delimiters[/color] [b][color=blue]to[/color][/b] {"_"}
[b][color=blue]set[/color][/b] [color=green]first2parts[/color] [b][color=blue]to[/color][/b] [b][color=blue]get[/color][/b] [color=blue]text items[/color] 1 [b][color=blue]thru[/color][/b] 2 [b][color=blue]of[/color][/b] [color=green]filestring[/color] --[i][color=olive]gets aaa and bbb from aaa_bbb_ccc and puts them in a list [/color][/i]
[b][color=blue]set[/color][/b] [color=green]newfoldername[/color] [b][color=blue]to[/color][/b] [color=green]first2parts[/color] [b][color=blue]as[/color][/b] [color=blue]string[/color] --[i][color=olive]turn the list into a string aaa_bbb [/color][/i]
[b][color=blue]set[/color][/b] [color=blue]AppleScript[/color]'s [color=blue]text item delimiters[/color] [b][color=blue]to[/color][/b] {":"} --[i][color=olive]to manipulate the path [/color][/i]
[b][color=blue]set[/color][/b] [color=green]fullpath[/color] [b][color=blue]to[/color][/b] [color=green]destination_folder[/color] & [color=green]newfoldername[/color]
[b][color=blue]try[/color][/b] --[i][color=olive]to move the file to the subfolder aaa_bbb [/color][/i]
[b][color=blue]tell[/color][/b] [color=blue]application[/color] "Finder" [b][color=blue]to[/color][/b] [color=blue]move[/color] [color=green]theFile[/color] [color=blue]to[/color] [color=blue]alias[/color] [color=green]fullpath[/color] [b][color=blue]with[/color][/b] [color=blue]replacing[/color]
[b][color=blue]on[/color][/b] [b][color=blue]error[/color][/b] [color=green]errormsg[/color] --[i][color=olive] if there is no folder, then make the folder and move the file into it [/color][/i]
[b][color=blue]tell[/color][/b] [color=blue]application[/color] "Finder"
[color=blue]make[/color] [color=blue]new[/color] [color=blue]folder[/color] [color=blue]at[/color] [color=green]this_folder[/color] [color=blue]with properties[/color] {[color=blue]name[/color]:[color=green]newfoldername[/color]}
[b][color=blue]end[/color][/b] [b][color=blue]tell[/color][/b]
[color=blue]move[/color] [color=green]theFile[/color] [color=blue]to[/color] [color=blue]alias[/color] [color=green]fullpath[/color]
[b][color=blue]end[/color][/b] [b][color=blue]try[/color][/b]
[b][color=blue]end[/color][/b] [b][color=blue]if[/color][/b]
[b][color=blue]end[/color][/b] [b][color=blue]repeat[/color][/b]
[b][color=blue]set[/color][/b] [color=blue]AppleScript[/color]'s [color=blue]text item delimiters[/color] [b][color=blue]to[/color][/b] [color=green]olddelimiters[/color] --[i][color=olive] reset TIDs[/color][/i]
--[i][color=olive] TIDY UP [/color][/i]
[color=blue]delete[/color] [color=green]these_files[/color]
[color=blue]empty[/color] [color=blue]trash[/color]
[b][color=blue]end[/color][/b] [b][color=blue]tell[/color][/b]
end adding folder items to
The script mounts the drive no problem, the error seems to be in the file copying sections in the loop. I’ll carry on puzzling over it and report back if I crack it myself. Again, many thanks!
[This script was automatically tagged for color coded syntax by Convert Script to Markup Code ]