Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
AppleScript
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Intent== AppleScript was designed to be used as an accessible end-user scripting language, offering users an intelligent mechanism to control applications, and to access and modify data and documents. AppleScript uses [[Apple events]], a set of standardized data formats that the Macintosh operating system uses to send information to applications, roughly analogous to sending [[XPath]] queries over [[XML-RPC]] in the world of [[web service]]s.<ref name="Sanderson"/>{{rp|xxvi}} Apple events allow a script to work with multiple applications simultaneously, passing data between them so that complex tasks can be accomplished without human interaction.<ref name="Goldstein"/> For example, an AppleScript to create a simple web gallery might do the following: # Open a photo in a photo-editing application (by sending that application an ''Open File'' Apple event). # Tell the photo-editing application to manipulate the image (e.g. reduce its resolution, add a border, add a photo credit) # Tell the photo-editing application to save the changed image in a file in some different folder (by sending that application a ''Save'' and/or ''Close'' Apple event). # Send the new file path (via another Apple event) to a text editor or web editor application. # Tell that editor application to write a link for the photo into an HTML file. # Repeat the above steps for an entire folder of images (hundreds or even thousands of photos). # Upload the HTML file and folder of revised photos to a website, by sending Apple events to a graphical [[FTP]] client, by using built-in AppleScript commands, or by sending Apple events to Unix FTP utilities. For the user, hundreds or thousands of steps in multiple applications have been reduced to the single act of running the script, and the task is accomplished in much less time and with no possibility of random human error. A large complex script could be developed to run only once, while other scripts are used again and again. An application's scriptable elements are visible in the application's Scripting Dictionary (distributed as part of the application), which can be viewed in any [[#Script editors|script editor]]. Elements are generally grouped into ''suites,'' according to loose functional relationships between them. There are two basic kinds of elements present in any suite: classes and commands. * ''Classes'' are scriptable objects—for example, a text editing application will almost certainly have classes for windows, documents, and texts—and these classes will have properties that can be changed (window size, document background color, text font size, etc.), and may contain other classes (a window will contain one or more documents, a document will contain text, a text object will contain paragraphs and words and characters). * ''Commands'', by contrast, are instructions that can be given to scriptable objects. The general format for a block of AppleScript is to ''tell'' a scriptable object to run a command. All scriptable applications share a few basic commands and objects, usually called the Standard Suite—commands to open, close or save a file, to print something, to quit, to set data to variables—as well as a basic ''application'' object that gives the scriptable properties of the application itself. Many applications have numerous suites capable of performing any task the application itself can perform. In exceptional cases, applications may support plugins which include their own scripting dictionaries. AppleScript was designed with the ability to build scripts intuitively by recording user actions. Such AppleScript recordability has to be engineered into the app—the app must support Apple events and AppleScript recording;<ref>{{cite web |title=Scriptable Applications |url=https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptX/Concepts/scriptable_apps.html#//apple_ref/doc/uid/TP40001569-1153888-BAJICJEG |website=developer.apple.com |access-date=July 26, 2018 |language=en}}</ref> as Finder supports AppleScript recording, it can be useful for reference. When [[AppleScript Editor]] (Script Editor) is open and the Record button clicked, user actions for recordable apps are converted to their equivalent AppleScript commands and output to the Script Editor window. The resulting script can be saved and re-run to duplicate the original actions, or modified to be more generally useful. === Natural language metaphor === Whereas Apple events are a way to send messages into applications, AppleScript is a particular language designed to send Apple events. In keeping with the objective of ease-of-use for beginners, the AppleScript language is designed on the [[natural language programming|natural language]] [[metaphor]], just as the [[graphical user interface]] is designed on the [[desktop metaphor]]. A well-written AppleScript should be clear enough to be read and understood by anyone, and easily edited. The language is based largely on HyperCard's HyperTalk language, extended to refer not only to the HyperCard world of cards and stacks, but also theoretically to any document. To this end, the AppleScript team introduced the [[AppleEvent Object Model]] (AEOM), which specifies the objects any particular application "knows". The heart of the AppleScript language is the use of terms that act as nouns and verbs that can be combined. For example, rather than a different verb to print a page, document or range of pages (such as printPage, printDocument, printRange), AppleScript uses a single "print" verb which can be combined with an object, such as a page, a document or a range of pages. <syntaxhighlight lang="AppleScript"> print page 1 print document 2 print pages 1 thru 5 of document 2 </syntaxhighlight> Generally, AEOM defines a number of objects—like "document" or "paragraph"—and corresponding actions—like "cut" and "close". The system also defines ways to refer to properties of objects, so one can refer to the "third paragraph of the document 'Good Day'", or the "color of the last word of the front window". AEOM uses an application ''dictionary'' to associate the Apple events with human-readable terms, allowing the translation back and forth between human-readable AppleScript and [[bytecode]] Apple events. To discover what elements of a program are scriptable, dictionaries for supported applications may be viewed. (In the [[Xcode]] and [[AppleScript Editor|Script Editor]] applications, this is under ''File → Open Dictionary''.) To designate which application is meant to be the target of such a message, AppleScript uses a "tell" construct: <syntaxhighlight lang="AppleScript"> tell application "Microsoft Word" quit end tell </syntaxhighlight> Alternatively, the tell may be expressed in one line by using an [[infinitive]]: <syntaxhighlight lang="AppleScript"> tell application "Microsoft Word" to quit </syntaxhighlight> For events in the "Core Suite" (activate, open, reopen, close, print, and quit), the application may be supplied as the [[Object (computer science)|direct object]] to transitive commands: <syntaxhighlight lang="AppleScript"> quit application "Microsoft Word" </syntaxhighlight> The concept of an object [[hierarchy]] can be expressed using nested blocks: <syntaxhighlight lang="AppleScript"> tell application "QuarkXPress" tell document 1 tell page 2 tell text box 1 set word 5 to "Apple" end tell end tell end tell end tell </syntaxhighlight> The concept of an object [[hierarchy]] can also be expressed using either nested [[prepositional phrase]]s or a series of possessives: <syntaxhighlight lang="AppleScript"> pixel 7 of row 3 of TIFF image "my bitmap" TIFF image "my bitmap"'s 3rd row's 7th pixel </syntaxhighlight> which in another [[programming language]] might be expressed as sequential [[subroutine|method calls]], like in this [[pseudocode]]: <syntaxhighlight lang="JavaScript"> getTIFF("my bitmap").getRow(3).getPixel(7); </syntaxhighlight> AppleScript includes syntax for ordinal counting, "the first paragraph", as well as cardinal, "paragraph one". Likewise, the numbers themselves can be referred to as text or numerically, "five", "fifth" and "5" are all supported; they are synonyms in AppleScript. Also, the word "the" can legally be used anywhere in the script in order to enhance readability: it has no effect on the functionality of the script.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)