从沙盒可可应用发送按键到最前端的应用 [英] Send keystrokes to frontmost app from sandboxed cocoa app

查看:125
本文介绍了从沙盒可可应用发送按键到最前端的应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从我的cooca应用程序向大多数应用程序发送击键。

I need to send keystrokes to front most app from my cooca app.

通过使用 CGEventCreateKeyboardEvent(),我已经有了工作代码。 AXUIElementPostKeyboardEvent(),但仅在未对应用程序进行沙箱处理时才有效。

I already have working code for it by using CGEventCreateKeyboardEvent() and AXUIElementPostKeyboardEvent(), but it only works if app is not sandboxed.

我在google上搜索了相同的内容,但没有找到任何可行的解决方案。

I have searched google for the same, but didn't find any working solution.

我看到一个Text应用程序和其他一些应用程序在沙盒环境中也做同样的事情,所以我想知道,是否有人可以帮助我确定aText.app和其他人如何在沙盒环境中发送击键。

I saw that a Text app and few others doing the same thing in sandboxed environment, so i am wondering, if someone help me to figure out, that how aText.app and others are able to send keystrokes in sandbox environment.

谢谢

推荐答案

这实际上是可能的。我在此处提供了一个示例应用程序- GitHub上的SendKey

This is actually possible. I have made an example app available here - SendKey at GitHub

我走了一条简单的路,开始了一个简单的AppleScript:

I took the easy road and started with a simple AppleScript:

delay 5

tell application "System Events"
    repeat 10 times
        keystroke "#"
    end repeat
end tell

脚本中的延迟仅给了我足够的时间使文本编辑器成为最前端的应用程序。我建议从运行此脚本开始,以查看其功能。

The 'delay' in the script simply gives me enough time to make a text editor the frontmost application. I would suggest starting with just running this script to see what it does.

然后,我使用默认的Application模板创建了一个Xcode项目,并写道:

Then, I created an Xcode project using the default Application template and wrote:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    NSString*       scriptPath  = [[NSBundle mainBundle] pathForResource:@"sendkey" ofType:@"scpt"];
    NSURL*          scriptURL   = [NSURL fileURLWithPath:scriptPath];
    NSDictionary*   errors;
    NSAppleScript*  script      = [[NSAppleScript alloc] initWithContentsOfURL:scriptURL error:&errors];

    NSLog( @"%@", errors );

    [script executeAndReturnError:&errors];

    NSLog( @"%@", errors );
}

我在未启用沙箱的情况下对其进行了测试,以验证其是否正常运行。然后我打开了沙箱,当然,它坏了。但是,幸运的是,有一种解决方法。目前,Apple提供了名为 com.apple.security.temporary-exception.apple-events 。而且,您可以请求为com.apple.systemevents授予例外。这是我的权利文件:

I tested this without turning on sandboxing to verified it works and it did. Then I turned on Sandboxing and, of course, it broke. But, fortunately, there is a way around that. For now, Apple is providing a temporary entitlement called com.apple.security.temporary-exception.apple-events. And, you can request the exception be granted for 'com.apple.systemevents'. This is what my entitlements file looks like:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
   "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>com.apple.security.temporary-exception.apple-events</key>
    <array>
      <string>com.apple.systemevents</string>
    </array>
    <key>com.apple.security.app-sandbox</key>
    <true />
  </dict>
</plist>

一旦我将此权利添加到我的沙盒应用程序并签名,它就会再次按预期工作。

Once I added this entitlement to my sandboxed app and signed it, it worked as expected again.

现在,如果您要发送其他密钥,则此问题&答案将演示如何动态构建脚本-将变量传递给applescript

Now, if you want to send other keys, this question & answer will demonstrate how to build your script on the fly - Passing variables to an applescript.

当然,一旦所有这些工作完成,您可能可以使用NSAppleEventDescriptor和相关类在代码中构建事件,但是我没有玩过

Of course, once you have all of these working, you can probably turn to NSAppleEventDescriptor and related classes to build the event in code, but I haven't played with that technique.

请注意,在使用临时授权时,Apple确实建议您执行以下操作:

Please note that Apple does suggest you do the following when using a temporary entitlement:


如果您选择不立即沙盒您的应用程序或使用临时的
例外权利,请使用Apple的错误报告系统让Apple
知道您遇到的问题。苹果公司在开发OS X平台时认为
是功能请求。另外,请确保使用iTunes Connect中的
评论注释字段来解释为什么需要
例外。

If you choose not to sandbox your app now or to use a temporary exception entitlement, use Apple’s bug reporting system to let Apple know about the issue you are encountering. Apple considers feature requests as it develops the OS X platform. Also, be sure use the Review Notes field in iTunes Connect to explain why the exception is needed.

这篇关于从沙盒可可应用发送按键到最前端的应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆