Alvin Alexander | Java, Scala, Unix, Perl, Mac OS X

Once you know that you can run an AppleScript program from the Unix command line it's easy to run it at scheduled intervals using the Unix cron facility. Here's an example of how to run my Safari script at the 7:30 a.m. every day.

30 7 * * * /Users/al/tmp/OpenUrls.scpt

I'm not going to get into the cron facility in any detail here, I just wanted to show how this is done. I will say that I modify the cron script by using this command:

AppleScript application FAQ: How do I save an AppleScript script as an AppleScript application?

I just finished writing an AppleScript program to open multiple URLs in multiple tabs in Safari, and -- being incredibly lazy -- I want to put this script on my desktop and have it run whenever I double-click it. That's all the work I'm willing to put into it, no other approach will do.

Well, I set out to write a simple tip on how to activate a Mac application using AppleScript, and I ended up writing a program to open a list of URLs in separate tabs in Safari using AppleScript. (Yeesh, I really took a detour. Oh, well.)

Without any further ado, here's the source code for this AppleScript program:

AppleScript “list” FAQ: How do I do something for every item in an AppleScript list? (or, How do I loop over an AppleScript list?)

Loop over an AppleScript list with the every keyword

I've shown examples of the AppleScript every keyword in other posts, so rather than create anything new, here's a repeat of one of those examples:

A quick note about getting the current time in AppleScript in two different formats. This command

set t to (time string of (current date))

yields something like this result (depending on the time of day):

"12:48:35 PM"

but this command

set t to (time of (current date))

yields something like this result:


(That's the number of seconds since midnight, in case you were wondering.)

AppleScript command FAQ: Can you show an example of the AppleScript delay command?

If your AppleScript is running too fast you can slow it down with the delay command, like this example:

display dialog 1 buttons {"OK"}
delay 3
display dialog 2

When you click OK after the first dialog, your AppleScript program goes to sleep for three seconds, then displays the second dialog.

Hopefully you'll have more practical uses for this AppleScript sleep/delay command my sample code. :)

AppleScript FAQ: How can I customize AppleScript dialog boxes?

When you display an AppleScript dialog box with the display dialog code, you don't have to just use the standard Cancel and OK buttons, you can change the text to something more meaningful to your specific prompt. For instance, suppose you want to ask someone their age range:

display dialog "How old are you?" buttons {"Less than 1", "Older than 1"}

(Okay, not the greatest example in the world, but that's all I've got today.)

AppleScript while loop FAQ: Can you share some examples of the AppleScript while loop syntax (the AppleScript repeat while syntax)?

Instead of using a keyword like "while" to create loops, AppleScript uses the "repeat" keyword to create loops. There are several versions of this syntax, as follows.

AppleScript repeat syntax: Repeat X times

Here's how to repeat an AppleScript command X number of times:

I've shown some basic AppleScript boolean-oriented syntax like this in another post:

set a to true
if a then
  display dialog a
end if

That code doesn't do too much, especially because you know the variable a is set to true, but it becomes a little more helpful when you don't know if a is true or false:

if a then
  -- do something really important here
end if

You can perform other if/then checks based on numerical tests, like this:

It's easy to work with boolean (true/false) variables in AppleScript. Here's an example of how you assign a boolean to a variable:

Variable names in AppleScript are pretty flexible, especially when compared to other languages. Variable names can consist of letters, numbers, and underscores, with the only limits being (1) the first character in the name can't be a number, and (2) the name itself must be less than about 250 characters (I don't know the exact limit, but you really don't want variable names anywhere near that length limit).

In the latest release of Dr. Dobbs, Michael Swaine reports that "after two years trying to make Ruby on Rails do what he wanted, Derek Sivers went back to PHP and finished the job in two months." Actually, here's a direct link to Derek Sivers' post, including his seven reasons he switched back from Rails to PHP, and what he learned from Rails.


I was reading Dr. Dobbs last night, and they referenced this story about how quantity ends up producing quality, at least when it comes to humans creating things.

The lesson goes like this: A ceramics teacher divides a class into two teams, then tells one team they'll be graded by sheer quantity, while the other side has to produce one great pot. In the end the Quantity Team delivers all the best quality pots.

I've found this to be true with new programmers. All newbies want to learn about things like design patterns, but what they really need to do is just sit down and write a lot of code.

This reminds me of the quote, "One learns by doing the thing." I don't know for sure who said that — possibly Sophocles — but I remember reading it in a Thermodynamics book in college, and I've never forgotten it.

AppleScript list FAQ: How do I create lists in AppleScript?

There are several different ways to create AppleScript lists. Let's take a look at a few examples.

AppleScript list creation examples

Here's a quick example of a few different AppleScript lists. First, some favorite foods:

set favoriteFoods to {"cookies", "cake", "cereal"}

Next, some of those other foods:

AppleScript subroutine FAQ: How do I create an AppleScript subroutine (or AppleScript function)?

Creating a subroutine in AppleScript is pretty easy. Jumping right in, here's an example of an AppleScript subroutine that adds one to whatever you pass into it:

AppleScript math FAQ: Can you show some examples of basic AppleScript math operations, like addition, subtraction, multiplication, and division?

Sure, let's take a look at some AppleScript math examples.

AppleScript addition syntax/examples

To add a few numbers together just use the normal set syntax and the + operator, like this AppleScript addition example:

set one to 1
set two to 2
set answer to one + two
display dialog answer

That example displays the number 3.

AppleScript current date FAQ: "How do I get the current date?", or "How do I get today's date?"

The short answer is just to use AppleScript code like this: (current date). Here's an example of how to use this:

display dialog (current date) as string

This AppleScript example gets the current date, and then displays it in a dialog.



To display a number in a dialog using AppleScript, just treat it like a string and you'll be fine. Here's a direct example:

display dialog 4.79

And here's an example using a variable:

set myNum to 4.88
display dialog myNum

If you really dig into AppleScript programming you're eventually going to need to learn what methods you can call on Mac applications. The way you do this is to dig into the ScriptEditor Dictionary. To open the Dictionary, click the File menu, then choose the Open Dictionary menu item. This brings up the following dialog:

A frequent AppleScript question I get is "How do I get information back from a user after I've prompted them with a dialog?" The following example demonstrates how I typically do this. I prompt the user to enter some text, then get their reply back. In this case the reply is stored in the variable named theName.

set theName to the text returned of 
  (display dialog "What is your name?" default answer "")

For your reference, the dialog created by this code looks like this: