AppleScript Programming/Scripting other applications

From Wikibooks, open books for an open world
Jump to navigation Jump to search

AppleScript can be used with other applications using the "tell" command. The tell command means to send a message to an application.

Anything written under that will be executed in the application Safari. So if we wanted to tell Safari to activate, we'd type:

tell application "Safari"
   activate
end tell

The "end tell" command will let AppleScript know that it has reached the end of the command. Also, make sure that the name of the application is in "quotes".

You can also shorten the command above to simply this:

tell application "Safari" to activate

Of course what Safari is for is getting content off of the web. The following example code activates Safari and has it open a particular web page.

tell application "Safari"
	activate
	open location "http://learnbymac.com"
end tell

Or instead of telling Safari to open a location, we could tell the Finder to and instead of our script specifying that Safari be run, the Finder would pick the default web browser and run that instead.

tell application "Finder"
	open location "http://learnbymac.com"
end tell

Notice that in the example above, we did not tell the Finder to activate. This means that the Finder didn't need to become the frontmost application. It just needed to be running, which is always is, and open the default web browser, which could be Firefox.app which doesn't even support AppleEvents. The Finder processed the tell-message and did the required action.

One thing that AppleScript is good at—why it was developed—is to combine the functionality of different Mac applications and let the user make new software without "reinventing the wheel"

Application Numbers[edit | edit source]

In the script below a range of cell values are read from the variable myTable which contains a reference to a table in an active document. It reads the values horizontally within the range, according to the reading direction of the language.

tell application "Numbers"

   tell myTable
      return value of every cell of range "A1:B10"
   end tell

end tell
Next Page: System Events | Previous Page: Aliases and paths
Home: AppleScript Programming