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

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:

AppleScript comments FAQ: How do I create comments in AppleScript?

Answer: There are two ways to create comments in AppleScript, and here are examples of both approaches.

AppleScript comments with "--" or "#" syntax

First, you can use the "--" syntax. This lets you create a comment like this at the beginning of a line:

-- my comment
display dialog "yada"

You can also use the same syntax to put a comment at the end of a line, like this:

AppleScript clipboard FAQ: Can you demonstrate an AppleScript clipboard example, such as displaying the Mac OS X clipboard contents in an AppleScript dialog?

One of the crazy things about AppleScript is how easy it is to get the contents of the clipboard, and then display them in a dialog. Here's an AppleScript clipboard dialog example that does just that:

display dialog (the clipboard)

Yep, that's it, just one line of text. Put this AppleScript code in your own ScriptEditor and run it, and you'll see it display the clipboard contents.

AppleScript string FAQ: How do I concatenate (merge) strings in AppleScript?

Fortunately string concatenation in AppleScript is pretty easy (if not a little different). To concatenate strings in AppleScript just use the ampersand (&) operator.

Here are a few AppleScript string concatenation examples, with a dialog thrown in so you can see the result:

A question I get frequently is "How do I assign some text (or a string) to a variable in AppleScript?" The syntax for this is as follows:

AppleScript dialog text FAQ: "How can I display an AppleScript dialog showing multiple lines of text?"

This is actually surprisingly easy, and there are a couple of ways to do it. Here are a few examples:

display dialog "Line 1
Line 2
Line 3"

Running this program displays the following AppleScript text dialog:

An AppleScript dialog that displays several lines of text.

Here's another way to accomplish the same thing:

AppleScript dialog FAQ: How can I display an AppleScript dialog with a textfield (text field)?

A frequent AppleScript question is "How do I prompt a user to enter some text?" Here's how you display an AppleScript dialog to prompt a user to enter a simple piece of information, in this case their name:

display dialog "What is your name?" default answer ""

Running this AppleScript dialog code results in the following dialog:

Want to get started writing your own AppleScript programs? If so, just open the Applications folder, then the AppleScript folder, and then double-click the ScriptEditor. This is the application you use to edit your AppleScript programs.

As a quick little test, once you have the ScriptEditor open, type this text into the editor:

display dialog "Hello, world"

Then press the Run button. You should see the text "Hello, world" displayed in a dialog, with Cancel and OK buttons, as shown in the following figure:

I haven't used it yet, but AE Monitor looks like it might be a good tool to see the Apple Events that are firing on your Mac OS X system. As I get more into Mac-specific programming (using Cocoa, Xcode, and AppleScript) I just wanted to make a note to remember this.