mac-os-x

recent posts related to the mac os x operating system

AppleScript tip: working with booleans

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:

AppleScript tip: legal variables names

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).

AppleScript list manipulation examples

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 subroutines: how to create a subroutine

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: Addition, subtraction, multiplication, division

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 - How to display the current date in a dialog

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.

 

 

AppleScript dialog - display a number 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

 

AppleScript tip: using the ScriptEditor dictionary

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:

AppleScript dialog - prompt for a response

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 (comment syntax)

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:

Syndicate content