📜 ⬆️ ⬇️

Creating programs for Mac OS X. Part 3: Apple Script

In this part, I will talk about another interesting application development tool for Mac OS X - the Apple Script scripting language.

Apple Script was designed to be used by end users, not programmers, and to allow them to control the applications and documents they work with. For example, using Apple Script, you can open a photo in the image editor, crop it to the desired size, write a link to the photo in a text file, etc.


Unlike how the user interacts with the application through the GUI, for example, typing information into the application text fields for working with databases, AppleScript works quite differently, the script uses the internal object model of the application, thereby adding values ​​to the database itself. This means that while the script is running, the application does not even need to be shown. Naturally, such a model of work requires that your application be written with support for Apple Script.
')
Every application that understands Apple Script publishes supported commands to the Apple Event dictionary, which is used to identify valid commands.

A language very similar to natural


In the Radio-T podcast, the host bobuk said that the script written in Apple Script is read like ordinary English text. In principle, he is right, because this is one of the fundamental features of Apple Script.

The Apple Script engine combines verbs and nouns to perform actions. For example, to print a document, a page from a document or a specific fragment, instead of calling the functions printPage, printDocument, printRange, we take the verb print and add the required noun:
print page 1
print document 2
print pages 1 thru 5 of document 2

To work with a specific application, the word “tell” is used.
tell application "iTunes"
playpause
end tell

The previous expression can be written in one line.
tell application "Microsoft Word" to quit

For events from the "Core Suite" (activate, open, re-open, close, print, exit) you can use the following instructions:
quit application "iPhoto"

Constructs can be nested:
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


You can use hierarchy as follows:
pixel 7 of row 3 of TIFF image "my bitmap"

But Apple’s capabilities are not limited to managing other applications. With it, you can create a normal Cocoa application with a graphical interface, etc. For example, the following code:
set pix to 72
set answer to text returned for display dialog "Enter in the number of inches" default answer "1")
display dialog answer & "in =" & (answer * pix) & "px"

when executed, it will show a dialog box asking for the input of a count of inches, then this value is converted into pixels and the result is shown in the next window.

Also, the script can be saved to continue to use as a full application. A trigger event handler must be inside the following construct:
on run
- handler code
end run

In principle, it is possible not to write it, then at the start script processing will start from the first line in the file.

If you throw a couple of other files on a script file, the following handler will be used at startup:
on open theItems
- do something with these theItems
end open


Scentav for writing scripts are in / Applications / AppleScript

As an editor / interpreter, use ScriptEditor.app

To open the Apple Event dictionary, in the Script Editor menu, click File => Open Dictionary, a window opens with a list of applications
Select the right one (for example, iTunes) and click OK. A window opens with a description of the commands for the application we need.

And now let's write a simple Cocoa application with which you can control iTunes.
Open XCode, File => New Project, Cocoa Application => in the Project Name write iTunes_Controller. Add a new Objective-C class "controller". In the file controller.h we write
#import <Cocoa / Cocoa.h>

interface controller: NSObject {
}
- (IBAction) nextClick: (id) sender;
- (IBAction) prevClick: (id) sender;
- (IBAction) pauseClick: (id) sender;
- (IBAction) playClick: (id) sender;
- (void) executeAppleScript: (NSString *) sctript;
end

In principle, there is nothing secret military in this code. Just write a class with which we will control iTunes. * Click functions are handlers for clicking the corresponding buttons on the form. And executeAppleScript: (NSString *) sctript is a function that will execute the script in the script parameter.

And now we click on MainMenu.nib and get into InterfaceBuilder. Add a new NSObject to the MainMenu.nib window and assign the controller class to it:

Then we place on the form four buttons labeled “next”, “play”, “pause”, “previous” and connect them with the corresponding. handlers from the controller class:

Save everything and return to Xcode. In the file controller.m we write the following:
- (void) executeAppleScript: (NSString *) script {
try {
NSAppleScript * ascript = [[NSAppleScript alloc] initWithSource: script];
[ascript executeAndReturnError: nil];
[ascript release];
}
catch (NSException * e) {
NSLog (@ "exception:% @", e);
}
}
- (void) nextClick: (id) sender {
[self executeAppleScript: @ "tell application \" iTunes \ "\ n next track \ n end tell"];
}
- (void) prevClick: (id) sender {
[self executeAppleScript: @ "tell application \" iTunes \ "\ n previous track \ n end tell"];
}
- (void) playClick: (id) sender {
[self executeAppleScript: @ "tell application \" iTunes \ "\ n play \ n end tell"];
}
- (void) pauseClick: (id) sender {
[self executeAppleScript: @ "tell application \" iTunes \ "\ n pause \ n end tell"];
}

In principle, there is nothing complicated in this code either. The only interesting thing is the NSAppleScript class, with the help of which object we execute the script. You can read more about NSAppleScript here .

Basically, I think this is enough to get a general idea of ​​Apple Script and add something to it.

PS: what else would be interesting to read in terms of creating applications?
PPS: I noticed that my articles are copied on other resources (thank God, at least the link to Habr is left). I certainly do not mind, but if copy-paste, then please, inform me about it (by email, habraposht or write in kamentah)

Source: https://habr.com/ru/post/23312/


All Articles