Post date: Aug 21, 2010 1:21:22 AM
Problem : I have a bunch of hand images approximately 6-7 images of the left and right hand of 20 people. I have to arrange them into separate folders according to the hand and the person so the heirarchy would be like "left -> #" and "right -> #" where left , right denote the left and right hand and # is the index of the person whose hand image it is.
Solution : I wrote an applescript to automate the task of making a new folder for each person's hand image and of putting the images in the "left" and "right" folder alternately. now i just have to drag images to a specific folder to which i have attached my script and then the files will automatically be put in the "left->#+1" folder if the previous file was put in "right->#" folder
Hacks possible from this code:
Folder actions
Creating variable in applescript
checking for file existence
getting output from shell script
giving input to shell script
getting the Mac folder name and the Posix folder name
looping through files in a list of files
If then elseif else conditional statements
telling finder to do something
SourceCode:
on adding folder items to this_folder after receiving added_items
-- insert actions here
tell application "Finder"
set foldName to the name of this_folder
set posixfoldName to the POSIX path of this_folder
set Lflagname to foldName & ":L"
set Rflagname to foldName & ":R"
set posixLflagname to posixfoldName & "L"
set posixRflagname to posixfoldName & "R"
if exists file Lflagname then
try
set filecount to do shell script "ls -d -p " & posixfoldName & "left/*" & " | grep -v \"No such file\" -c"
on error errMsg number errNum
set filecount to "0"
end try
set filecount to filecount as integer
do shell script "mkdir " & posixfoldName & "left/" & (filecount + 1)
do shell script "mv " & posixLflagname & " " & posixRflagname
--move these_items to the foldName & "left/" & (filecount + 1)
try
repeat with ix from 1 to number of items in added_items
set new_item to item ix of added_items
set the item_path to the POSIX path of new_item
do shell script ("mv " & item_path & " " & posixfoldName & "left/" & (filecount + 1) & "/")
end repeat
end try
else if exists file Rflagname then
try
set filecount to do shell script "ls -d -p " & posixfoldName & "right/*" & " | grep -v \"No such file\" -c"
on error errMsg number errNum
set filecount to "0"
end try
set filecount to filecount as integer
do shell script "mkdir " & posixfoldName & "right/" & (filecount + 1)
do shell script "mv " & posixRflagname & " " & posixLflagname
try
repeat with ix from 1 to number of items in added_items
set new_item to item ix of added_items
set the item_path to the POSIX path of new_item
do shell script ("mv " & item_path & " " & posixfoldName & "right/" & (filecount + 1) & "/")
end repeat
end try
else
make new file at this_folder with properties {name:"R"}
do shell script "mkdir " & posixfoldName & "right"
do shell script "mkdir " & posixfoldName & "left"
end if
end tell
end adding folder items to