From: Matthew Moss Date: 2008-12-11T12:28:23+09:00 Subject: Re: Folder actions On Dec 10, 2008, at 6:20 PM, Einar Magnús Boson wrote: > Hi all, > I need to have a script run every time a file is added to a folder > on OS X, I have found out that this can be done with applescript but > I can't for the life of me figure out how to write anything that > works in that language. Weirdest thing I have ever come across. I've > seen people refer to it as a "read only language" and I tend to agree. > > Is there any way I can attach a ruby script instead? Or does anyone > know of an example of how to get a ruby script running from > applescript with the name of the just added file as argument? > > Does anyone know if apple is gonna support something more usable > than applescript as a scripting language in a future version of > their OS? There is RubyOSA (rubyosa.rubyforge.org) which can do a lot of Applescript-type scripting tasks from Ruby. (It's a Ruby/OSA bridge.) However, I don't believe it (or anything similar) is a full OSA component, which would be required for certain advanced features, including Folder Actions. Folder Actions seems rather persnickety, but here is what I did to get a basic task working. First, create an Applescript file, call it trampoline.scpt, and use this code: on adding folder items to theFolder after receiving theItems set args to "" repeat with anItem in theItems set args to args & " '" & anItem & "'" end repeat display alert (do shell script "~/Desktop/show_args.rb " & args) end adding folder items to theItems is an AppleScript list of the files added to the folder, and the first four lines of the script converts it into one argument string (adding single quotes around each item in case they contain spaces, etc.) The "do shell script" command is the part that launches your Ruby script. (And this could use RubyOSA.) In my case, the ruby script is sitting on my desktop (at the moment, but could be elsewhere, just fix the path). Also, "do shell script" returns the output of the script, which I conveniently display via "display alert". This line you'll want to change a bit to better suit your needs; this is just an example. My ruby script was pretty simple, just for testing: #!/usr/bin/env ruby puts ARGV.map { |s| s.tr(':','/') }.join("\n") The call to 'tr' fixes the path, as it arrives old-Mac style (with colon delimiters). As it is a test, I just join all the paths together, one per line, and "puts" it, which gets returned back to the AppleScript (and eventually displayed by "display alert" above). One thing to keep in mind. Folder Actions is pretty stupid about where the folder action script lives. trampoline.scpt above MUST live in ~/ Library/Scripts/Folder Action Scripts, otherwise you can attach it (seemingly successfully) all you want, but nothing will happen. (Can also, I suspect, live in /Library/Scripts/Folder Action Scripts). The Ruby script should live wherever trampoline.scpt specifies (in my example, ~/Desktop/show_args.rb). I'm sure there are better ways this stuff could be done... but I'm no AppleScript expert.