Online Book Reader

Home Category

AppleScript_ The Definitive Guide - Matt Neuburg [222]

By Root 1578 0

Moe

Do not use any scripting addition commands that put up a user interface, such as display dialog, directly from within osascript. The problem is that osascript is not an application context, so it has no provision for user interactivity. That's why we targeted the Finder in the earlier examples involving display dialog. In Tiger, an attempt to use an interactive scripting addition command will be blocked coherently with a nice error message ("No user interaction allowed"); in earlier systems, however, the dialog would appear, but you couldn't click the buttons to dismiss it, and the only escape was to kill the osascript process.

Arguments after the script on the command line with osascript are treated as a list of strings to be passed to the script's run handler (see "The Run Handler" in Chapter 9); this feature is new in Tiger. For example:

% cat > textfile.txt

on run what

set total to 0

repeat with anItem in what

set total to total + anItem

end

return total

end

^D

% osascript textfile.txt 1 2 3

6

You are most likely to use osascript not directly from the command line, but from within a shell script written in a language such as Perl. In this situation it's rather easy to tie oneself in knots escaping characters when constructing a string intended for osascript, because two environments, the shell script language and the shell, are going to munge this string before it reaches AppleScript. This line of Perl shows what I mean:

$s = `osascript -e "tell app \\"Finder\\" to get name of every disk"`;

The Perl backtick operator hands its contents over to the shell for execution, but first there's a round of variable interpolation within Perl; during that round, the escaped backslashes are mutated into single backslashes, and these single backslashes correctly escape the double quotes around "Finder" in the string that the shell receives as the argument to osascript.

One solution is to single-quote the string, which avoids the round of interpolation. This example is a Perl script that single-quotes everything in sight, and also illustrates how easily Perl lets you create a literal string representing multiline AppleScript code:

$howdy = 'tell app "Finder"

display dialog "howdy"

end';

`osascript -e '$howdy'`;

An even better approach is to use a "here document ," which is supported by most scripting languages. This eliminates worries about escaping characters while letting you construct the AppleScript code dynamically through variable interpolation. To illustrate, here's a rather silly Perl program intended to be run in the Terminal; it asks the user for the number of a disk and then fetches the name of that disk:

#!/usr/bin/perl

$s = <<"END_S";

tell application "Finder"

count disks

end tell

END_S

chomp ($numDisks = `osascript -ss -e '$s'`);

print "You have $numDisks disks.\n",

"Which one would you like to know the name of?\n",

"Type a number between 1 and $numDisks: ";

while (<>) {

chomp;

last if $_ < 1 || $_ > $numDisks;

$ss = <<"END_SS";

tell application "Finder"

get name of disk $_

end tell

END_SS

print `osascript -ss -e '$ss'`,

"Type a number between 1 and $numDisks: ";

}

Observe that the result of osascript has an extra return character appended to it, which has to be chomped if that isn't what we wanted. Here's the game in action, played in the Terminal:

% ./disker.pl

You have 3 disks.

Which one would you like to know the name of?

Type a number between 1 and 3: 1

"feathers"

Type a number between 1 and 3: 2

"gromit"

Type a number between 1 and 3:4

Here's a serious real-life example of osascript in action. It started out as a Ruby script written to construct a histogram of a text file, showing the 30 most frequently used words in the file. When the question arose of how to store the results, it seemed a good idea to put them into a Microsoft Excel spreadsheet; the obvious way to transfer the data from Ruby to Excel was AppleScript. At that point, it also seemed a good idea to have Excel chart the the results (as shown in Figure 25-1).

Here's the script:


Return Main Page Previous Page Next Page

®Online Book Reader