PDA

View Full Version : Using folder actions to trigger Perl script


paulrockNJ
08-24-2004, 11:25 AM
Is this even possible? I don't really know anything about 1) Folder Actions, 2) Applescript, or 3) Terminal, but I was wondering if the following is possible:

I have a Perl script that I run occasionally that transforms all the text files in a certain folder. Here's what I type into terminal: "perl convert.pl FOLDER loose" (no quotes). Pretty self explanatory I think (except for maybe "loose" -- that just tells the script how to behave).

What I'd love to do it set it up so that all I have to do is drag FOLDER onto a Folder Actions enabled folder, and have it spit out the transformed files.

Possible? Any idea how to do it?

hayne
08-24-2004, 01:01 PM
I believe that folder actions is limited to AppleScript.
But AppleScript can call an arbitrary shell script via "do script" (search these forums and you will find lots of examples) so all you have to do is arrange for that shell script (or Perl script) to be passed the right arguments on the command line.

robJ
08-24-2004, 01:08 PM
It might be possible. Here's an (untested) example of what the folder action code might look like:


on adding folder items to this_folder after receiving added_items
repeat with item_ in added_items
if folder of (info for item_) is true then
do shell script "perl convert.pl " & ¬
(quoted form of POSIX path of item_) & " loose"
end if
end repeat
end adding folder items to
-- Rob

paulrockNJ
08-24-2004, 02:50 PM
Hmmm, this didn't work. Where does the convert.pl have to be placed? If the Folder that has the actions applied to it is living on the desktop, does the .pl go on the desktop or in the folder?

frankko
08-24-2004, 03:34 PM
I think the best thing to do would be to place the .pl script anywhere you wanted (I have a folder in my Documents folder called Scripts), but in the AppleScript, call it by its full path, for example /Users/username/Documents/Scripts/convert.pl.

If you set the script to be executable and have a proper shebang at the top of the script (#!/path/to/perl), you can just call the script directly instead of starting your line with "perl ". In other words, not

perl /Users/username/Documents/Scripts/convert.pl loose

but rather

/Users/username/Documents/Scripts/convert.pl loose

But that's a minor issue. I think what's important is, however you call the script, reference its full path.

bramley
08-24-2004, 03:57 PM
I've checked RobJ's script and there's nothing wrong with the AS end of things.

Just to confirm Frankko's point, you may need a UNIX path to the script, if your script is not on the shell's path. There does seem to be a difference between paths defined in tcsh and those used by the 'do shell script' command in Applescript.

do shell script "/UNIX/path/to/the/script/perl convert.pl " & ¬
(quoted form of POSIX path of item_) & " loose"


Another possible problem is that the Folder Action script must be in '~/Library/Scripts/Folder Action Scripts'

Also make sure everything is set up properly to use FAs. To check that go here: http://www.apple.com/applescript/folderactions/01.html

hayne
08-24-2004, 05:02 PM
The answer about the working directory for "do script" (and many other frequently asked questions) is in this Apple document:
http://developer.apple.com/technotes/tn2002/tn2065.html

Newbish
08-25-2004, 02:02 PM
The only time I've gotten shell scripts to activate from Applescript was telling Terminal to "do script". I've successfully run shell scripts and PHP in that manner.

example:


on adding folder items to this_folder after receiving these_items
tell application "Terminal"
do script "/Users/newbish/bin/test.php" in window frontmost
end tell
end adding folder items to


The above is the actual folder action script. It in turn calls a PHP routine, which takes care of the things I needed taking care of when something is dropped in that folder.

As I understand it, in Tiger, Applescript will have much better access to UNIX layer routines and programs. But don't take my word for it; I still can't find the reference I read that gave me that impression.

paulrockNJ
08-25-2004, 06:04 PM
Here's what I've got so far.....

on adding folder items to this_folder after receiving added_items
repeat with item_ in added_items
if folder of (info for item_) is true then
tell application "Terminal"
do script "cd /Users/pmaiorana/Desktop"
delay 1
do script "perl convert.pl " & (POSIX path of item_) & " loose" in window frontmost
end tell
end if
end repeat
end adding folder items to

If I replace (POSIX path of item_) with "NAME OF FOLDER" this works perfectly. It seems I need to pull in just the name of the folder, not the path, no slashes, no anything. Just the folder name.

Possible?

robJ
08-26-2004, 12:57 AM
If I replace (POSIX path of item_) with "NAME OF FOLDER" this works perfectly. It seems I need to pull in just the name of the folder, not the path, no slashes, no anything. Just the folder name.

Possible?

This might work:


on adding folder items to this_folder after receiving added_items
repeat with item_ in added_items
set info_ to (info for item_)
if folder of info_ is true then
tell application "Terminal"
do script "cd /Users/pmaiorana/Desktop"
delay 1
do script ("perl convert.pl " & (quoted form of (name of info_)) & ¬
" loose") in window frontmost
end tell
end if
end repeat
end adding folder items to
-- Rob

paulrockNJ
08-26-2004, 03:55 PM
Got it! Here's what I wound up with:

on adding folder items to this_folder after receiving added_items
repeat with item_ in added_items
set info_ to (info for item_)
if folder of info_ is true and name of info_ does not contain "converted" then
tell application "Terminal"
do script "cd /Users/pmaiorana/Desktop/FOLDER"
delay 1
do script ("perl /Users/pmaiorana/Documents/Scripts/convert.pl " & ((name of info_)) & " loose") in window frontmost
end tell
end if
end repeat
end adding folder items to
on closing folder window for this_folder
tell application "Finder"
activate
delete entire contents of this_folder
end tell
end closing folder window for

robJ
08-26-2004, 05:26 PM
This looks ok as long as the name of the folder does not contain spaces (or anything else that should be escaped for the shell). Is there a reason why you removed the "quoted form of"? As far as I know, it shouldn't hurt anything.

-- Rob (who is better at AppleScript scripting than shell scripting)

paulrockNJ
08-26-2004, 05:36 PM
Yeah, the folder is only ever a string of numbers, so spaces aren't a problem.

I removed the "quoted form of" while I was trying to get this all to work, thinking maybe the quotes were throwing off the Perl script. Turns out they werent and I just figured there was no point in putting it back (is there?) if it wasn't hurting anything (is it?)

Thanks for all your help.

robJ
08-26-2004, 05:42 PM
If the names are strings with no spaces, you shouldn't need "quoted form of".

Glad to help. :-)

-- Rob