Online Book Reader

Home Category

AppleScript_ The Definitive Guide - Matt Neuburg [197]

By Root 1537 0
it without quitting.

In this example, we use AppleScript to construct a miniature "database." We have some strings; taking advantage of the write command's starting at parameter, we write each string into a 32-character "field." Notice the cautious error handling:

set pep to {"Mannie", "Moe", "Jack"}

set f to (path to current user folder as string) & "testFile"

try

set fNum to open for access file f with write permission

on error

close access file f

return

end try

try

set eof fNum to 0 -- erase if exists

set eof fNum to (count pep) * 32

repeat with i from 1 to (count pep)

write item i of pep to fNum starting at (i - 1) * 32

end repeat

close access fNum

on error

close access fNum

end try

Now we'll fetch the data from the "database." We take advantage of the fact that all data that isn't part of a string is null.

set f to choose file of type "TEXT"

try

set fNum to open for access f

on error

close access f

return

end try

set L to {}

try

set ct to (get eof fNum) / 32

repeat with i from 1 to ct

set end of L to read fNum from (i - 1) * 32 ¬

before ASCII character 0 -- read up to but not including null

end repeat

close access fNum

on error

close access fNum

end try

L -- {"Mannie", "Moe", "Jack"}

String and Clipboard


These commands give string-related information or work with the Clipboard.

Numbers and Dates


These commands add some extra arithmetic and date-time powers to AppleScript.

Miscellaneous


These are scripting addition commands I couldn't categorize.

Name

display dialog

Synopsis

general informational, text entry, and button-choice dialog

general informational, text entry, and button-choice dialog

A remarkably flexible little command. You can put up an information dialog. It can have an icon and a title. It can include a user text entry field; optionally, the user's text can appear as bullets (for password entry and so forth). You can dictate the names of up to three buttons, specify which is the OK button (which responds to Return) and which is the Cancel button (which responds to Esc or Command-Period, generates error -128), and learn which one the user pressed. The dialog can be set to time out if the user does not respond, and you can learn that this is what happened. By default, the buttons are "Cancel" and "OK"; a button called "Cancel" is the Cancel button by default. Returns a record.

Examples

set r to display dialog "Quick! Pick a Pep Boy!" buttons {"Mannie", "Moe", "Jack"} ¬

with icon caution giving up after 3

set favoritePepBoy to button returned of r

if favoritePepBoy is "" and gave up of r then set notFastEnough to true

set whoIsIt to text returned of (display dialog "What is your name?" ¬

default answer "" buttons {"OK"} default button "OK")

Name

display alert

Synopsis

informational dialog

informational dialog

Syntactically, a simpler version of display dialog; there is no user text entry. Has a more Cocoa-like appearance than display, dialog; there can be title text and smaller message text, and the icon has the proper look for the host application. The default is a single button ("OK"), and there is always a default button (the rightmost, by default). A button named "Cancel" is not automatically the Cancel button. The third (leftmost) button is gapped away from the others. Returns a record. (New in Tiger.)

Example

tell application "Finder"

display alert "Pep Alert" message "Mannie, Moe, and Jack welcome you."

end tell

Name

choose from list

Synopsis

listbox selection dialog

listbox selection dialog

Puts up a scrolling list of strings or numbers for the user to choose from. Returns a list of chosen strings or numbers, or false (not an error!) if the user cancels. There are two buttons, and you can set their titles; you can add a prompt and a window title. You can specify whether to permit multiple selections and/or an empty selection.

Example

set p to choose from list {"Mannie", "Moe", "Jack"} with prompt "Pick a Pep Boy:"

Name

choose file

Synopsis

file selection dialog

Return Main Page Previous Page Next Page

®Online Book Reader