Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternatives for CGEventPostToPSN() for activating an other app's menu item in a sandboxed environment?

I have this app where I need to activate an other app's menu item (like Print cmd+p) from within my app. Right now I'm using CGEventPostToPSN() to do the job and it works fine, but when I activate sandboxing, it stops working.

CGEventPostToPSN(&psn, keyDownEvent);
CFRelease(keyDownEvent);
CGEventPostToPSN(&psn, keyUpEvent);
CFRelease(keyUpEvent);

My question now is what can I do when I need to enable sandboxing? I heard a lot about the Accessibility API but I was unable to find out how I can activate an other app's menu item. I'm thankful for any answer on this problem.

Fabian

like image 984
Fabian Kreiser Avatar asked Nov 03 '11 10:11

Fabian Kreiser


1 Answers

In a sandboxed environment, you can still send Apple Events to another application—you just need to apply for an "Apple Event Temporary Exception" for your app. This is detailed in Apple's Element Key Reference in the section "Apple Event Temporary Exception".

Apple Event Temporary Exception
When you adopt App Sandbox, your app retains the ability to:
● Receive Apple events
● Send Apple events to itself
● Respond to Apple events it receives
However, with App Sandbox you cannot send Apple events to other apps unless you configure the apple-events temporary exception entitlement.

For each app you want to send Apple events to, specify the app’s bundle identifier, in all lowercase characters, as a string value for this entitlement key’s value array. For example, to enable sending Apple events to iPhoto from your app, use the string value com.apple.iphoto.

Entitlement key Capability
com.apple.security.temporary-exception.apple-events Enables sending of Apple events to one or more destination apps.

As far as I am aware, Apple have not explicitly defined 'temporary' in this context, but the consensus is that Apple are allowing this for now but that some future app store or Mac OS X update may remove the ability to do this.

Note that you have to explicitly state in advance the apps that you need to be able to send events to—this cannot be dynamically selected at runtime by your app or the user.

like image 151
Duncan Babbage Avatar answered Oct 18 '22 20:10

Duncan Babbage