Use folder action scripts on a shell scripts folder

May 03, '04 09:04:00AM

Contributed by: Anonymous

Recently I purchase a book called "Wicked Cool Shell Scripts" by Dave Taylor, and it is full of exactly that, cool shell scripts. Having not done a whole lot of shell scripting before, I thought it would be good to pick it up see what interesting stuff I could do with it.

Having copied out a couple of the scripts from the book (sure, you can download them, but what do you learn from that?), I became a little annoyed with having to write sh /path/to/your/script.sh whenever I wanted to run the script. I quickly found out that you can add folders to your $PATH by editing the .bash_login in your home directory. Go into the terminal, type

pico .bash_login
and add the following line:
 export PATH="${PATH}:/Users/patrick/bin"
But replace the /Users/patrick/bin portion with the path to wherever you store your scripts. Hit Control-O and return to save it, then Control-X to exit pico. You can now simply type script_name.sh at the command line and that script will run, as long as it is in your specified scripts folder, whatever you made it in the above step.

This is good, but keep in mind that if you have scripts that refer to other scripts (as there are many in "Wicked Cool Shell Scripts") then they will not work unless you specify execution permissions for them. This is done with a the simple chmod 755 script_name.sh command. Also, to make it easier you use in the command line, you would probably want remove the ".sh" file extension by using a mv script_name.sh script_name command as well. These commands are all fairly simple, but if you are dealing with a lot of scripts, be it making them or downloading them, it can be very time consuming and/or annoying. Therefore, I have simplified the whole process with an AppleScript, and a little shell script of my own. Here it is:

on adding folder items to this_folder after receiving these_items
  tell application "Finder"
    repeat with I from 1 to number of items in these_items
      set modification date of item I of these_items ¬
       to (current date)
      set xtension to name extension of item I of these_items
      if xtension is not "" then
        set extension hidden of item I of these_items to true
        set the name of item I of these_items ¬
         to (the displayed name of item I of these_items)
      end if
      do shell script "cd ~/bin; chmod 755 $(ls -1t | head -n 1)"
    end repeat
  end tell
end adding folder items to
This script is used by attaching it to a folder using folder actions (control-click on the folder in the Finder). Also be sure to change the ~/bin part to whatever directory you are using for your shell scripts. Essentially, any file(s) (such as your shell scripts) dropped into the folder are given the appropriate permissions and have the ".sh" (or any other file extension, careful with this!) removed, allowing you to use your scripts very easily in the command line. I found this very useful when playing with the many other shell scripts in the book.

If anyone has anything to append, alter, or otherwise suggest, please leave some comments. I hope this helps some people with scripts.

[robg adds: I'm sure there will be comments on this one; the intent is to show what's possible with a fairly simple AppleScript and folder actions. Be careful with scripts like this one which affect everything that gets added to a directory; you may not want the name changed and/or execute permissions added to everything in the folder.]

Comments (4)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20040426213240310