# AppleScript Reference for Internet Explorer 5

## Introduction

The following two imaginary reference entries illustrate how this document is organized, using the “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.

## AppleScript Class/Property Example: dog

## dog

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 date, 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 will return an error if the age property of the dog has a high value, 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.

Examples

    set the last vaccination date of dog "Sparky" ¬
        of household "Jones" to "Wednesday, July 21, 1999 10:30:00"

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 contain an additional section indicating which classes have it as a property. For example, the selected text property (see below) contains a Property of section that specifies application.

## AppleScript Command Example: plant

## plant

Place the specified flora into the ground where it can grow.

Syntax

plant Reference [in Location Reference] [using Reference]

Result

Boolean

Remarks

The first parameter, a Reference, specifies a bush, tree, vine, or some other flora to place into 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.

Examples

    plant cactus 23 in desert "Mojave" ¬
        using shovel 1 of backpack "Jo's Backpack"

Notes 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 Examples section, showing values that the command might return.

All the following entries are commands (rather than classes or properties) unless otherwise specified.

## Activate

Make Internet Explorer active and display a particular window.

Syntax

Activate [Integer] [Flags Integer]

Result

Integer

Remarks

The first 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 optional Flags parameter does nothing at present and is reserved for future use by Microsoft.

The optional result, an Integer, indicates the window ID of the active window after Internet Explorer becomes active.

Examples

    tell application "Internet Explorer 5.0"
        Activate
        OpenURL "http://www.microsoft.com/"
    end tell

## application

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.

## CloseAllWindows

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.

You do not need to determine the return value of this command, and so the example below could have been abbreviated as follows:

    CloseAllWindows

Examples

    set AllClosed to CloseAllWindows

## CloseWindow

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.

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.

You do not need to determine the return value of this command, and so the example below could have been abbreviated as follows:

    CloseWindow ID -1

Examples

    --Close active window
    set FrontClosed to CloseWindow ID -1

## do script

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. To see this technique in action, look at some of the AppleScripts that were distributed with this documentation.

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

1. `--Count the number of links in the active window set NumberOfLinks to (do script ("document.links.length"))`
2. `--Find the URL of the active window set CurrentURL to (do script ("document.location"))`

## GetSource

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.

Examples

    set PageSource to GetSource

## GetURL

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.

Examples

    GetURL (theLink) to file "Macintosh HD:Desktop Folder:example.html"

## GetWindowInfo

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

1. `--Get the window info of the frontmost window
   set activeWindowID to item 1 of ListWindows
   set activeWindowInfo to (GetWindowInfo activeWindowID)`
2. `--Get the URL of the active window
   set MainURL to first item of (GetWindowInfo -1) as string`

Example result

{"http://www.microsoft.com", "Welcome to Microsoft's Homepage"}

## ListWindows

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

Examples

    set activeWindowID to item 1 of ListWindows
    set activeWindowInfo to (GetWindowInfo activeWindowID)

## open

Open a document in Internet Explorer.

Syntax

open Alias

Examples

    open file "Macintosh HD:Desktop Folder:example.html"

## open bookmark

Open a bookmark file.

Syntax

open bookmark Alias

Remarks

The Alias parameter must be a file 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.

Examples

    open bookmark file "Macintosh HD:Desktop Folder:MyBookmarks.html"

## OpenURL

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.

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

1. `OpenURL ("javascript:window.moveTo((screen.width/2)+10,40);
   window.resizeTo((screen.width/2)-15,(screen.height-50))")`
2. `OpenURL (theLink) to file OutputFile`

## ParseAnchor

Combine a base URL and a relative URL to produce a fully specified 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 specified URL “http://www.microsoft.com/mac/ie/”, home page of Microsoft Internet Explorer for the Macintosh.

Examples

    set theURL to ParseAnchor "http://www.microsoft.com" withURL "mac/ie/"

## print

Print a file.

Syntax

print Alias

Examples

    print file "Macintosh HD:Desktop Folder:example.html"

## PrintBrowserWindow

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.

Examples

    with timeout of 180 seconds
        PrintBrowserWindow
    end timeout

## quit

Quit Internet Explorer.

Syntax

quit

Examples

    tell application "Internet Explorer 5.0"
        quit
    end tell

## run

Run Internet Explorer.

Syntax

run

Examples

    tell application "Internet Explorer 5.0"
        run
    end tell

## selected text

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.

Examples

    copy (get selected text) to TheSelectedText

## ShowFile

Display the specified file, containing data of a specified MIME type, in the specified window.

Syntax

ShowFile Alias [MIME type String] [Window ID Integer] [URL String]

Remarks

The first parameter is a file 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 ID 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.

Examples

    tell application "Internet Explorer 5.0"
        ShowFile alias "example.html"
    end tell