Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Use folder action scripts on a shell scripts folder UNIX
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.]
    •    
  • Currently 1.67 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (3 votes cast)
 
[13,783 views]  

Use folder action scripts on a shell scripts folder | 4 comments | Create New Account
Click here to return to the 'Use folder action scripts on a shell scripts folder' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Use folder action scripts on a shell scripts folder
Authored by: ngb on May 03, '04 12:52:46PM
In the interest of universality, use $HOME to refer to your home directory. This way, you can copy your .bash_profile from user to user and machine to machine. The Path assignment line now looks like this:
export PATH="${PATH}:$HOME/bin"


[ Reply to This | # ]
Use folder action scripts on a shell scripts folder
Authored by: jakacmar on May 03, '04 01:41:37PM
AS Tip: When you're using a repeat that is counting the items in a list as is done in the script in the hint, it's much easier to use "repeat with x in y" then to use "repeat with x from 1 to count of items of y". For instance, the script in the hint could look like:
on adding folder items to this_folder after receiving these_items
	tell application "Finder"
		repeat with this_item in these_items
			set modification date of this_item to (current date)
			set xtension to name extension this_item
			if xtension is not "" then
				set extension hidden of this_item to true
				set the name of this_item 
					to (the displayed name of this_item)
			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 replaces "repeat with i from 1 to count of items of these_items" with "repeat with this_item in these_items". While functionally the same, I find that using this format is much quicker to write. It takes more work to have to type (item i of these_items) than to just type "this_item". Just a thought.

[ Reply to This | # ]
Link to download these "wicked cool" scripts.
Authored by: nwfrg on May 03, '04 09:32:13PM
For those who don't fancy a lot of typing you can learn plenty just from reading the scripts. So if you'd rather download than retype here's the link: http://www.intuitive.com/wicked/wicked-cool-shell-script-library.shtml

[ Reply to This | # ]
Use folder action scripts on a shell scripts folder
Authored by: Delarue on May 04, '04 05:15:03AM
Of course the path settings given above work only if you use the bash.
Tcsh users add the line

setenv PATH ${PATH}:$HOME/bin
in ~/.cshrc

- Julian

[ Reply to This | # ]