"The development of applescript" Notes

Source: Internet
Author: User
Tags apple event
This article is to read the notes of William R. Cook of "applescript.

This article describes the universal scripting language applescript on Mac OS and the universal inter-process communication mechanism Apple Events.

Data Objects in Scriptable applications that support scripts include (child) elements (elements) and properties (properties) fields. (Sub) element fields can have multiple values. These values are referenced by (sub) objects, and the values of attribute fields are either of the basic types (such as strings and integers) or an object reference. For example, a document contains several paragraph sub-elements and a name attribute. A paragraph has the style and size attributes and contains the word and character sub-elements.

Apple events is a variant of the standard Remote Procedure Call mechanism. Applications can communicate with each other by sending Apple Events. Each event contains a verb and several parameters. The parameter can be a basic type value or an object specifier (Object descriptor ). The object descriptor is actually a query expression. The remote program interprets and evaluates it to obtain the data object (SET) in the program ). Therefore, this mechanism avoids the existence of remote object (stub. For example, after the following expression is evaluated, a set of textstyle attributes is obtained:

The textstyle of Character 1 to 10 of card field "Description"

An object descriptor is generally composed of properties (such as textstyle) and elements (such as character and card field). element (s) can be selected from the set by name, subscript, or range after elements ).

Applescript is often used for automation of complex tasks and application customization. It provides commonly used programming facilities (control flow, variables, data structures ). One of its main functions is to send and receive Apple Events. Sending an apple event is similar to a process call, while processing the received Apple event is handled by subroutine of the same name. Applescript provides
The special Syntax of object references (Object Reference) to process the object specifiers of Apple event. Its format is defined as follows:

Reference: =
| Property
| 'Beginning'
| 'End'
| 'Before' term
| 'After' term
| 'Some' singularclass
| 'First' singularclass
| 'Last' singularclass
| Term ('st' | 'nd' | 'RD '| 'th') anyclass
| 'Ddle' anyclass
| (Pluralclass | 'every' anyclass) ['from' term toorthrough term]
| Anyclass term [toorthrough term]
| Singularclass 'before' term
| Singularclass 'after' term
| Term ('of' | 'in' | ''s') Term
| Term ('whose' | 'where' | 'that ') Term

Toorthrough: = 'to' | 'thru '| 'inter'

Call: = message '('expr *')'
| Message ['in' | 'of'] [term] arguments
| Term name arguments

Message: = Name | terminologymessage

Arguments: = (preposition expression | flag | record )*
Flag: = ('with' | 'without') [name] +

Record: = 'giveen '(name': 'expr )*

Preposition: =
'To' | 'from' | 'thru '| 'pass'
| 'By' | 'on' | 'into' | terminologypreposition

In applescript, the value referenced by an object corresponds to an object descriptor. If an operation is for Object Reference, an apple event containing the correct verb and object descriptor is created accordingly. The following is an example of Object Reference:

The first word of paragraph 22
Name of every figure of document "taxes"
The modification date of every file whose size> 1024

Note that the last example is an object reference with conditional testing. Object references are very similar to the pointer type of first-class. They can be automatically referenced or used to change related values.

Each Scriptable application has a corresponding application terminology, which defines all the events, attributes, and element names it supports. Applescript has its own terminology:

Application "name" identifies an application;
Application "appname" of Machine "machinename" identifies an application on a specific machine;

A script command can be sent to a specific application through tell:

Tell application "Excel" On Machine X
Put 3.14 into cell 1 of Row 2 of window 1
End

Because spreadsheet terms in Microsoft Excel contains Nouns such as cell, formula, and recalculate
Therefore, in the scope introduced by the statement tell application "Excel", scripts can use all Excel terms (similar to dynamically scoping ), just as they are inherent components of applescript, this is equivalent to a domain-specific extension for the basic programming concepts of applescript.

The Parser of applescript must be able to process the language's built-in facilities and the glossary of the application. The term set can contain names composed of multiple words, which means that the lexical analyzer must be able to recognize a complete logical identifier composed of multiple words. Therefore, the lexical analyzer depends on the state of the syntax Parser: when it enters a tell scope, the token table is extended and reset when it leaves.

Applescript has a simple object model. A script object contains properties and methods ). The method is dynamically distributed (dynamically dispatched), so it supports Simple object-oriented programming. The following is an example:

Script counter
Property count: 0
To Increment
Set count to count + 1
Return count
End Increment
End Script

The definition syntax of the method is similar to the logo. When the above script is executed, a new script object is bound to the counter name. The method definition can also contain the script definition to create multiple instances. This method is the factory method ). Because the variables accessible by scripts are lexically scoping, all objects created by a factory method can access the internal status of the objects to which the factory method belongs, this mechanism is very similar to the class/metaclass mechanism of smalltalk. The object model of applescript is similar to self based on prototype, and supports single inheritance by entrusting unprocessed commands to the parent attribute values of the object.

Some applications can generate corresponding applescript programs by recording users' operations on the GUI. For example, if you select the File Open menu and select a "resume" file under the "personal" directory, the corresponding Apple event is a fileopen event with the path parameter "Personal: Resume. Obviously, an apple event is usually composed of multiple underlying events (such as mouse clicks, such records are more advanced and abstract than those that only record low-level messages such as the mouse and keyboard, and each user operation must be described using the apple event, so it also makes the record program much more complicated. For this reason, the article proposes an architecture method: an application should be divided into two parts: Gui and backend, which communicate only through Apple Events. Gui translates low-level user operations (keyboard and mouse) into Apple Events and sends them to the backend. Programs that adopt this architecture are inherently record-recorded. Apple event manager only needs to record all events sent from the GUI to the backend.

The most interesting part of applescript is its syntax similar to that of a natural language. However, at the end of this article, the author thinks that this syntax has disrupted the thinking of many developers. My thinking has been stirred up by the author's conclusion.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.