• Aucun résultat trouvé

A Simple Mac Help Script

Dans le document Register for Free Membership to (Page 119-124)

The /Library/Scripts/Basics/AppleScript Help script (see Figure 2.42) launches the “Help Viewer” application and displays the AppleScript “Help” topic.

Figure 2.42A Basic Non-interactive Help Viewer

Although this script is only moderately helpful, it can be made better by searching for “Help” on a “user-supplied” topic, and adding the ability to search Apple manual (man) pages as well as Apple Help.To do this, we will need to figure out how to prompt for user input.This is accomplished through an expanded display dialog statement.

Interactive Dialog Boxes

The basic display dialog statement displays a simple message and two buttons

can be expanded to accept user input with the addition of a default answer to the display dialogstatement:

display dialog ¬

"What would you like help on?" default answer "AppleScript"

This command produces a dialog with a text input field (see Figure 2.43).

Figure 2.43Dialog With Text Input

This dialog is useless unless we write some code to handle that user input.

The result variable holds the result of the last command executed. When a script finishes running, the “Results” panel of the Script Editor window shows the last value of the script’s results variable (see Figure 2.44).

Figure 2.44Tracking the resultsVariable with Script Editor

In this case, the text field contained AppleScript and the OK button was clicked.These values were stored in the “text returned and button returned”

values of the return variable, respectively. An ifstatement can be used to test the text entered into the input box:

display dialog ¬

"What would you like help on?" default answer "AppleScript"

if text returned of result is "AppleScript" then display dialog "You entered AppleScript"

else

display dialog "You didn't enter AppleScript"

end if

Notice that we are checking the value of text returned of result, and not simply text returned. We must properly reference these values as belonging to result. We could also use an if statement to check which button is pressed, although pressing the Cancelbutton will cause the script to terminate:

display dialog ¬

"What would you like help on?" default answer "AppleScript"

if button returned of result is "OK" then display dialog "You pressed OK!"

else

--this line will never be reached if Cancel is pressed!

end if

However, checking the value of both the text returned and button

returned values this way will not work as expected.This is because the return variable is dynamic, meaning it is constantly updated whenever a statement is executed.This includes if statements. Once the if statement runs, there is a whole new return value, and the return value of display dialog is overwritten.

Because of this, the returnvalue must be grabbed immediately after the display dialogstatement.The /Library/Scripts/Finder/Add to File script does this in a very elegant fashion- with a single copystatement:

copy the result as list to {the prefix_or_suffix, the button_pressed}

This copy command takes the text returned and button returned values in the return variable and assigns them to prefix_or_suffix and button_pressed, respectively. We could use this to improve our Apple Help script, by

intro-if button_pressed is "OK" then

display dialog "You pressed OK, and entered " & help_topic & "!"

else

--this line will never be reached if Cancel is pressed!

end if

Now that help_topic and button_pressed contain the text entered and the button clicked, respectively, we can use these values throughout the script as needed without fear of return being overwritten.The first display dialog com-mand in our if statement reveals the value of button_pressed (OK) as well as the value of help_topicusing the & character, which effectively “glues” strings together into a unified output statement.

Testing the button returned of a simple two-button dialog (with OK and Cancel buttons) is rather pointless, since clicking OKwill continue the script and clicking Cancel will terminate the script.The ultimate goal of this exer-cise is to create a script that will display either man pages or Apple Help pages, which calls for an overhaul of our dialog’s buttons. Adding additional buttons to a dialog is rather easy:

display dialog ¬

"What would you like help on?" default answer ¬

"AppleScript" buttons {"Man", "Apple Help", "Cancel"}

Figure 2.45An Updated Three-button Help Dialog

This code results in the dialog box shown in Figure 2.45. Determining which buttons the user clicked is as easy as updating our ifstatements to reflect the new buttons. Our updated script, along with three buttons, a copy statement to catch the dialog’s return values, an updated ifstatement, and the

code from /Library/Scripts/Finder/Add to File to launch Apple Help is shown below:

display dialog ¬

"What would you like help on?" default answer ¬

"AppleScript" buttons {"Man", "Apple Help", "Cancel"}

copy the result as list to {help_topic, button_pressed}

if button_pressed is "Man" then

display dialog "You pressed the \"Man\" button!"

else if button_pressed is "Apple Help" then tell application "Help Viewer"

activate

search looking for help_topic end tell

end if

The Cancel button works just as it did before, terminating the script.

Clicking the Apple Help button launches the Apple Help application and automatically performs a search for help_topic, the text entered in the dialog box.The man (manual) lookup section of the script is unfinished, and displays a dialog box, but man is a Terminalcommand (specifically a shell command, which we’ll cover in the next section), but a visit to the Script Editor’s dictio-nary for Terminal reveals the do shellcommand.This command (wager a guess?) runs a shell commandwithin Terminal.To view a manual page for a spe-cific topic, we would normally run man followed by the name of the topic. In terms of our Apple Help script, we would run a shell command of man fol-lowed by the help_topic variable, using the “&” (ampersand) to “glue” the command together:

if button_pressed is "Man" then tell application "Terminal"

do script "man " & help_topic end tell

By plugging this into the if statement, we arrive at our finished script, which accepts user input and will look up topics in wither man or Apple Help, depending on which button is clicked:

display dialog ¬

"What would you like help on?" default answer ¬

"AppleScript" buttons {"Man", "Apple Help", "Cancel"}

copy the result as list to {help_topic, button_pressed}

if button_pressed is "Man" then tell application "Terminal"

do script "man " & help_topic end tell

else if button_pressed is "Apple Help" then tell application "Help Viewer"

activate

search looking for help_topic end tell

end if

This may seem like a long explanation for a fairly simple script, but user interaction via AppleScript comes in very handy, especially if you plan to integrate Automator workflows, AppleScript, and shell scripting—a very pow-erful combination.The next section discusses bash scripting, and the final sec-tion of this chapter integrates Automator, AppleScript, and bash into a

powerful and flexible application.

Dans le document Register for Free Membership to (Page 119-124)