Folder actions

  • Thread starter Einar Magnús Boson
  • Start date
E

Einar Magnús Boson

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?

Thank you!
einarmagnus
 
M

Matthew Moss

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.
 
E

Einar Magnús Boson

There is RubyOSA (rubyosa.rubyforge.org) which can do a lot of =20
Applescript-type scripting tasks from Ruby. (It's a Ruby/OSA =20
bridge.) However, I don't believe it (or anything similar) is a full =20=
OSA component, which would be required for certain advanced =20
features, including Folder Actions.

Folder Actions seems rather persnickety, but here is what I did to =20
get 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, =20
and 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 =20
fix the path). Also, "do shell script" returns the output of the =20
script, which I conveniently display via "display alert". This line =20=
you'll want to change a bit to better suit your needs; this is just =20=
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 =20
the AppleScript (and eventually displayed by "display alert" above).

One thing to keep in mind. Folder Actions is pretty stupid about =20
where the folder action script lives. trampoline.scpt above MUST =20
live in ~/Library/Scripts/Folder Action Scripts, otherwise you can =20
attach it (seemingly successfully) all you want, but nothing will =20
happen. (Can also, I suspect, live in /Library/Scripts/Folder Action =20=
Scripts).

The Ruby script should live wherever trampoline.scpt specifies (in =20
my example, ~/Desktop/show_args.rb).

I'm sure there are better ways this stuff could be done... but I'm =20
no AppleScript expert.


Thank you!
Be there better ways or not, it works! That is all I care about :)

I am generally very impressed with Apple, but applescript...

/einarmagnus - will look into rubyOSA
 
L

Luc Heinrich

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.

What you want is launchd. Googling for 'launchd WatchPaths' will give =20=

you a bunch of tutorials and more about launching whatever you want on =20=

folder modifications. A bit more hardcore, but infinitely more =20
flexible. And it's not AppleScript ;)

--=20
Luc Heinrich - (e-mail address removed)
 
M

Matthew Moss

What you want is launchd. Googling for 'launchd WatchPaths' will =20
give you a bunch of tutorials and more about launching whatever you =20=
want on folder modifications. A bit more hardcore, but infinitely =20
more flexible. And it's not AppleScript ;)

Very cool, I like.
Yes, much better than my AppleScript hack.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top