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

不羁岁月 提交于 2019-12-09 14:11:12

问题


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


回答1:


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.




回答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



来源:https://stackoverflow.com/questions/7993062/alternatives-for-cgeventposttopsn-for-activating-an-other-apps-menu-item-in-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!