On Dec 10, 2008, at 6:20 PM, Einar Magnús Boson wrote:
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.