macdevcenter.com
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Scripting Mac OS X
Pages: 1, 2, 3

Appendix: List of Common Administrative Commands for Shell, Perl, and AppleScript, Listed Side-by-Side

Creating an Executable Script

To create an executable AppleScript, you must open /Applications/AppleScript/Script Editor, write the script that passes the Check Syntax operation, then select File->Save As... and change its format to Application. You also want to check Never Show Startup Screen.

To create a Perl script, create a new plain text file and on the first line put the "shebang" (#!) and then the path to the Perl interpreter, like so:

#!/usr/bin/perl

# code goes here
# etc

You must also make sure it has the execute permission set.

To create a shell script, make it just like you would a Perl script, except replace the path to the Perl executable with the path to the shell-script language you will use. For example:

#!/bin/sh

# code goes here
# etc
Executing a Script

Perl, shell, and AppleScripts can all be launched in the Terminal by typing in the path to the script, either a full path or a relative path. For example, if your script is on your Desktop:

/Users/yourname/Desktop/myscript

or if you open a new Terminal window, it defaults to /Users/yourname, so you can just type:

./Desktop/myscript

AppleScript files saved as Application format can also be double-clicked. Perl and shell scripts cannot be double clicked, unless you add .command to the end of the script. However, a script that is opened in this way will open the Terminal, and then execute in a new Terminal window.

There are third party utilities such as DropScript, Pashua, and Platypus that will turn a script into an application.

Perl and shell scripts that do not have execute permissions can also be executed in the Terminal by typing the path to the Perl or shell interpreter, and then the path to the script. For example, type this in the Terminal:

/bin/sh /path/to/script

or

/usr/bin/perl /path/to/script
Creating Variables

In AppleScript:

set theVariable to "something"

In Perl:

$theVariable = "something";

In Perl, variables always start with $ when you create them and when you access the contents.

In a BASH shell script, create a variable using the variable name, without a $, and with no spaces after the =, like this:

theVariable="something"

To get at the contents of a BASH variable, you must place a $ before the variable, like this:

echo $theVariable

Other script environments, such as TCSH, set variables differently. TCSH is as follows:

set theVariable="something"
echo $theVariable
Comments

In AppleScript, a comment begins with --.

-- this is a comment

In Perl and shell scripts, a comment begins with #.

# This is a comment
Arrays (or Lists)

In AppleScript, an array is called a List and is created like this:

set theList to { "item1", "item2", "item3"}

Access an item of the list like so:

display dialog item 1 of theList

Get the number of items like so:

set x to count of every item of theList

Loop with the list like this:

repeat with i in theList
	display dialog i
end repeat

In Perl, you create a list like this:

@theArray = ("item1", "item2", "item3");

Access an item of the array like so:

print $theArray[0];

To get the number of items in the array, use this:

$number_of_items = @theArray;

Loop with the list like this:

foreach $i (@theArray) {
   print "$i\n";
}

In a BASH shell script, create an array like so:

theArray=(item1 item2 item3)

Print out the first item like so:

echo "${theArray[0]}"

Get the number of items like so:

echo "${#theArray[@]}"

Loop with the list like this:

for i in ${theArray[@]}
do
   echo $i
done
if-then Condition Blocks

In AppleScript, an if-then block looks like this:

set something to "blah"
if (something is equal to "bla") then
   display dialog "something is bla"
else if (something is equal to "blah") then
   display dialog "something is blah"
else
   display dialog "something is not bla or blah"
end if

In Perl, it looks like this:

$something = "blah";
if ($something eq "bla") {
   print "something is bla\n";
} elsif ($something eq "blah") {
   print "something is blah\n";
} else {
   print "something is not bla or blah\n";
}

Also, in Perl, text comparisons are done with eq (equal), or ne (not equal). Number comparisons are done with == (equal to), != (not equal to), etc.

In BASH, it looks like this:

something="blah"
if [ "$something" = "bla" ]; then
   echo "something is bla"
else
   if [ "$something" = "blah" ]; then
     echo "something is blah"
   else
     echo "something is not bla or blah"
   fi
fi

In Perl, spaces in condition statements don't always matter. In BASH, you must have the spaces around the brackets and the equal sign.

Check to See if a File Exists

In AppleScript, to check to see if a file exists, you do this:

tell application "Finder"
   if (file "Hard Disk:path:to:file" exists) then
     -- do something
   end if
end tell

In Perl, you do this:

if ( -f "/path/to/file" ) {
   # do something
}

In BASH, you do this:

if [ -f "/path/to/file" ]; then
   # do something
fi
Combining Strings

In AppleScript, you combine strings with &:

set theVariable to "c"
set x to "a " & "b " & theVariable

In Perl, you do it with a period:

$theVariable = "c";
$x = "a " . "b " . $theVariable;

If you use double quotes, you can place variables straight in the quotes and it will be replaced with the variable value, like this:

$x = "a b $theVariable";

or, to put text at the end of the variable:

$x = "a b ${theVariable}d";

If you really want a $, you have to escape it:

$x = "a b \$theVariable";

Or use a single quote:

$x = 'this that $theVariable';

You have to be careful with special characters in a double quote. If you are worried about using special characters, you can either use a single quote or just test escaping it.

# this is a test
$x = "test: &@!$?";
print $x;

In BASH, you combine variables like so:

variable1="a "
variable2="b "
variable3=${variable1}${variable2}c
echo ${variable3}

James Reynolds is a member of the University of Utah's Student Computing Labs Mac Group. His main duty is the deployment of Mac OS X. Most of his responsibilities include the OS customizations, scripts, and security of the Mac OS X lab and kiosk computers supported by SCL.


Return to the Mac DevCenter