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

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.

 

The Mac OS X Activity Monitor is a cool utility. As shown in the following figure, the main screen shows all the processes running on your system, the user that owns the process, the percent of CPU it's using, the virtual memory it's using, and more. My most common thing here is to sort by memory or CPU use to get a general idea of what's going on.

I generally use Spotlight when searching my Mac for a file, but there are times it doesn't work, especially when I'm trying to find a file that contains a phrase I know. For instance, I may have a file named "Fred.txt", and it contains the phrase "foo bar", but when I open Spotlight and type in "foo bar", the file Fred.txt never shows up.

One of the cool things I just discovered on my Mac is the slideshow capability of the Finder. This can be a great tool for previewing a collection of images, or just to rotate images on your screen just for fun. Just highlight a few images, right-click, and choose the Slideshow option.

The DigitalColor Meter application is a cool little utility that comes free with Mac OS X. It has one simple purpose: to let you determine the color of a pixel of an image. Actually, it's a bit more than that, but that's what I use it for.

I haven't had the problem of a frozen application on my Mac OS X laptop in a long time, but when it does I can never remember the keystroke combination to bring up the Force Quit dialog. The key combination is [Option][Command][Esc], which I know at this moment because I'm looking at it in a book.

Since I can't remember that combination I'm hoping this mnemonic will help: "Oh Crap Eddie", where "Oh" stands for "Option", "Crap" == "Command", and "Eddie" == "Esc".

If you've ever wanted to see your Dashboard widgets outside of the normal Dashboard environment, Amnesty Widgets lets you run your widgets directly on your desktop. It's not free, but if it's something you always wanted, you can download a free trial.

 

I just found the RubySearch dashboard widget for Mac OS X, and I like it. It offers a simple interface to your local ri/rdoc repository, with hyperlinking between classes, methods, superclasses, etc. It may be a lot better than going back and forth between a Terminal window and typing ri.

 

In a previous tip I discussed how to create a Mac sticky note from inside a Cocoa application, but I forgot to mention about how to use stickies as a standalone application.

Fortunately, it's pretty easy. Just open your Applications folder, and click the Stickies application icon. Once the Stickies application is started, just click File, and New Note to create a new sticky note, or press [Command][N].

I've been having a blast these last few days with Mac OS X Dashboard Widgets. Apple has assembled a nice collection of them, and they're all easy to install. Just download them (they seem to all be zip files), double-click the zip file to extract the contents, and then double-click the installer. Best of all, the widgets are free, fun to play with, and in some cases, they may even help your productivity.

I just learned about Mac Stickies, and they're pretty cool. If you're in a Cocoa application (like Safari, TextMate, and others) you can select text and/or graphics, and then easily save the content to a sticky note on your Mac desktop. To save the content you can either (a) remember the [Command][Shift][Y] keystroke, or else (b) click the application menu item (i.e., the "Safari" menu item if you're using Safari), then Services, then Make New Sticky Note.

The picture below shows what a Mac sticky note looks like when it's created on the desktop.