Simple Scripts

Comments

When you are writing your scripts, you will want to leave comments for yourself. You can preface any line with two dashes and that line will be ignored by AppleScript. You can write whatever you want on that line.

You can surround any set of lines with an open parentheses and two asterisks (close with two asterisks and a close parentheses) to cause AppleScript to ignore all of those lines.

(**

This is a comment.

None of this is AppleScript

**)

make new document

--close all of the windows we’ve opened

close all windows

Dictionaries

A useful feature of Script Editor is the ability to look at the “dictionary” of scriptable applications. If you look at the dictionary for Netscape, Word, or Explorer, you can see what things those programs can do.

Change the volume level on your computer

You can download “scripting additions” that extend the functionality of AppleScript. For example, many computers use “Jon’s Commands”. There are also scripting additions that come with AppleScript by default. The “Standard Additions” include a command to “set volume”. If you have other people using your computer, and you always want to have the sound at the right level, you can place an AppleScript application in your Startup Folder that does:

set volume 3

for example. Every time your computer starts up, it will run that script, and when that script runs it will set the sound volume to an average level. (You can change it to any number from 0 to 7.)

Open Four News Sites

We can do the four news sites by hand as well. Type the following script into your Script Editor and save it as “Open News II”:

tell application "Netscape Communicatorª"

activate

--assume 1024 by 768 screen, start at 10, 50

--open CNN news in upper left

make new document

GetURL "www.cnn.com" inside window 1

set bounds of window 1 to {10, 50, 500, 390}

--open Google news in upper right

make new document

GetURL "news.google.com" inside window 1

set bounds of window 1 to {501, 50, 1000, 390}

--open Yahoo news in lower left

make new document

GetURL "news.yahoo.com" inside window 1

set bounds of window 1 to {10, 390, 500, 768}

--open personal weblog

make new document

GetURL "cerebus.sandiego.edu/~jerry/blog/" inside window 1

set bounds of window 1 to {501, 390, 1000, 768}

end tell

You can edit the “bounds” of each window as you see fit.

Automatically Reload Web Pages

If you’ve paid attention to the “Save” screen for your scripts, there is an option which by default is unchecked called “Stay Open”. If you check this, your script will not quit when it is done. It will stay open, and can continue working.

AppleScripts work by sending and receiving “messages”. When we “told” Netscape to make new windows and open URLs, our script sent the appropriate “messages” from our script to Netscape.

If our script stays open, the system will also send it messages. Whenever an application “quits”, the system sends it a “quit” message. Our script gets that also (it automatically knows what to do when it receives that message: it quits). Another message we receive is the “idle” message. We get the “idle” message when we aren’t doing anything (or more specifically, when our script is not doing anything).

Add the following lines to your “Open News II”:

on idle

tell application "Netscape Communicatorª"

copy (window count) - 1 to windowCount

repeat with windowID from 1 to windowCount

tell window windowID

copy unique ID to myID

copy URL to myURL

OpenURL myURL toWindow myID

end tell

end repeat

end tell

return 60

end idle

This is what is called a “handler” in AppleScript. It “handles” messages. When you handle a message, you can send a return message. In this case, we’re telling the system that we’re done with our idle, and we would like to have another idle message in 60 seconds (“return 60”). By default, idle times are in 30-second increments, and it gets called immediately after the script first goes idle in order to set the idle time to a different default.

We are using the “copy” command of Applescript to keep track of things that are happening. Each window has a unique ID and a URL attached to it. We are copying that unique ID to a variable called “myID” and we are copying the URL to a variable called “myURL”. From this point on, whenever we want to use the URL of this window, we can refer to it simply as “myURL”.

Shell Tell

You’ll notice that we have two tells, one inside of the other. First, we tell Netscape to do something, and inside of that we are telling one of Netscape’s windows to do something.

Repetitive Tasks

One of the things you can use scripts for is to automate boring and repetitive tasks. One of the features that scripts such as AppleScript have to make it easy to automate repetitive tasks is the “repeat” structure. (Other scripting languages might call it “while”, “for”, or “foreach”, among other names.)

Everything between “repeat” and “end repeat” will be repeated until our instructions are done. We told it to repeat “with windowID from 1 to windowCount”. Within that repeat “block”, the variable “windowID” will start at 1 the first time through, and then increase by 1 each successive repetition, until it reaches the value of “windowCount”.

We could have done the same thing by typing those lines between the “repeat” four times, as we did when we opened the windows to load the web pages. But this makes it a lot easier. And it means that if we later add a fifth web page (we’ll need an awfully big monitor) we won’t need to change this part of the script at all.

Talking Clock

One of the features of the standard additions is the current date and the ability to speak out loud.

set volume 1

copy the (current date) to theDate

say theDate as string

This makes your computer quiet (so you don’t bother other people near where you are using this tutorial), and then it copies the current date into a “variable” called “theDate”. Finally, it says whatever is in the variable “theDate”. The “say” command can only say text. If you try to say something other than text, AppleScript tries to send that thing a message called “say” instead of sending that item to “say”. So you need to tell AppleScript to convert that thing to text first. This is why we put the “as string” after “say theDate”.

You can look at what it is speaking by adding “get theDate” to the end of your script and then looking at your window titled “the result” (you can pull down the Windows menu to see this listed).

What it is speaking is not particularly useful. It is reading the numbers without recognizing what they are. For example, it is reading 2002 as “twenty zero two” on the computer on which I’m writing this, and it is trying to speak the letters “AM” as a word “am”. We’ll need to help it along.

--be nice to our neighbors

set volume 1

on idle

--get the current date and time

copy the (current date) to theDate

--get the parts of the date and time that we want

copy the weekday of (theDate) as string to theDay

copy the time string of (theDate) to theTime

--get rid of the seconds from the time

set theTime to characters 1 thru 5 of theTime

say theDay & ", " & theTime

return 60

end idle

The “ampersand” or “&” character glues together two pieces of text. In this case, we’re putting our variable “theTime” after our variable “theDay” and putting a command and space between them.

Make sure you save your talking clock as an “Application” that will “Stay Open”.

Drag and Drop

When you drop a file onto a program, that program gets a message explaining what happened. You can let your AppleScripts accept those messages with the “open” handler. Make a script with nothing but:

on open (theItems)

repeat with anItem in theItems

tell application "Finder"

copy the name of item anItem to theName

end tell

display dialog theName

end repeat

end open

Save this as an Application called “Uploader”. Then, drop a few files on it. It should name each file in a dialog box.

You could use this feature to “tell” an FTP program such as Fetch or Interarchy to upload files automatically to your web site. When we do our upload, we’ll want to make sure we don’t upload things accidentally that we don’t want. Usually, we just want to upload images and text files (html files are usually text). Change your Uploader script to:

property allowedTypes : {"JPEG", "TEXT", "HTML"}

on open (theItems)

repeat with anItem in theItems

tell application "Finder"

copy the name of item anItem to theName

copy the file type of item anItem to theType

end tell

if theType is not in allowedTypes then

display dialog theType & " (" & theName & ") is not allowed for upload."

end if

end repeat

end open

We created a property (very much like a variable) that contains our allowed types. If the file is not an allowed type, we display a warning message. This allows you to add that type to the list of allowed types if you wish to.

Finally, we need to tell Fetch to upload the file. You may wish to look at Fetch’s dictionary.

property allowedTypes : {"JPEG", "TEXT", "HTML"}

--replace "username" with your username and "password" with your password

--replace "hostname" with the server where your files will be stored

--replace "path" with the path to your web directory, usually something like

-- /home/username/public_html

property destinationURL : "ftp://username:password@hostname/path"

on open (theItems)

repeat with anItem in theItems

tell application "Finder"

copy the name of item anItem to theName

copy the file type of item anItem to theType

end tell

if theType is in allowedTypes then

tell application "Fetch"

activate

put into url destinationURL item anItem

end tell

else

display dialog theType & " (" & theName & ") is not allowed for upload."

end if

end repeat

end open

We’re using an “if” structure in this script. The “if” structure performs everything between “if” and “end if” depending on certain conditions. Often, there will be one set of statements before an “else” for if the condition works out, and another set of statements after an “else” for if the condition does not work out.

If “theType” is in the list of “allowedTypes”, we work with Fetch. Otherwise, we inform the user that this is not an allowed type.

Other Things You Might Do

Simple alarm clock: ask for a message, a time, and then sleep to that time and speak that message.

Publish a Word document as RTF, HTML, and PDF--then automatically upload it using the other scripts.

Grab and write a random quote from a FileMaker database of quotes.

Combine multiple PDF files into a single file. (Requires Acrobat)