Hi all,
I need to have a script run every time a file is added to a folder =20
on OS X, I have found out that this can be done with applescript but =20=
I can't for the life of me figure out how to write anything that =20
works in that language. Weirdest thing I have ever come across. I've =20=
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 =20=
know of an example of how to get a ruby script running from =20
applescript with the name of the just added file as argument?
Does anyone know if apple is gonna support something more usable =20
than applescript as a scripting language in a future version of =20
their OS?
There is RubyOSA (rubyosa.rubyforge.org) which can do a lot of =20
Applescript-type scripting tasks from Ruby. (It's a Ruby/OSA bridge.) =20=
However, I don't believe it (or anything similar) is a full OSA =20
component, which would be required for certain advanced features, =20
including Folder Actions.
Folder Actions seems rather persnickety, but here is what I did to get =20=
a basic task working.
First, create an Applescript file, call it trampoline.scpt, and use =20
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 " & =20
args)
end adding folder items to
theItems is an AppleScript list of the files added to the folder, and =20=
the first four lines of the script converts it into one argument =20
string (adding single quotes around each item in case they contain =20
spaces, etc.)
The "do shell script" command is the part that launches your Ruby =20
script. (And this could use RubyOSA.) In my case, the ruby script is =20
sitting on my desktop (at the moment, but could be elsewhere, just fix =20=
the path). Also, "do shell script" returns the output of the script, =20
which I conveniently display via "display alert". This line you'll =20
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 =20
colon delimiters). As it is a test, I just join all the paths =20
together, one per line, and "puts" it, which gets returned back to the =20=
AppleScript (and eventually displayed by "display alert" above).
One thing to keep in mind. Folder Actions is pretty stupid about where =20=
the folder action script lives. trampoline.scpt above MUST live in ~/=20
Library/Scripts/Folder Action Scripts, otherwise you can attach it =20
(seemingly successfully) all you want, but nothing will happen. (Can =20
also, I suspect, live in /Library/Scripts/Folder Action Scripts).
The Ruby script should live wherever trampoline.scpt specifies (in my =20=
example, ~/Desktop/show_args.rb).
I'm sure there are better ways this stuff could be done... but I'm no =20=
AppleScript expert.