Of course there are things easily done with Applescript that aren't so easy with a shell script. However, the opposite is also true. Too often I see people writing complicated (and slow) Applescript for things that the shell can do very easily.
Let's take the trivial example of turning a folder into a burn folder. That's trivial; it's just renaming the file, which doesn't even require a script. If we want folder "xyz" to become a burn folder, we just:
mv xyz xyz.fpbf
Or, a tad more esoteric but just as good:
That last but relies on bash brace expansion and may not work if you aren't using bash.
But what if I don't want to remember the ".fpbf"? Now we start getting into scripting. A first cut might be this:
#!/bin/bash printf "What folder is to be a burn folder?" read folder mv $folder $folder.fpbf
Cool. But scripts can be given arguments, too, so we could just do:
mv $1 $1.fpbf
If you called the script "burnfolder", and remembered to "chmod 755 burnfolder" and put it somewhere like ~/bin, you could then just type "burnfolder xyz" and it would work.
But what if you want to be able to do it either way? That is, give it a directory, or have it ask if you don't? That's not hard:
folder=$1 while [ ! "$folder" ] do printf "What folder? " read folder done mv $folder $folder.fpbf
You can get quite a bit fancier. For example, you might want to test to be sure this really is a folder and not just an ordinary file. Again, not hard to do:
#!/bin/bash folder=$1 while [ ! "$folder" ] do printf "What folder? " read folder done if [ -d $folder ] then mv $folder $folder.fpbf else echo "$folder is not a folder" fi
Bash scripting isn't all that hard, is it?
Got something to add? Send me email.
More Articles by Tony Lawrence © 2009-11-07 Tony Lawrence