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 # etcYou 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/myscriptor if you open a new Terminal window, it defaults to /Users/yourname, so you can just type:
./Desktop/myscriptAppleScript 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/scriptor
/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 $theVariableOther 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 commentIn Perl and shell scripts, a comment begins with
#.# This is a comment- Arrays (or
Lists) -
In AppleScript, an array is called a
Listand is created like this:set theList to { "item1", "item2", "item3"}Access an item of the list like so:
display dialog item 1 of theListGet the number of items like so:
set x to count of every item of theListLoop with the list like this:
repeat with i in theList display dialog i end repeatIn 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-thenCondition Blocks-
In AppleScript, an
if-thenblock 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 ifIn 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), orne(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 fiIn 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 tellIn 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 " & theVariableIn 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
-
Login Hook
2010-07-23 14:50:30 pditech1 [View]
-
Google via Terminal (user input problem)
2007-05-19 15:29:36 paymon [View]
-
Google via Terminal (user input problem)
2007-05-21 09:27:50 jamesreynolds [View]
-
Google via Terminal (user input problem)
2007-05-19 15:29:25 paymon [View]
-
Scripting Internet Connect
2005-03-07 08:03:21 Larriestyle [View]
-
Scripting Internet Connect
2005-03-07 08:50:16 jamesreynolds [View]
-
Making and running scripts
2004-10-01 15:04:19 yush [View]
-
Making and running scripts
2004-10-01 15:29:37 jamesreynolds [View]
-
Making and running scripts
2004-10-01 15:30:59 jamesreynolds [View]
- Trackback from http://chronicles.risckybabble.org/2004/05/03/bookmark_cleanup_pt_3/index.php
Bookmark clean-up, Pt. 3
2004-05-03 14:34:26 [View]
-
loginhook
2004-03-04 10:10:02 yantheman [View]
-
loginhook
2004-03-04 13:26:16 jamesreynolds [View]
-
Tk Aqua
2004-01-05 10:21:17 anonymous2 [View]
- Trackback from http://itc.uncc.edu/ladon/weblog/archives/002552.html
Learn Scripting On Mac OS X
2003-11-17 14:00:34 [View]
-
language comparisons
2003-11-15 01:49:14 anonymous2 [View]
-
more difficult defaults
2003-11-14 12:17:27 eoligarry [View]
-
more difficult defaults
2003-11-14 13:01:50 jamesreynolds [View]
-
more difficult defaults
2004-03-04 14:49:26 yantheman [View]
-
Also, take a look at Tcl
2003-11-13 09:16:21 anonymous2 [View]
-
Defaults Tips & Tricks
2003-11-10 15:49:22 anonymous2 [View]
-
What about the alternatives?
2003-11-10 00:30:58 brianl [View]
-
What about the alternatives?
2003-11-10 11:27:35 anonymous2 [View]
-
What about the alternatives?
2003-11-10 13:35:42 jamesreynolds [View]
-
Getting Back
2003-11-09 16:07:24 johnmcadams1 [View]
-
Getting Back
2003-11-10 11:30:26 anonymous2 [View]
-
end-of-line character and sudo
2003-11-08 08:28:48 hayne [View]

