Online Book Reader

Home Category

AppleScript_ The Definitive Guide - Matt Neuburg [10]

By Root 1531 0
remembers it for you.

Here's an example involving URLs. Often, working in some application, I see a URL that I'd like to "go to" in the approprate manner. If it's an http URL, my default browser should open and fetch that page. If it's an email address, my email program should create a new message to that addressee. In some applications you can just click a URL and the right thing happens, but many applications provide no such facility, so I have to resolve the URL manually. This means I must look at the URL and decide on the appropriate helper program; then I select and copy the URL; then I somehow start up the helper program; finally, I paste the URL into the appropriate location. In a browser, I must hit Return afterwards, in order to go to that URL; in an email program, I must create a new message first, in order to have something to paste into. This doesn't sound like very many steps, but it's all very annoying, especially in comparison to those applications where the right thing just happens with a single click.

The solution is an AppleScript program. I've assigned it a keyboard shortcut (ways of doing this are discussed in Chapter 2), so the procedure is this: select and copy the URL, then press the keyboard shortcut. That's a significant savings in time and trouble. Here's the script:

set theProc to (get path to frontmost

application as Unicode text)

tell application "Finder"

activate

delay 1 -- give time for clip to convert from Classic

set theURL to (get the clipboard as string)

end tell

ignoring application responses

try

open location theURL

end try

end ignoring

activate application theProc

The switch to the Finder is to force the clipboard contents to convert themselves to a usable form (and the delay is to give this time to happen); this seems to be needed particularly when working in a Classic application. At the end of the script I switch back to the application I was using at the outset. The heart of the script is the open location command, which does the "right thing" with the URL.

Customization


No application in the world can meet everyone's desires and expectations, because whatever the application's features, it is impossible for the developers of that application to anticipate everything that every user will wish to do with it. AppleScript can be a solution to this problem. Scriptability can provide, in essence, an entire alternative user interface: instead of the graphical user interface of buttons and menus, it's a programming interface. An application's scriptability says to you: "Here are all the types of thing this application operates on, and here are the operations you can perform on them; if none of this application's menu items and buttons and other graphical interface items performs just the sequence of operations you desire, feel free to use AppleScript to create a sequence that does."

Here's a real-life example . On the Internet, someone asked about assigning track numbers in iTunes. A track number is an attribute of a song, which can be set in that song's Get Info dialog; it can be made to appear in the playlist display, and you can sort on it. Thus, track numbers can be used to control the order of playback in a playlist. This user wanted to assign track numbers immediately after "ripping" a CD to iTunes , so that the order in which the tracks appeared on the original CD, and in which they appeared in the initial playlist derived from that CD, could easily be restored within iTunes later on. In essence, the user was saying: "The tracks are already in their correct order within this playlist; how can I use that order to assign all the tracks a track number, in a single move?"

My response was: "Use AppleScript." Here's a script that does it:

set i to 1

tell application "iTunes"

tell (get view of browser window 1)

repeat with aTrack in (get every track)

set track number of aTrack to i

set i to i + 1

end repeat

end tell

end tell

An interesting philosophical debate then ensued. The user thanked me, but expressed regret that this was the "only way" to accomplish

Return Main Page Previous Page Next Page

®Online Book Reader