Thursday, March 17, 2011

Apple Script: Telling Applications to do commands

The basic script for telling applications to do something is:

tell application "applicationname"
end tell

The first line of the script tells the application "applicationname" to do what is between the tell and the end tell part. End tell, tells the script editor to stop telling that application to do commands. Whenever you use a tell command you must also have an end tell command.
Here is an example script:

tell application "iTunes"
activate
end tell



This tells the application "iTunes" to do the command(s) until the end tell. The command(s) in this script is activate, which activates the application iTunes. Another script is this:

tell application "iTunes"
quit
end tell



This tells the application "iTunes to quit. To find an applications name and the commands you can use with them, go to the Script Editor -> File -> Open Dictionary and then select the application you would like to use apple script to control. Lets for example look at iTunes. Scan through the commands and we see the command next track. The v after next track can be thought of as verb, meaning it is an action.



Lets make a script out of that command. The script will look like this:

tell application "iTunes"
next track
end tell



Running this script will make itunes play the next track. Lets look at another command, play.



Play is an action command as stated by the v after play. The words after : are the description of those commands. Anything within the bracket [information] is optional. We can make a script that just uses play:

tell application "iTunes"
play
end tell



This will tell iTunes to play. If iTunes is on pause and you run this script iTunes will play. If we look back to our play command there was an optional [specifier] : item to play. We can have an item after the play command and it will play that item. Here is an item, track.



Track can be thought of as a noun because of the n after it. This means it isn't an action just a person, place, or thing. Now since we know our commands we can make a neat script:

First we start out with what we learned in our last section and type:

display dialog "What song would you like to play?" default answer ""
set play_song to text returned of result

Remember this displays a box with the text "What song would you like to play?" and lets you type in an answer. Then it sets the play_song variable to what was typed in.
Now we can use that with what we learned in this section and add to the script.

display dialog "What song would you like to play" default answer ""
set play_song to text returned of result
tell application "iTunes"
try
play track play_song
end try
end tell

  • We know what the first two lines mean.
  • The third line tells the application "iTunes" do to the commands until the end try command
  • The next line says try. Try is another command like tell. Try, tries to do everything between it and the end try command. I will explain why we use the try command later
  • The next line with play the track play_song which is a variable that stands for whatever the user types to answer the question "What song would you like to play?"
  • The last two lines end the try command and end the tell command



Run the script and type in a song name. If the song name is in your iTunes it will play it. The reason we used the try command is because if we didn't and we typed in a song name that was in our iTunes library we would get this error:



This is because we told iTunes to play a song that isn't there so it errors. When we use the try command to play a song that isn't there it will try and if it errors it won't stop the script, but it will end the try command.

Apple Script: Getting Input from a User

I'm going to tell you about three options to get information from a user. The first is to get what the user writes. To do this we will use this code:

display dialog "Are you a noob?" default answer ""
set noob_answer to text returned of result
display dialog noob_answer

  • The first line of the script displays the text Are you a noob? and lets you type in an answer. Without the default answer "" part of the code it will not let you type in an answer.
  • In the second line noob_answer is our variable and we set our variable to the text returned of result. The result being what we put in the text and clicked ok and setting that result as text.
  • The last line will display whatever the variable noob_answer is set to, in this case we set the result of the question to the variable so it will display whatever you type.

Script


Type in whatever you want and click ok


Result


The next option we have is to have a list of options for a user to choose from. To do this we use this script:

(choose from list {"Noob", "Average", "Expert"} ¬ 
with prompt "What is your programming skill?")
set list_answer to result as text
display dialog list_answer

  • The first line of code tells Script Editor that it wants the user to choose from a list with options Noob Average and Expert. The symbol ¬ tells the Script Editor to read that line and the line below it as one line. It is useful to make your scripts look cleaner. To make the symbol use Option + l <- Lowercase L
  • The second line has the with prompt part which displays the description of what the user is picking.
  • The third line sets the variable list_answer to the result choosen from the list and sets it as text.
  • The last line displays the variable list_answer, which is set to the result choosen from the list.

Script


Choose from the list


Result if Average choosen


The third option we are going to use to get information from a user is making a user choose between buttons. The limitation for buttons is that there can only be 3 buttons. The script for this is:

display dialog "What food do you like best?" ¬
buttons {"Waffles", "Pizza", "Ice Cream"}
set best_food to button returned of result
say best_food

  • The first like tells us to display the text "What food do you like best?" again we use the ¬ symbol to tell the Script Editor that line 1 and line 2 should be read as one line. We don't need to use that symbol if we put them in one line like this:
  • display dialog "What food do you like best?" buttons {"Waffles", "Pizza", "Ice Cream"}
    but in the editor it looks better the with the ¬ symbol and is easier to edit.
  • The second line makes the buttons Waffles, Pizza and Ice Cream available to choose, remember there is a maximum of three buttons.
  • The third line sets the variable best_food to the button that is returned as a result of a user clicking that button
  • In the last line I introduce a new command. Instead of display dialog which displays a box, we can also use say, which tells your computer to speak. In this example the computer will say whatever the variable best_food stands for
Script:


Choose a button:


Result: Your computer says what button you choose! Now that you know three ways you can get information from a user we can now learn how to tell applications do to things.

Apple Script: Hello World

The first script a person writes in a language tends to be a "Hello World" tutorial.
To write Hello World in Apple Script open the Script editor and type in:
display dialog "Hello World" and click the green arrow with run underneath it.

Script


The result of this will display this window:


Anything written within the double quotations " " will be thought of as absolute text meaning that the text is read as it appears, here is another example using a variable. A variable is something that stands something else. In math class when x=43, x was a variable.

Lets write another Hello World script but this time lets use a variable. Variables can be written as anything you want them to be but usually it is good to put them in the format of sometext_sometext. That is because we don't want to confuse them with any internal commands apple script has. Type the following in the editor:

set first_variable to "Hello World"
display dialog first_variable

Script

Run this script and once again this will come up with a box saying Hello World. We didn't use double quotations around first_variable, this is because we want what the variable is set to and not the variable name. Check out below what would happen if we wrote it with double quotations.

Script

We would get this result:

This is because display dialog used the text "first_variable" instead of the variable first_variable.

Now that you know how to display text and how to use variables we can move on to our next part.

Wednesday, March 16, 2011

Apple Script

Apple Script is a build in programmable language built in with every Mac computer. To access the editor go to the "Spotlight" finder in the top right hand corner of your computer and type in "Script Editor" click the Script Editor result and begin coding:


Picture of what it should look like. Most likely the Top Hit and Applications will be the same thing so click either one. Now that you have the Script Editor you can use the tutorials below to make your first scripts!

1. Hello World: Displaying Dialog
2. Getting User Input: Type, Lists, Buttons
3. Telling applications to do commands: Itunes