Swift macOS:如何粘贴到另一个应用程序 [英] Swift macOS: How to paste into another application

查看:145
本文介绍了Swift macOS:如何粘贴到另一个应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Swift编写Mac OS 10.10+的辅助应用程序。我需要能够将常规NSPasteboard 中的内容粘贴到以前处于活动状态的应用程序中。

I'm writing an assistive application for Mac OS 10.10+, using swift. I need to be able to paste the content from the general NSPasteboard into the application which had previously been active.

只是为了使其更加清晰:我需要粘贴到另一个应用程序中。

Just to make it extra clear: I need to paste into another application.

它应该像这样工作:


  1. 用户正在使用某些随机应用程序

  1. The user is using some random application

因为用户由于残疾而无法按cmd + v ,他们做出了激活我的应用程序的手势(此部分已经完成,它不在此问题的范围之内)。

Because the user can't press cmd+v due to disability, they make a gesture which activates my app (this part is done and it's outside the scope of this question)

我的应用程序已激活,现在我需要在用户预先使用的应用中模拟粘贴操作。这就是我不知道该怎么做的地方。

My app becomes active and now I need to simulate a Paste action in the app the user has been using beforehand. This is the bit I don't know how to do.

最后,以前处于活动状态的应用程序需要再次处于活动状态。

Finally the previously active app needs to become active again.

请记住要将该应用程序提交到AppStore。

Please bare in mind the app is to be submitted to the AppStore.

推荐答案

最简单的是,您需要做两件事:

At its simplest you need to do two things:


  1. 订阅当前应用程序更改的通知,这样您就知道应该将事件发送到哪个应用程序。

  1. Subscribe to notifications of the current application changing so you know what application you should send events to.

使用系统事件模拟Cmd + V按键

但是,坏消息是,正如您所说的,您需要在App Store提交的应用程序中执行此操作,因此您的应用程序必须位于沙箱中,并且需要临时授权 com.apple.security.temporary-exception.apple-events 将事件发送到系统事件直接引用Apple文档

However, the bad news is, as you say you need to do this in an App Store submitted app, your app needs to be inside the sandbox and requires the temporary entitlement com.apple.security.temporary-exception.apple-events to send events to System Events directly quoting the Apple documentation:


请求必要的苹果-事件Finder和系统事件的临时例外权利可能会导致在应用程序审阅过程中被拒绝,因为授予对这些过程的访问权限使您的应用程序可以自由地控制大部分操作系统。对于系统级任务,请使用其他方法,如上所述。

requesting the necessary apple-events temporary exception entitlements for the Finder and System Events will likely result in rejection during the app review process, because granting access to these processes gives your app free rein over much of the operating system. For system-level tasks, use other methods, as discussed above.

还有一种方法可以使用辅助方法执行步骤2技术,但在沙箱内部也无法使用。从根本上说,您试图做的事情(激活一个外部任意应用程序,并对其进行控制)在沙盒设计用来阻止您做的事情上大有可为。

There is also a way to do the step #2 using assistive technologies, but that too will not work inside the sandbox. Fundamentally, what you are trying to do (to activate an external arbitrary app, and to control it) is pretty much bang on what the sandbox is designed to prevent you from doing.

幸运的是,自macOS 10.8起,现在有了 NSUserAppleScriptTask ,它在沙箱外部执行。

Fortunately, since macOS 10.8 there is now NSUserAppleScriptTask, which is executed outside of the sandbox.

NSUserAppleScriptTask 有一个变化:使用 NSUserAppleScriptTask 执行的脚本需要放置在应用程序的脚本目录中,您的应用程序无法对其进行写入,只能对其进行读取。

There is a twist to NSUserAppleScriptTask: scripts executed with NSUserAppleScriptTask need to be placed inside the application's script directory, and your application cannot write to it, only read from it.

此objc .io文章显示了一个示例,该示例说明如何请求对脚本目录进行安全范围内的写访问以能够将脚本写入其中;因此,对于您自己和您的用户而言,您很烦恼,第一次需要打开一个打开的对话框,并且您需要存储安全范围的书签,因此您无需重复练习,但这是您能够做到的最好的选择在沙盒内做。

This objc.io article shows an example of how you can request for security scoped writing access to the scripting directory to be able to write your script to it; Annoyingly to yourself and your user you need to bring up an open dialog on the first time, therefore, and you need to store the security scoped bookmark so you don't need to repeat the exercise, but that's the best you'll be able to do inside the sandbox.

除了跳 NSUserAppleScriptTask 篮球以外,您唯一的其他选择就是说服苹果公司,这是一个非常好的主意接受具有临时权利的应用程序,以便它可以编写系统事件脚本(我不会屏住呼吸)。

Your only other route beside jumping the NSUserAppleScriptTask hoops to my knowledge is to convince Apple that it's a really good idea to accept your app with the temporary entitlement that allows it to script System Events (I would not hold my breath).

这篇关于Swift macOS:如何粘贴到另一个应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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