JavaScript for Automationの基礎
JavaScript for Automationとは何か
このAppleのドキュメント『JavaScript for Automation Release Notes』によれば:
JavaScript for Automation provides the ability to use JavaScript for interapplication communication between apps in OS X.[1]
JavaScript for Automation is a host environment for JavaScript[2]
The JavaScript OSA component implements JavaScript for Automation.[3]
略称は「JXA」である。[1:1]
-
https://developer.apple.com/library/archive/releasenotes/InterapplicationCommunication/RN-JavaScriptForAutomation/Articles/Introduction.html ↩︎ ↩︎
-
https://developer.apple.com/library/archive/releasenotes/InterapplicationCommunication/RN-JavaScriptForAutomation/Articles/OSX10-10.html#//apple_ref/doc/uid/TP40014508-CH109-SW2 ↩︎
-
https://developer.apple.com/library/archive/releasenotes/InterapplicationCommunication/RN-JavaScriptForAutomation/Articles/OSX10-10.html#//apple_ref/doc/uid/TP40014508-CH109-SW3 ↩︎
Object Specifierなるもの
Many objects in the JavaScript for Automation host environment refer to external entities, such as other applications, or windows or data inside of those other applications. When you access a JavaScript property of an application object, or of an element of an application object, a new object specifier is returned that refers to the specific property of that object.[1]
[抄訳]
JavaScript for Automation host環境における多くのオブジェクトは、外部にある実体を参照する。
Note: An object specifier is not the actual value of the external entity's property; it is a reference to the object.[1:1]
Object Specifierの分類
Types of Terminology
The navigation pane of a dictionary includes the types of terms described in Table 12-1.
Type Icon Description Suite A suite is a grouping of related commands and classes. Apps often have a Standard Suite, which includes terminology supported by most scriptable apps, such as an open
command, aquit
command, and anapplication
class.Command A command is an instruction that can be sent to an app or object in order to initiate some action. For example, delete
,make
,Class A class is an object within an app, or an app itself. Mail, for example, has an application
class, amessage
class, and asignature
class, among others. Objects within an app sometimes contain other objects as elements. For example, in Mail, amailbox
objects can containmessage
objects as elements.Property A property is an attribute of a class. For example, the message
class in Mail has many properties, includingdate received
,read status
, andsubject
. Some properties are read-only, while others are readable and writable.
Object Specifierの継承と包含
Key Concepts
It’s important to understand the concepts of inheritance and containment when navigating a scripting dictionary.
Inheritance
In a scriptable app, different classes often implement the same properties. For example, in Finder, the
file
andfolder
classes both havecreation date
,modification date
, andname
properties. Rather than defining these same properties multiple times throughout the scripting dictionary, Finder implements a genericitem
class. Since files and folders are considered types of Finder items, these classes inherit the properties of theitem
class. In other words, any properties of theitem
class also apply to thefile
andfolder
classes. There are many other items in Finder that also inherit these same properties, including thedisk
,package
, andalias file
classes.A class that inherits the properties of other classes can also implement its own properties. In Finder, the
file
class implements a number of file-specific properties, includingfile type
andversion
. Thealias file
class implements anoriginal item
property.In some cases, a class inherits properties from multiple classes. In Finder, an alias is a type of file, which is a type of item. Therefore, the
alias file
class inherits the properties of both thefile
anditem
classes, as shown in Figure 12-2.Figure 12-2 In scripting, classes can inherit the properties of other classes
Containment
Classes of a scriptable app reside within a certain containment hierarchy. The application is at the top level, with other classes nested beneath. Finder, for example, contains disks, folders, files, and other objects. While files don’t contain elements, folders and disks can contain other folders and files. See Figure 12-3. Mail is another example—the application contains accounts, which can contain mailboxes, which can contain other mailboxes and messages.
Figure 12-3 In scripting, classes can contain other classes as elements
When referencing a class, you must do so very specifically according to its containment hierarchy in order to provide the scripting system with context. To reference a file in Finder, you would specify where the file resides in the folder hierarchy. See Listing 12-1 and Listing 12-2. To reference a message in Mail, you would specify where the message resides in the mailbox and account hierarchy.
APPLESCRIPT
Listing 12-1 AppleScript: Referencing a file by containment hierarchy in Finder
tell application "Finder" modification date of file "My File.txt" of folder "Documents" of folder "YourUserName" of folder "Users" of startup disk end tell --> Result: date "Monday, September 28, 2015 at 10:10:17 AM"
JAVASCRIPT
Listing 12-2 JavaScript: Referencing a file by containment hierarchy in Finder
var Finder = Application("Finder") Finder.startupDisk.folders["Users"].folders["YourUserName"].folders["Documents"].files["My File.txt"].modificationDate() // Result: Mon Sep 28 2015 17:10:17 GMT-0700 (PDT)