Cocoa applescript applet что это
AppleScript in Lion
The focus of AppleScript in Lion is about bringing the power of AppleScript/Objective-C to the desktop.
Cocoa-AppleScript Applets. The AppleScript Editor can now create and edit Cocoa-AppleScript applets that provide direct access to the Cocoa frameworks from within their AppleScript code, by using the AppleScript/Objective-C bridge first introduced in Snow Leopard. Accordingly, applets can now display custom interfaces, such as progress windows, programmatically, taking advantage of the standard Cocoa interfaces available in the OS.
Script Templates. New for AppleScript Editor is a template menu, containing a variety of useful script templates, including scripts for creating file processing droplets, Aperture import actions, Mail rule actions, and iChat message responders.
Global Script Application Targets. In addition, the AppleScript Editor now has the ability to globally target a script to a specific application through a new optional “tell application” popup menu. Targeted scripts are excellent for quick development as they do not require the use of tell blocks or using terms clauses.
Cocoa-AppleScript Applets
AppleScript in Mac OS X v10.6 (Snow Leopard) introduced AppleScript/Objective-C and the ability to create AppleScript applications in Xcode that could directly access any part of the Cocoa frameworks. In Mac OS X v10.7 (Lion) the power of AppleScript/Objective-C has been made accessible to scripters with the introduction of Cocoa-AppleScript Applets in the AppleScript Editor.
For the first time, AppleScript can access the Cocoa Foundation and AppKit frameworks directly within applet code, augmenting AppleScript’s legendary ability to communicate with and control applications, with Cocoa’s powerful routines for text, number, window, and image manipulation. Combine this new Cocoa integration with AppleScript’s existing access to the UNIX command-line, and you’ve got a fantastic automation powerhouse.
Selecting the Cocoa-AppleScript Applet script template from the AppleScript Editor’s New From Template menu.
Cocoa-AppleScript Applet Template
The new Cocoa-AppleScript applet is available as a script template via the AppleScript Editor’s New From Template menu. Once selected, you will be prompted to name and save the new applet. This applet accepts most standard AppleScript constructs and code, as well as statements and routines written in AppleScript/Objective-C.
Using AppleScript/Objective-C in an AppleScript applet, to transform text to uppercase.
Caveats
AppleScript/Objective-C Updates
In Mac OS X Lion, two important changes were made to AppleScript/Objective-C
Script Templates
Finally, an easy way to quickly create your favorite types of scripts! AppleScript Editor in Lion introduces a New from Template menu that contains a variety of useful script templates, including templates for creating Mail rule actions, iChat response scripts, Aperture import actions, and file processing droplets. You can add your own templates by placing the template scripts in the Home > Library > Application Support > Script Editor > Templates folder.
Selecting an iChat template from the AppleScript Editor New from Template menu.
Selecting a template from the menu will prompt you for a name and location for the duplicated template file. Once provided the duplicated script file will open in the AppleScript Editor:
An iChat event handler script for automatically responding to specified buddies.
Global Script Application Targets
For scripters wanting a simple way to target scripts at a particular application, AppleScript Editor in Lion implements a target application menu, beneath the toolbar in a script window, from which you can select the application that will be the target of all the script statements.
This feature can be enabled in the AppleScript Editor editing preference pane by selecting the checkbox titled: Show tell application pop-up menu
When a script is targeted at a specific application, you don’t need to include tell blocks or tell statements for the targeted applicaiton.
LEARN APPLESCRIPT/OBJ-C
AppleScriptObjC Explored by Shane Stanley is the definitive resource on AppleScript Objective-C.
In addition to documenting how to build applications using AppleScriptObjC, this PDF book comes with fully-editable and annotated example projects for you to explore in detail.
COCOA-APPLESCRIPT EXAMPLES
The AppleScript Editor application in Mac OS X v10.7 (Lion) comes with a set of Cocoa-AppleScript example templates. You can download these additional examples and make them available in the AppleScript Editor by placing them in the Home > Library > Application Support > Script Editor > Templates folder.
RELATED LINKS
Developer documentation for some of the Cocoa classes available for use with the Cocoa-AppleScript applets:
APPLESCRIPT ACTIONS
Two Xcode projects have been posted demonstrating how to use the new AppleScript/Objective-C templates to creating Automator actions using lists. The automated publishing example can be viewed here.
Скриптинг для Mac OS X: начинаем программировать на AppleSсriрt
Содержание статьи
Из этой статьи ты узнаешь, что такое AppleScript, зачем и кому он нужен, как можно автоматизировать чужие приложения и добавлять возможность автоматизации в свои.
Автоматизируй это
Часто встречаются такие задачи, для решения которых делать отдельный проект на компилируемом языке нерационально. Например, когда нужно быстро слепить на коленке код, который должен просто выполнять конкретную работу — без всяких GUI-украшений, обработки всевозможных исключительных ситуаций, оптимизации и прочего. Здесь на помощь и приходят языки сценариев — известные тебе shell, Perl, PHP и так далее. Все они (ну или почти все) доступны и под Mac OS X. Но в этой операционке в дополнение к общепринятым скриптовым языкам есть и специальный язык сценариев, ориентированный именно на Mac OS X и тесно с ней связанный. Это AppleScript.
AppleScript идет вместе с системой начиная с System 7. Выросший из проекта HyperCard (который содержал скриптовый язык HyperTalk, очень похожий на естественный английский), AppleScript первоначально создавался для того, чтобы обеспечить обмен данными между задачами, а также для управления работой сторонних приложений. Сам по себе AppleScript обладает довольно скромной функциональностью: на этом языке даже сценарии для выполнения сравнительно простых задач часто выглядят как обращение к другим приложениям. Впрочем, после существенной перестройки системы при переходе к линейке Mac OS X язык AppleScript стал боле гибким и мощным, а новый фреймворк Cocoa позволил разработчикам встраивать в свои приложения возможность автоматизации с помощью AppleScript’а с минимальными усилиями.
Простой сценарий
Для редактирования и исполнения скриптов мы будем использовать стандартный Script Editor. Найти его можно в папке /Application/AppleScript. Для начала напишем простой «HelloWorld’ный» скрипт.
display alert «Hello World!» # Покажем диалог
say «Hello World» # Вывод в колонки
Объяснять тут, думаю, ничего не нужно, но хочется отметить крайне простой доступ к синтезатору речи из AppleScript c помощью команды say. Вот это и есть настоящее общение с пользователем в стиле Apple :). Конечно же, этот диалог можно легко кастомизировать. Например, добавить нужные кнопки:
Панель с дополнительными кнопками
display alert «Hello World!» buttons <"Hello", "Bye">
set answer to button returned of the result
if answer is «Hello» then
.
else
.
end if
Теперь напишем что-нибудь более полезное. Например, дадим пользователю выбрать файл и прочитаем его содержимое:
# Панель выбора файла
set theFile to (choose file with prompt «Select a file to read:» of type <"TEXT">)
open for access theFile
Читаем контент
set fileContents to (read theFile)
close access theFile
На этих примерах хорошо видна основная идея AppleScript — он очень близок к живому английскому языку. Поэтому читать скрипты легко даже для человека, далекого от кодинга. Каждая командаглагол может быть дополнена существительными-модификаторами и параметрами.
Взаимодействие c приложениями
Для взаимодействия с другими приложениями AppleScript использует механизм сообщений:
tell application «Microsoft Word»
quit
end tell
C помощью команды tell выбираем приложение, которому мы будем отправлять сообщение. В данном случае мы просим MS Word завершиться. В блоке «tell — end tell» может быть отправлено любое количество команд. Сообщения, которые отсылаются приложению, могут быть и более специфичными. Все зависит от того, какие команды реализовали его разработчики. iTunes, например, экспортирует довольно много команд и свойств в среду AppleScript:
Запускаем нужный плейлист в iTunes
tell application «iTunes»
play the playlist named «My Favorite»
end tell
Чтобы тебе было проще работать с классами и командами, которые экспортирует приложение, они организованы в разделы. Все приложения, которые поддерживают скриптинг, имеют хотя бы два раздела: один стандартный и один из более специфичных для данного приложения разделов. Стандартный раздел содержит набор стандартных команд, которые поддерживает любое Mac-приложение: open, print, close и quit. Содержание остальных разделов зависит от фантазии разработчиков.
Выполнение AppleScript из своего приложения
Если ты пишешь приложение на Objective-C/Cocoa, то возможно, что некоторые вещи будет удобнее сделать с помощью AppleScript. Для создания и выполнения в Cocoa-приложениях скриптов существует класс NSAppleScript. Вот простой пример его использования — реализация получения у приложения iChat строки статуса пользователя.
NSAppleScript *iChatGetStatusScript = nil;
iChatGetStatusScript = [[NSAppleScript alloc] initWithSource: @»tell application «iChat» to get status message»];
NSString *statusString = [[iChatGetStatusScript executeAndReturnError:&errorDict] stringValue];
Возможно, то же самое можно сделать и другим путем, не используя созданный во время выполнения скрипт, но вряд ли альтернативный код будет выглядеть проще, чем этот. Если скрипты большие, можно хранить их в ресурсах бандла и читать при необходимости.
Автоматизация в Cocoa-приложении
Содержимое файла scriptTermonology отображается в Script Editor’е при просмотре словаря приложения. Этот файл содержит описание экспортируемых в AppleScript объектов.
Открыв scriptSuite-файл в Plist Editor’е, можно видеть, что он содержит следующие основные разделы:
Разбирать внутреннее устройство этих файлов особого смысла нет, так как тебе скорее всего придется иметь дело только с sdef-файлами.
Пример sdef-файла
.
NSAppleScriptEnabled
Scrtipting.sdef
Добавим к проекту файл Scripting.sdef следующего содержания:
Итак, из AppleScript’а у нас доступно одно свойство — myprop. Осталось написать ObjC-код, который будет обрабатывать чтение данного свойства из скриптов. Для этого нужно создать категорию NSApplication, поскольку именно этот класс мы выбрали в качестве получателя сообщений от скриптов.
Eсли мы теперь из AppleScript обратимся к свойствам нашего приложения, то увидим среди них свое свойство и его значение:
tell application «Scripting»
properties
end tell
Заключение
Конечно, описать здесь все возможности AppleScript и его взаимодействия с Cocoa-приложениями невозможно. Да это и не нужно — для этого есть мануалы. А мы со своей стороны продолжим цикл статей о кодинге под эппловские платформы и расскажем тебе еще много нового и интересного.
AppleScript in Lion
The focus of AppleScript in Lion is about bringing the power of AppleScript/Objective-C to the desktop.
Cocoa-AppleScript Applets. The AppleScript Editor can now create and edit Cocoa-AppleScript applets that provide direct access to the Cocoa frameworks from within their AppleScript code, by using the AppleScript/Objective-C bridge first introduced in Snow Leopard. Accordingly, applets can now display custom interfaces, such as progress windows, programmatically, taking advantage of the standard Cocoa interfaces available in the OS.
Script Templates. New for AppleScript Editor is a template menu, containing a variety of useful script templates, including scripts for creating file processing droplets, Aperture import actions, Mail rule actions, and iChat message responders.
Global Script Application Targets. In addition, the AppleScript Editor now has the ability to globally target a script to a specific application through a new optional “tell application” popup menu. Targeted scripts are excellent for quick development as they do not require the use of tell blocks or using terms clauses.
Cocoa-AppleScript Applets
AppleScript in Mac OS X v10.6 (Snow Leopard) introduced AppleScript/Objective-C and the ability to create AppleScript applications in Xcode that could directly access any part of the Cocoa frameworks. In Mac OS X v10.7 (Lion) the power of AppleScript/Objective-C has been made accessible to scripters with the introduction of Cocoa-AppleScript Applets in the AppleScript Editor.
For the first time, AppleScript can access the Cocoa Foundation and AppKit frameworks directly within applet code, augmenting AppleScript’s legendary ability to communicate with and control applications, with Cocoa’s powerful routines for text, number, window, and image manipulation. Combine this new Cocoa integration with AppleScript’s existing access to the UNIX command-line, and you’ve got a fantastic automation powerhouse.
Selecting the Cocoa-AppleScript Applet script template from the AppleScript Editor’s New From Template menu.
Cocoa-AppleScript Applet Template
The new Cocoa-AppleScript applet is available as a script template via the AppleScript Editor’s New From Template menu. Once selected, you will be prompted to name and save the new applet. This applet accepts most standard AppleScript constructs and code, as well as statements and routines written in AppleScript/Objective-C.
Using AppleScript/Objective-C in an AppleScript applet, to transform text to uppercase.
Caveats
AppleScript/Objective-C Updates
In Mac OS X Lion, two important changes were made to AppleScript/Objective-C
Script Templates
Finally, an easy way to quickly create your favorite types of scripts! AppleScript Editor in Lion introduces a New from Template menu that contains a variety of useful script templates, including templates for creating Mail rule actions, iChat response scripts, Aperture import actions, and file processing droplets. You can add your own templates by placing the template scripts in the Home > Library > Application Support > Script Editor > Templates folder.
Selecting an iChat template from the AppleScript Editor New from Template menu.
Selecting a template from the menu will prompt you for a name and location for the duplicated template file. Once provided the duplicated script file will open in the AppleScript Editor:
An iChat event handler script for automatically responding to specified buddies.
Global Script Application Targets
For scripters wanting a simple way to target scripts at a particular application, AppleScript Editor in Lion implements a target application menu, beneath the toolbar in a script window, from which you can select the application that will be the target of all the script statements.
This feature can be enabled in the AppleScript Editor editing preference pane by selecting the checkbox titled: Show tell application pop-up menu
When a script is targeted at a specific application, you don’t need to include tell blocks or tell statements for the targeted applicaiton.
LEARN APPLESCRIPT/OBJ-C
AppleScriptObjC Explored by Shane Stanley is the definitive resource on AppleScript Objective-C.
In addition to documenting how to build applications using AppleScriptObjC, this PDF book comes with fully-editable and annotated example projects for you to explore in detail.
COCOA-APPLESCRIPT EXAMPLES
The AppleScript Editor application in Mac OS X v10.7 (Lion) comes with a set of Cocoa-AppleScript example templates. You can download these additional examples and make them available in the AppleScript Editor by placing them in the Home > Library > Application Support > Script Editor > Templates folder.
RELATED LINKS
Developer documentation for some of the Cocoa classes available for use with the Cocoa-AppleScript applets:
APPLESCRIPT ACTIONS
Two Xcode projects have been posted demonstrating how to use the new AppleScript/Objective-C templates to creating Automator actions using lists. The automated publishing example can be viewed here.
Создание программ для Mac OS X. Часть 3: Apple Script
В этой части я расскажу про еще одно интересное средство разработки приложений для Mac OS X — скриптового языка Apple Script.
Apple Script разрабатывался чтобы применяться конечными пользователями, а не программистами, и позволить им контролировать приложения и документы, с которыми они работают. Например, с помощью Apple Script можно открыть фотографию в редакторе изображений, обрезать до нужного размера, записать ссылку на фото в текстовый файл и т.п.
В отличии от того, как пользователь через GUI взаимодействует с приложением, например впечатывает информацию в текстовые поля приложения для работы с базами данных, AppleScript работает совсем иначе, скрипт использует внутреннюю объектную модель приложения, тем самым внося значения в саму базу данных. Это означает, что во время работы скрипта приложение даже не обязательно показывать. Естественно такая модель работы требует того, чтобы ваше приложение было написано с поддержкой Apple Script.
Каждое приложение, которое понимает Apple Script, публикует поддерживаемые комманды в Apple Event словаре, который используется, чтобы определить допустимые комманды.
Язык, очень похожий на натуральный
в подкасте Радио-Т ведущий bobuk говорил что скрипт, написанный на языке Apple Script читается как обычный английский текст. В принцие он прав, т.к. это и есть одна из основополагающих особеннойстей Apple Script.
Движок Apple Script комбинирует глаголы и существительные, чтобы выполнить действия. Например, чтобы напечатать документ, страницу из документа или опеределенный фрагмент, вместо вызова функций printPage, printDocument, printRange, мы берем глагол print и добавляем нужное существительное:
print page 1
print document 2
print pages 1 thru 5 of document 2
tell application «iTunes»
playpause
end tell
tell application «Microsoft Word» to quit
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
Использовать иерархичность можно следующим образом:
pixel 7 of row 3 of TIFF image «my bitmap»
set pix to 72
set answer to text returned of (display dialog «Enter in the number of inches» default answer «1»)
display dialog answer & «in = » & (answer * pix) & «px»
при исполнии покажет диалоговое окно с запросом ввода кол-ва дюймов, потом это значение конвертируется в пиксели и результат показывается в следующем окне.
Также скрипт можно сохранить чтобы в дальнейшем использовать как полноценное приложение. Обработчик события запуска должен находиться внутри следующей конструкции:
В принципе, его можно и не писать, тогда при запуске обработка скрипта начнется с первой строки в файле.
Если бросить на файл со скриптом пару других файлов, то при запуске будет использован следующий обработчик:
on open theItems
— что-нибудь делаем с этими самыми theItems
end open
Средстав для написания скриптов лежат в /Applications/AppleScript
В качестве редактора/интерпритатора исаользуется ScriptEditor.app
Чтобы открыть Apple Event словарь надо в меню Script Editor нажать File=> Open Dictionary, откроется окно со списком приложений
выбираем нужное(например iTunes) и клацаем OK. Открылось окно с описанием комманд для нужного нам приложения
А теперь напишем простенькое Cocoa приложение, с помощью которого можно контроллировать iTunes
Открываем XCode, File=>New Project, Cocoa Application => в Project Name пишем iTunes_Controller. Добавляем новый Objective-C class «controller». В файле controller.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
В принципе, в этом коде ничего секретно-военного нету. Просто пишем класс, с помощью которого и будем контролировать iTunes. Функции *Click — это обработчики нажатия по соответствующим кнопочкам на форме. А executeAppleScript:(NSString*)sctript — функция, которая будет исполнять скрипт в параметре script.
А теперь клацаем по MainMenu.nib и попадаем в InterfaceBuilder. Добавляем в окно MainMenu.nib новый NSObject и назначем ему класс controller:
Затем разместим на форме четыре кнопки с надписями «next»,«play»,«pause»,«previous» и соеденим их с соотв. обработчиками из класса controller:
Сохраняем все и возвращаемся в XCode. В файле controller.m пишем следующее:
— (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»];
>
В принципе в этом коде ничего сложного тоже нет. Единственная интересная вещь — класс NSAppleScript, с помощью объекта которого и выполняем скрипт. Подробнее про NSAppleScript можно почитать здесь.
В принципе, я думаю, что этого достаточно чтобы получить общее представление о Apple Script и попробавть что-нибудь на нем написать.