Inter-Process Communication on Mac – Know How part3 (Apple Events)
In Part 1 of this blog series we discussed about various techniques available for Inter-Process Communication (IPC) on Mac OS X.
As discussed in the previous post Apple Events are the only IPC mechanism which is universally supported by GUI applications on Mac OS X for remote control. Operation like opening a application or telling a application to open a file or to quit etc. can be done using these.
AppleScript is a scripting language built on top of Apple Events which can be used using scripting bridge in a Mac application.
Here in this blog we will see the implementation of IPC using apple events.
An Apple event is the basic message for IPC in Open Scripting Architecture(OSA). In OSA communication takes place through exchange of Apple events.
Cocoa provides automatic handling for some Apple events that all applications can receive irrespective of whether the application is scriptable or not.
All mac applications having GUI are able to respond to certain apple events sent by OS. These are known as required events.
The processing of Apple events is done as follows: (more Info)
1. For the expected apple event application registers callback routines (Apple event handler) with Apple Event Manager.
2. When application receives Apple event it dispatch the event to appropriate handler using Apple Event Manager.
To perform IPC using Apple events, we can use AppleScripts. An AppleScript script consists of one or more statements, written in a syntax described in AppleScript Language Guide.
Some example of simple AppleScript script are as follows :
/*Tell a application to launch*/
tell application “Finder” to open application file (POSIX file “/Applications/Test.app”)
/*Tell TextEdit to modify font and text color in specified document*/
tell document “Intro” of application “TextEdit”
set size of (words where it = “IPC”) to 20
set color of (words where it = “AppleEvent”) to “red”
/*Tell Finder to close front window*/
tell application “Finder”
set frontWin to name of front window
close window frontWin
We can use other scripts system (like shell scripts) in AppleScript as follows :
/*Use Shell script in AppleScript script to unzip a file*/
set zipFilePath to POSIX path of “/Users/user/Desktop/Test.zip”
set dirpath to POSIX path of “/Users/user/Desktop”
do shell script “/usr/bin/unzip -d ” & quoted form of dirpath & ” ” & quoted form of zipFilePath
We can use AppleScript with Objective-C code using Scripting bridge.Scripting Bridge,provides an automated process for creating an Objective-C interface to scriptable applications. This allows Cocoa applications and other Objective-C code to efficiently access features of scriptable applications, using native Objective-C syntax. For more info on scripting bridge please see Scripting Bridge Programming Guide.
Posted on November 4, 2014, in Cooca, Inter-Process Communication (IPC), Objective-C, Scripting and tagged Apple Events, AppleScript, Hem Dutt, MAC OSX, Mindfire Solutions, Objective-C, Xcode. Bookmark the permalink. 3 Comments.