Saturday, June 24, 2017

#1 2007-08-27 04:00:41 am

Kevin Bradley
Administrator
From: Independence, MO
Registered: 2006-03-13
Posts: 548
Website

Common Folder Action Problems

Not long ago (about a month ago, as a matter of fact) we here at MacScripter received a message from someone asking about a specific kind of folder action.  The gentleman wanted to know how to create a folder action that would accept a dropped file, then create a new subfolder named like the dropped file, and finally place the file in the new subfolder. Specifically, he seemed frustrated that he couldn't just copy the name of the file to a new folder.

On writing to this gentleman, I found out he lives in Pamplona, Spain, so English wasn't his native language. He writes English very well, but I began to immediately see why he might be having problems with some of AppleScript's english terms, and so I began corresponding with Professor Odero in order to help him with his problem. Despite what some may think of my writing skills, English is my native language but Spanish has been my adopted second language since high school and I still relish an opportunity to brush up on it. And when all else fails, I can always fall back on my Dashboard Translator widget to help me!

The first stab at writing my own script revealed Professor Odero's main hurdle. Try this in Script Editor (and keep your mouse pointer over the Stop icon!):

Applescript:


on adding folder items to theFolder after receiving theItems
   repeat with anItem in theItems
       tell application "Finder"
           set theName to displayed name of anItem
           set newFolder to (make new folder at theFolder with properties {name:theName})
           move anItem to newFolder
       end tell
   end repeat
end adding folder items to

See the problem? When you create the new subfolder in the action folder, AppleScript sees that as "adding folder items" and starts an infinite loop of new folders!

Now, in a situation like this there are (at least) two ways you can straighten it out. One, the obvious way, is to create the new folder somewhere besides the action folder. That might be an acceptable solution for some applications, but in this case, we specifically wanted the new folder in the action folder. It doesn't help us much to have an action folder if we can't keep anything in it. If we wanted that kind of solution, it would make more sense to create a droplet: a script application that can respond when files or folders are dropped on it.

So the other means of attack for this kind of problem is to create the subfolder in such a way that it won't be noticed by your action script. The one way to do that is to manipulate the folder name so that it has a known character (or character sequence) embedded in it. For our purpose, if the file dropped is "Readme" we want a folder called "Readme ƒ" to be created.

The "ligature f" character (option-f) has often been used on the Macintosh as shorthand for the word "folder," so it seemed like a good choice. However, since the word "folder" in Spanish is "carpeta," we could have used a 'c" character of some kind. Fortunately, Professor Ordero is bilingual and the "f" ligature worked for him too. For your own uses, though, just remember that you can substitute other strings. For uses like this, I also like the bullet character (option-8).

So, we now modify our script as follows:

Applescript:


on adding folder items to theFolder after receiving theItems
   repeat with anItem in theItems
       tell application "Finder"
           set theName to displayed name of anItem
           set theName to theName & " ƒ"
           set newFolder to (make new folder at theFolder with properties {name:theName})
           move anItem to newFolder
       end tell
   end repeat
end adding folder items to

Don't run this version, yet! As you might have noticed, all we've done is add the "ƒ" character to the folder name. We still have to fool the script into NOT going into an infinite loop. The next modification is to enclose our main code in an IF block that tests for the "ƒ" character in the new item's name:

Applescript:


on adding folder items to theFolder after receiving theItems
   repeat with anItem in theItems
       tell application "Finder"
           set theName to displayed name of anItem
           if theName does not contain "ƒ" then
               set theName to theName & " ƒ"
               set newFolder to (make new folder at theFolder with properties {name:theName})
               move anItem to newFolder
           end if
       end tell
   end repeat
end adding folder items to

Now we have a script that won't go off creating folders until the Finder goes insane! To finish up, I always like to use a try block if I have code that might generate errors. You'll find that Apple's Mail and other programs often won't report an AppleScript error if it occurs, so you need to help yourself to find out when things are going wrong. Here's the finished script:

Applescript:


on adding folder items to theFolder after receiving theItems
   repeat with anItem in theItems
       tell application "Finder"
           set theName to displayed name of anItem
           if theName does not contain "ƒ" then
               try
                   set theName to theName & " ƒ"
                   set newFolder to (make new folder at theFolder with properties {name:theName})
                   move anItem to newFolder
               on error the error_message number the error_number
                   display dialog "Folder action caused an error: " & the error_number & ". " & the error_message buttons {"OK"} default button 1
               end try
           end if
       end tell
   end repeat
end adding folder items to

Never be discouraged if you seem to run into a blank wall with scripting. Often times it just takes "turning the problem upside down" to see a different way of doing what you want to do!


Nitewing '98
--
I distrust morning people, largely because I suspect them of getting together early one day while the rest of us were asleep and setting up the rules of civilization.

Filed under: folder, action, Finder, Bradley

Offline

 

#2 2009-02-15 05:21:53 pm

acadie
Member
Registered: 2008-12-07
Posts: 2

Re: Common Folder Action Problems

hi Kevin!
last year i spent half a day trying to find a solution to the same problem as Professor Odero... then i found your excellent post!

it did the trick superbly!
however, after not using it for a long while, when i tried to use your script again, it just doesn't work anymore.. nothing happens. I'm now on 10.5.6 and i did read somewhere that applescript changed some aspects of it's coding... so i'm wondering if this applescript change might have broken the code....? or it's something i'm not getting.

your help, or anybody else's would be very appreciated!
thank you!!
smile

Offline

 

#3 2011-08-21 12:26:25 pm

geowar
Member
Registered: 2011-02-15
Posts: 1

Re: Common Folder Action Problems

I would have just written the script to ignore dropped folders

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.039 seconds, 9 queries executed ]

RSS (new topics) RSS (active topics)