Linking Programs


You have seen how AppleScript can automate tasks on your own computer. Beginning with Mac OS X 10.1, AppleScript can also send Apple events messages to open applications on other Macs in a network. As a result, you can use AppleScript to control applications on other people’s computers. Of course, the reverse is also true; other people can use AppleScript to control applications on your computer. To send and receive Apple events over a network your Mac must be running with Mac OS X 10.1, 10.2, or Mac OS 9.

Sharing programs by sending and receiving Apple events messages across a network is called program linking. For security reasons, program linking is normally disabled. Computers that you want to control with AppleScript must be set to allow remote Apple events. Likewise you must set your computer to allow remote Apple events.

Mac OS X 10.1 and up use the TCP/IP protocol to send and receive Apple events messages over a network. Therefore, Mac OS X 10.1–10.3 can send and receive Apple events messages over the Internet as well as a local network. Mac OS X 10.1–10.3 can’t use the AppleTalk protocol to send or receive Apple events over a remote network as can Mac OS 9 and earlier. Mac OS X 10.0–10.0.4 can’t send or receive remote Apple events at all.

Allowing remote Apple events

If you want a Mac OS X computer to receive Apple events from remote computers, you must set it to allow remote Apple events. First, open System Preferences and then choose View Sharing (or click the Sharing button). In Sharing Preferences, click the Application tab and then turn on the option labeled Allow remote Apple events. If you want the computer to receive Apple events from remote Mac OS 9 computers, turn on the option labeled Allow Mac OS 9 computer to use remote Apple events. If you turn on this option, a dialog appears in which you must enter a password that Mac OS 9 users will have to use when sending Apple events messages to the computer. You can change this password later by clicking Set Password in Sharing Preferences. Figure 23-11 shows remote Apple events turned on in Sharing Preferences.

click to expand
Figure 23-11: Set Mac OS X to receive remote Apple events by using the Sharing pane of System Preferences.

Scripting across a network

Using AppleScript to run a program across the network doesn’t take much more work than writing a script to use a program on the same computer. For example, the following script sends commands to the Finder on the computer at IP address 192.168.1.203:

set remoteMachine to machine "eppc://192.168.203" tell application "Finder" of remoteMachine     using terms from application "Finder"          activate          open the trash     end using terms from end tell

The example script begins by setting the value of variable remoteMachine to the URL of a remote computer. A URL for remote Apple events begins with eppc:// and is followed by the remote computer’s IP address or DNS name. (The prefix eppc stands for event program-to-program communication.) Starting with Panther, machines can also be called by their Rendezvous name in scripts (no URL required).

The second statement of the example script names the application, in this case Finder, and uses the variable remoteMachine to identify the remote computer.

start sidebar
Turning On Program Linking in Mac OS 9

If you want a computer that’s using Mac OS 9 to receive remote Apple events messages from your computer, the Mac OS 9 computer must have program linking turned on. To do this, open the Mac OS 9 computer’s File Sharing control panel and click the Start button in the control panel’s Program Linking section. In addition, turn on the option labeled Enable Program Linking clients to connect over TCP/IP. The Mac OS 9 computer is ready for program linking when the button’s label changes to Stop and the File Sharing control panel reports “Program Linking on,” as shown in the following figure.

A Mac OS 9 computer can receive remote Apple events when program linking is turned on and set to use TCP/IP.

click to expand

Mac OS 9 can be configured to receive remote Apple events only from specific users. These access restrictions are set up with the Users & Groups tab in the File Sharing control panel. In addition, each application on a Mac OS 9 computer can be set to not receive any remote Apple events. This restriction is set in each application’s Info window in the Finder.

end sidebar

Inside the tell application...end tell block is another block that is bracketed by the statements using terms from and end using terms from. When AppleScript encounters the statement using terms from, it compiles subsequent statements using the named application’s scripting dictionary but does not send the resulting Apple events to this application. The Apple events from a using terms from block are sent to the application named in the enclosing tell application block. In the example script, AppleScript compiles the activate and open the trash statements using terms from the Finder’s scripting dictionary on the local computer (your computer) but sends the resulting Apple events to the Finder on the remote computer.

When you run a script that sends remote Apple events, AppleScript has to connect to the remote application. Before doing this, AppleScript displays a dialog in which you must enter a name and password of a user account on the remote computer. If you connect successfully, the script continues. At this point the example script should cause the remote computer’s Finder to become the active application and open the Trash in a Finder window.

If you run the script again, you don’t have to go through the authentication process. After AppleScript is connected to an application on a particular remote computer, you don’t have to go through the authentication dialog each time you want to send an Apple event.

AppleScript Studio

You have seen that AppleScript is a very powerful scripting language, and should now have some ideas as to how you can make your own life more automated. While AppleScript is billed as a scripting language, it is, for all intents and purposes, a programming language as well. Apple knows this, and has built into the developer’s tools a means to easily create an application-quality front end to your AppleScripts. Using AppleScript Studio, in concert with Script Editor you can build full-fledged applications with icons, menus and all of the interface elements associated with a complete application. AppleScript Studio has been incorporated into Xcode and Interface Builder and is included free with Mac OS X.3 on the Developer Tools CD. Cocktail and Carbon Copy Cloner, utility applications mentioned in Chapter 21, are two good examples of applications that were written in AppleScript and given a proper Mac OS X interface using AppleScript Studio.




Mac OS X Bible, Panther Edition
Mac OS X Bible, Panther Edition
ISBN: 0764543997
EAN: 2147483647
Year: 2003
Pages: 290

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net