The following two imaginary reference entries illustrate how the AppleScript Reference is organized, using "real world" examples of a pet dog and planting a cactus. Following each example is a section of notes that explain how an actual entry might differ from these imaginary ones.
Mammal very similar to wolf. Class.
Plural Form
dogs
Elements
ear by numeric index, by condition
eye by numeric index, by condition
leg by numeric index, by condition
nose
paw by numeric index, by condition
tail
tongue
tooth by numeric index, by condition
Properties
age, breed, color, favorite toy, housebroken, last vaccination, name, neutered, sex, size
Remarks
This class is useful for discouraging intruders but is frequently affectionate as well. It is an element of both the family and the K9 corps classes.
Each paw element of a dog contains several claw and toe elements.
You can teach dogs tricks with the train command, but this operation might return an error, so place the command within a try block.
The ear, eye, eyebrow, leg, paw, and tooth elements of dog can all be accessed by index, but not the nose, tail, and tongue elements, since each dog contains only one of these elements.
Example
set the last vaccination of dog "Sparky" ¬
of household "Someone" to "September 5, 1999"
Note Entries for classes and properties are quite similar, except that if the example above were an entry for a property, rather than for a class, it would not contain sections for Plural Form, Elements, or Properties, but would contain an additional phrase near the beginning indicating which classes have it as a property. For example, the selected text class (see below) contains a Property Of section that specifies application.
The data type of a property is not specified in the entry if the values are enumerated; for example, a hypothetical breed property for our dog example might use the possible values Boxer, Chow, Husky, German Shepherd, and so forth instead of a normal data type. If a property uses a set of enumerated values rather than a standard data type, the entry has a Possible Values section.
Place the specified flora in the ground where it can grow. Command.
Syntax
plant Reference [in Location Reference using Reference]
Result
Boolean
Remarks
The first parameter, a Reference, specifies a bush, tree, vine, or other flora to place in the ground. The optional in parameter specifies a planting location, and the optional using parameter specifies the shovel or other implement used to dig the hole.
The result is a Boolean indicating the success or failure of the operation. A value of true indicates the organism was planted successfully.
Example
plant cactus 23 in desert "Mojave" ¬
using shovel 1 of backpack "Someone's Backpack"
Note If the value returned by a command is unusual in some way, the reference entry for the command will contain an Example Result section after the Example section, showing values that the command might return.
All the following entries are commands (rather than classes or properties) unless otherwise specified.
Make Internet Explorer active and optionally display a particular window.
Syntax
Activate [Integer]
Result
Integer
Remarks
The sole parameter, an Integer, specifies the ID of the window to display when Internet Explorer becomes active. You can use a value of -1 to indicate the active window. You can also obtain a list of IDs for open windows with the ListWindows command.
The result, an Integer, indicates the window ID of the active window after Internet Explorer becomes active.
Example
tell application "Internet Explorer"
Activate
OpenURL "http://www.microsoft.com/"
end tell
The Internet Explorer application itself. Class.
Properties
selected text
Remarks
The application class is not used much except for its selected text property. This fact may change with future releases of Internet Explorer.
Close every window in Internet Explorer.
Syntax
CloseAllWindows
Result
Boolean
Remarks
This command has no parameters, but its result, a Boolean, indicates whether the CloseAllWindows operation was successful. A value of true indicates all windows were closed.
If you do not need to determine the return value of this command, you can abbreviate the example below as follows:
CloseAllWindows
Example
set AllClosed to CloseAllWindows
Close a window, specified either by window ID or title.
Syntax
CloseWindow [ID Integer] [Title String]
Result
Boolean
Remarks
The optional ID parameter specifies the ID of the window to close, as given by such commands as ListWindows. You can use a value of -1 to specify the active window. Omitting the parameter closes the currently active Internet Explorer window.
The optional Title parameter specifies the title of the window to close. If neither ID nor Title is specified, Internet Explorer closes the active window.
The result, a Boolean, indicates whether the CloseWindow operation was successful. A value of true indicates the specified window was closed.
If you do not need to determine the return value of this command, you can abbreviate the example below as follows:
CloseWindow ID -1
Example
--Close active window
set FrontClosed to CloseWindow ID -1
Execute JavaScript commands.
Syntax
do script String [window Integer]
Result
String
Remarks
The String parameter contains the JavaScript text to execute. The optional window parameter specifies the ID of the window in which to execute the script. You can get a list of window IDs with the ListWindows command.
JavaScript is useful when there is no AppleScript command in the Internet Explorer dictionary to do what you need. It is possible to generate JavaScript code programmatically by means of AppleScript: you can systematically build a JavaScript expression from individual building blocks within AppleScript code.
Some JavaScripts may not run unless there is at least one open Web page in Internet Explorer.
You can also run multiline JavaScripts with the OpenURL command.
Examples
--Count the number of links in the active window
set NumberOfLinks to do script "document.links.length"
--Find the URL of the active window
set CurrentURL to do script "document.location"
Get the HTML source of an Internet Explorer window.
Syntax
GetSource [Integer]
Result
String
Remarks
The optional Integer parameter specifies the index of the window from which to get the source. Omitting the parameter specifies the currently active Internet Explorer window.
You can get a list of window IDs with the ListWindows command.
Example
set PageSource to GetSource
Open a URL, optionally saving its data to disk.
Syntax
GetURL String [to File Specification]
Remarks
The optional to parameter specifies the file to which to save the data. If you omit this parameter, the browser will not save any data.
Example
GetURL theLink to file "Macintosh HD:Desktop Folder:example.html"
Return the URL and title of the specified window as a window info record.
Syntax
GetWindowInfo Integer
Result
List
Remarks
The single parameter for this command, an Integer, specifies the ID of the window from which you want the URL and title. You can get a list of window IDs with the ListWindows command. Using -1 for the window ID specifies the active window.
The result of this command is a window info record, which contains the URL and title of the specified window.
The example result below shows a typical window info record resulting from use of the GetWindowInfo command. The first item of a window info record is always the window's URL; the second, its title.
Examples
--Get the window info of the frontmost window
set activeWindowID to item 1 of (ListWindows)
set activeWindowInfo to (GetWindowInfo activeWindowID)
--Get the URL of the active window
set MainURL to first item of (GetWindowInfo -1)
Example result
Get a list of all open windows as a list of window IDs. The first item in the list is the ID for the active window; all the other window IDs follow in order.
Syntax
ListWindows
Result
List
Example
set activeWindowID to item 1 of (ListWindows)
set activeWindowInfo to GetWindowInfo activeWindowID
Open a document in Internet Explorer.
Syntax
open List of Alias
Example
open {alias "Macintosh HD:MyFolder:ex1.html", ¬
alias "Macintosh HD:MyFolder:ex2.html"}
Open a bookmark file.
Syntax
open bookmark Alias
Remarks
The Alias parameter must be an alias to a bookmark file, which can be an Internet Explorer Favorites file, a Netscape bookmarks file, or any other kind of HTML file with links.
If the specified bookmark file is not available, Internet Explorer reloads the current bookmark file, and then appends the links in the file that was opened to the Favorites file.
While any kind of HTML file with links can be loaded, not all such pages are appropriate. Pages whose links have descriptive names work best.
Example
open bookmark alias "Macintosh HD:Desktop Folder:MyBookmarks.html"
Retrieve a URL off the Web.
Syntax
OpenURL String [to File Specification] [toWindow Integer] [Flags Integer] [FormData Anything] [MIMEType String]
Remarks
The optional to parameter specifies the file to which to save downloaded data, while the optional toWindow parameter specifies the window into which to open the URL. If you use a value of -1 for toWindow, the browser will open the URL into the active window. If you use a value of 0, the browser will open it into a new window.
Valid values for the optional Flags parameter are 1 (ignore the document cache while opening the URL), 2 (ignore the image cache while opening the URL), and 4 (operate in background mode). These flag values can be added together to combine their effects; for example, a value of 3 ignores both the document cache and the image cache.
The optional MIMEType parameter specifies the MIME type for the form data given in the FormData parameter.
Example 1 below shows how to use the OpenURL command to run JavaScript consisting of multiple lines of JavaScript code separated by semicolons. (See also the do script command.) Some JavaScripts may not run unless there is at least one open Web page in Internet Explorer.
Examples
OpenURL ("javascript:window.moveTo((screen.width/2)+10,40);
window.resizeTo((screen.width/2)-15,(screen.height-50))")
OpenURL (theLink) to OutputFile
Combine a base URL and a relative URL to produce a fully qualified URL.
Syntax
ParseAnchor String withURL String
Result
String
Remarks
The first parameter represents the main URL, such as http://www.microsoft.com, while the second parameter represents the relative URL, such as mac/ie/. The result in this case is the fully qualified URL http://www.microsoft.com/mac/ie/, home page of Microsoft Internet Explorer for Mac.
Example
set theURL to ParseAnchor "http://www.microsoft.com" withURL "mac/ie/"
Print a file.
Syntax
print List of Alias
Example
print {alias "Macintosh HD:MyFolder:ex1.html", ¬
alias "Macintosh HD:MyFolder:ex2.html"}
Print the contents of the browser window.
Syntax
PrintBrowserWindow [Integer]
Remarks
The optional Integer parameter specifies the index of the window to print. Omitting the parameter specifies the currently active Internet Explorer window.
Example
with timeout of 180 seconds
PrintBrowserWindow
end timeout
Quit Internet Explorer.
Syntax
quit
Example
tell application "Internet Explorer"
quit
end tell
Run Internet Explorer.
Syntax
run
Example
tell application "Internet Explorer"
run
end tell
The currently selected text. Read-only String.
Property of
application
Remarks
The selected text property contains the text that is currently selected in the active Internet Explorer window.
Example
copy selected text to TheSelectedText
Display the specified file, containing data of a specified MIME type, in the specified window.
Syntax
ShowFile Alias [Mime Type String] [Window Identifier Integer] [URL String]
Remarks
The first parameter is an alias specifying the file to display.
The optional Mime Type parameter specifies the MIME type for the data in the file that is being displayed.
The optional Window Identifier parameter specifies the ID of the window into which to open the file. Note that you can get a list of window IDs with the ListWindows command.
The optional URL parameter specifies a URL that will allow the document to be reloaded if it cannot be read from the file alias for some reason.
Example
ShowFile alias "example.html"