Passing AppleScript Folder Action arguments to Perl

brian_d_foy on 2004-06-12T23:54:34

I want to log the invocation of a Folder Action I have set up on my Powerbook. It is supposed to import into iPhoto any photos I add to the folder, but something else is triggering it, and often when the folder seemingly no files in it. For instance, it always runs when the computer wakes up.

Since this action happens in the background, it is really hard to debug, even with Script Debugger. I could just chuck AppleScript all together, but I still have to solve the same problem: pass along the arguments to perl.

I have many forms of the arguments to choose from, but the POSIX path is good enough for me. Once I get that path, I jump through all sorts of hoops to split and join stuff so I can pass them to perl and have each path show up as its own entry in @ARGV.

on adding folder items to this_folder after receiving these_items
	set args to {posix_path(this_folder)}
	repeat with this_item in these_items
		set args to args & posix_path(this_item)
	end repeat
	
	do shell script "/Users/brian/show_args" & " " & join_args(args)
end adding folder items to

on posix_path(this_alias) tell application "Finder" set this_path to POSIX path of this_alias end tell return replace_chars(this_path, " ", "\\ ") end posix_path

on join_args(this_list) set old_delimiter to AppleScript's text item delimiters set AppleScript's text item delimiters to " " set this_text to this_list as string set AppleScript's text item delimiters to old_delimiter return this_text end join_args

on replace_chars(this_text, search_string, replacement_string) set old_delimiter to AppleScript's text item delimiters set AppleScript's text item delimiters to the search_string set the item_list to every text item of this_text set AppleScript's text item delimiters to the replacement_string set this_text to the item_list as string set AppleScript's text item delimiters to old_delimiter return this_text end replace_chars


The script itself is boring:

#!/usr/bin/perl

$" = "\n";

open my($fh), ">> /Users/brian/show_args.txt"; print $fh "-" x 73, "\n[", scalar localtime, "]\n@ARGV\n";


However, I can monitor the Folder Action arguments now:

-------------------------------------------------------------------------
[Sat Jun 12 16:23:27 2004]
/Users/brian/Desktop/Test Folder/
/Users/brian/Desktop/Test Folder/To Do.txt
-------------------------------------------------------------------------
[Sat Jun 12 16:23:28 2004]
/Users/brian/Desktop/Test Folder/
/Users/brian/Desktop/Test Folder/npr7500.smil
-------------------------------------------------------------------------
[Sat Jun 12 16:46:49 2004]
/Users/brian/Desktop/Test Folder/
/Users/brian/Desktop/Test Folder/Holidays on Ice 3.mp3


Now I just have to let this thing sit for a while and see what happens.