APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

Shell vs. Applescript - getting input

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:

  mv xyz{,.fpbf}
 
 

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.





(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> -> Shell vs. Applescript - getting input



Increase ad revenue 50-250% with Ezoic


More Articles by

Find me on Google+

© Tony Lawrence



Kerio Samepage


Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us