Question

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

Was it helpful?

Solution 2

I still don't have a final answer to this question, but it seems to be possible with the Accessibility API as suggested by @nielsbot.

There are some promising functions like AXUIElementPostKeyboardEvent() and AXUIElementPerformAction(). Problem: Using the Accessibility API to control another app is prohibited in Apple's Sandbox Guideline.

Note: With App Sandbox, you can and should enable your app for accessibility, as described in this document. However, you cannot sandbox an assistive app such as a screen reader, and you cannot sandbox an app that controls another app.

If you came across this question and have the same problem as me, I fear you're out of luck. Reconsider if you really have to control another app and if so, you might not be able to distribute your app on the Mac App Store, sorry.

Fabian

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top