Swift应用程序启动后如何发布Quartz事件? [英] How to post a Quartz Event after Swift application launch?

查看:99
本文介绍了Swift应用程序启动后如何发布Quartz事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个简单的Cocoa应用程序,该应用程序将从AppleScript启动,只是将Quartz Event发布到另一个应用程序.

I am writing a simple Cocoa app that will be launched from AppleScript just to post a Quartz Event to another app.

不需要用户界面,因此我从界面生成器"中删除了该窗口,并从应用程序代表"中删除了该窗口的出口.我从方法applicationDidFinishLaunching(_ :)调用私有方法postClickEvent().我在mouseCursorPosition(x:0,y:0)处使用零mouseEventSource初始化leftMouseDown和leftMouseUp CGEvent,并将它们发布在位置cghidEventTap上.

There is no need for a user interface so I deleted the window from the Interface Builder and the outlet to it from the Application Delegate. I call a private method postClickEvent() from the method applicationDidFinishLaunching(_:). I initialize leftMouseDown and leftMouseUp CGEvents with nil mouseEventSource at mouseCursorPosition (x: 0, y: 0) and post them at location cghidEventTap.

import Cocoa  

@NSApplicationMain
AppDelegate类:NSObject,NSApplicationDelegate {

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

func applicationDidFinishLaunching(_ aNotification: Notification) {  
    postClickEvent()  
}  

func applicationWillTerminate(_ aNotification: Notification) {  
}  

private func postClickEvent() {  
    let point = CGPoint(x: 0, y: 0)  

    let leftMouseDownEvent = CGEvent(mouseEventSource: nil, mouseType: .leftMouseDown, mouseCursorPosition: point, mouseButton: .left)  
    let leftMouseUpEvent = CGEvent(mouseEventSource: nil, mouseType: .leftMouseUp, mouseCursorPosition: point, mouseButton: .left)  

    leftMouseDownEvent?.post(tap: .cghidEventTap)  
    leftMouseUpEvent?.post(tap: .cghidEventTap)  
}  

}

我希望该应用在启动后立即单击左上角并打开Apple菜单,但不会发生.

I would expect the app as soon as it is launched to click in the top left corner and open the Apple menu but it does not happen.

我可以想到几种可能的解决方案.

I can think of several possible solutions.

  • 在调试器中运行的应用可能需要权限才能发布 低级用户输入事件.应用是否需要许可才能发布 石英事件?
  • 默认情况下发送方法applicationDidFinishLaunching(_ :) 启动应用程序后通知中心 已初始化,但尚未接收到第一个事件.可能是 postClickEvent()应该从 应用程序委托?
  • 事件在初始化时可能需要一个事件源.但是过去 这三个事件中的任何一个都使用init(stateID :)初始化的事件源 可能的事件源状态ID不会更改它.
  • 仅当应用程序在全屏Xcode之后获得焦点时才可以发布事件,因此单击为时过早 Apple菜单项.但是将线程休眠10秒不会 更改它.
  • The app that runs in the debugger might need a permission to post a low-level user input event. Does an app need a permission to post a Quartz Event?
  • The method applicationDidFinishLaunching(_:) is sent by the default notification center after the application has been launched and initialized but before it has received its first event. Maybe postClickEvent() should be called from another method of the Application Delegate?
  • The events might need an Event Source at initialization. But passing the Event Source initialized with init(stateID:) for any of the three possible Event Source State IDs does not change it.
  • The events can be posted just when the app receives focus after full screen Xcode so too early to click the Apple menu item. But sleeping the thread for 10 seconds does not change it.

推荐答案

默认情况下,使用沙箱是Xcode 9.0中Swift Package Manager的一项新功能.

Using a sandbox by default is a new feature of Swift Package Manager in Xcode 9.0.

更新的软件包清单说明和软件包在macOS上构建以使用沙箱,从而阻止了网络访问和文件系统修改.这有助于减轻恶意制作的清单的影响. (31107213)

Updated package manifest interpretation and package builds on macOS to use a sandbox, which prevents network access and file system modification. This helps mitigate the effect of maliciously crafted manifests. (31107213)

Xcode发行说明

但是Quartz Events与App Sandbox不兼容.

But Quartz Events are incompatible with App Sandbox.

您无法沙箱控制一个应用程序的应用程序.使用CGEventPost之类的功能发布键盘或鼠标事件提供了一种规避此限制的方法,因此,沙盒应用程序不允许这样做.

You cannot sandbox an app that controls another app. Posting keyboard or mouse events using functions like CGEventPost offers a way to circumvent this restriction, and is therefore not allowed from a sandboxed app.

应用程序沙箱设计指南

运行该应用程序会在控制台中留下沙盒违规日志消息,表明该应用程序被拒绝了Human Interface Device控件.

Running the app leaves a Sandbox Violation log message in the Console that the app was denied Human Interface Device control.

控制台

在Xcode目标编辑器中禁用应用程序沙箱"可以发布Quartz事件.

Disabling App Sandbox in the Xcode target editor allows to post a Quartz Event.

Xcode

这篇关于Swift应用程序启动后如何发布Quartz事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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