# Folder actions

**URL:** <https://www.ruby-forum.com/t/folder-actions/155361>\
**Category:** Ruby\
**Created:** [December 11, 2008, 1:27am UTC](https://www.ruby-forum.com/t/folder-actions/155361 "2008-12-11T01:27:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Einar\_MagnSSSSs\_Bos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/a5b964/32.png) [@Einar\_MagnSSSSs\_Bos](https://www.ruby-forum.com/u/Einar_MagnSSSSs_Bos)\
**Post date:** [December 11, 2008, 1:27am UTC](https://www.ruby-forum.com/t/folder-actions/155361/1 "2008-12-11T01:27:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Einar\_MagnSSSSs\_Bos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/a5b964/32.png) [@Einar\_MagnSSSSs\_Bos](https://www.ruby-forum.com/u/Einar_MagnSSSSs_Bos)\
**Post date:** [December 11, 2008, 4:35am UTC](https://www.ruby-forum.com/t/folder-actions/155361/2 "2008-12-11T04:35:59Z")

</div>

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](http://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.

---

<div class="post-metadata">

**Author:** ![Einar\_MagnSSSSs\_Bos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/a5b964/32.png) [@Einar\_MagnSSSSs\_Bos](https://www.ruby-forum.com/u/Einar_MagnSSSSs_Bos)\
**Post date:** [December 11, 2008, 5:51am UTC](https://www.ruby-forum.com/t/folder-actions/155361/3 "2008-12-11T05:51:26Z")

</div>

On 11.12.2008, at 03:28 , Matthew M. wrote:

> > 
> 
> Applescript-type scripting tasks from Ruby. (It’s a Ruby/OSA  
> on adding folder items to theFolder after receiving theItems  
> string (adding single quotes around each item in case they contain  
> My ruby script was pretty simple, just for testing:  
> where the folder action script lives. trampoline.scpt above MUST

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

---

<div class="post-metadata">

**Author:** ![Einar\_MagnSSSSs\_Bos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/a5b964/32.png) [@Einar\_MagnSSSSs\_Bos](https://www.ruby-forum.com/u/Einar_MagnSSSSs_Bos)\
**Post date:** [December 11, 2008, 10:56am UTC](https://www.ruby-forum.com/t/folder-actions/155361/4 "2008-12-11T10:56:37Z")

</div>

On 11 déc. 08, at 01:20, Einar Magnús Boson wrote:

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

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

---

<div class="post-metadata">

**Author:** ![Einar\_MagnSSSSs\_Bos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/a5b964/32.png) [@Einar\_MagnSSSSs\_Bos](https://www.ruby-forum.com/u/Einar_MagnSSSSs_Bos)\
**Post date:** [December 11, 2008, 3:35pm UTC](https://www.ruby-forum.com/t/folder-actions/155361/5 "2008-12-11T15:35:49Z")

</div>

On Dec 11, 2008, at 3:48 AM, Luc H. wrote:

> more flexible. And it’s not AppleScript 😉  
> Very cool, I like.  
> Yes, much better than my AppleScript hack.
