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

Shell vs. Applescript - getting input

© December 2005 Tony Lawrence

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:

 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:

 while [ ! "$folder" ]
 printf "What folder? "
 read folder
 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:

 while [ ! "$folder" ]
 printf "What folder? "
 read folder
 if [ -d $folder ]
 mv $folder $folder.fpbf
 echo "$folder is not a folder"

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

Inexpensive and informative Apple related e-books:

Take Control of OS X Server

Take Control of Upgrading to El Capitan

Take control of Apple TV, Second Edition

Are Your Bits Flipped?

Take Control of Parallels Desktop 12

More Articles by © Tony Lawrence

Printer Friendly Version

Have you tried Searching this site?

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

Printer Friendly Version

Once you eliminate the impossible, whatever remains, no matter how improbable, must be the truth. (Arthur Conan Doyle)

Linux posts

Troubleshooting posts

This post tagged:


Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode

SCO Unix Sales, Support, & Service

Phone:  707-SCO-UNIX (707-726-8649Toll Free: 833-SCO-UNIX (833-726-8649)