Still teaching myself Applescript and still having fun
Made quite some progress the last days thanks to this wonderful forum !
I gave myself another assignment:
"Throw all sorts of files in a folder. The folder puts them into subfolders which are
named with the filesâ extensions. If no such subfolder exists, create it. Files without
extensions go to a folder âno extensionâ. "
Example: all xxx.jpg go to a subfolder called âjpgâ
Everything works fine (Iâm so proud i got this going ) but there are some questionsâŚ
Problem 1: why can I create a subfolder with a fixed string but not with a generated
string? Please see comments!
Problem 2: if I move the folder into another folder or rename the folder the script is activated! (A folder DS_Store is created and filled with the invisible file .DS_Store)
Edit:
Problem 3: I just tried it with 200 files! Boy, oh boy, how does this script choke! It seems to work but it takes forever! I have never seen so many beachballs of death! Restarting the Finder had no effect. I had to restart the whole Mac!
on adding folder items to this_folder after receiving added_items
set basepath to this_folder as string
--something weird happened here: I first tested my folder action with a fixed string
--later on in the script I use basepath to create the subfolders. This works fine.
--set basepath to "Macintosh HD:Users:vml:Desktop:opentest:" as string
--In a later stage I set basepath to the current folder whereever that is:
--set basepath to this_folder as string
--creating the subfolders fails!
repeat with f in added_items
set this_info to info for f
if not folder of this_info then -- its not a folder
set fstring to f as string
set olddelimiters to AppleScript's text item delimiters --I'll move this out of the loop I guess
set AppleScript's text item delimiters to {"."}
set extstring to get last text item of fstring
if extstring = fstring then -- there is no extension. This will go wrong with filenames that do contain a "." somewhere
set extstring to "no extension"
end if
set AppleScript's text item delimiters to olddelimiters --I'll move this out of the loop I guess
set fullpath to basepath & extstring
try
move f to alias fullpath
on error
try -- this try is only because of the next problem, later I removed it.
-- In the next line basepath fails, I have to use this_folder
-- when basepath was a fixed string it worked fine!!
-- when basepath was set by this_folder it fails!!!
tell application "Finder" to set x to make new folder at this_folder (*why not basepath????*) with properties {name:extstring}
on error
display dialog basepath buttons {"Cancel"} default button 1
-- the dialog shows the correct basepath!!!
end try
move f to alias fullpath
end try
end if
end repeat
end adding folder items to