【问题标题】:Using NSPerformService API to invoke Continuity Camera Service使用 NSPerformService API 调用 Continuity Camera Service
【发布时间】:2019-06-02 18:24:06
【问题描述】:

我正在尝试使用BOOL NSPerformService(NSString *itemName, NSPasteboard *pboard); API 以编程方式调用 Continuity Camera (Mac OS) 服务,以便可以在简单的按钮单击后挂钩该功能。需要作为itemName参数传入的Continuity Camera服务的名称是什么?

我无法从 com.apple.nsserivcescache.plist 文件中找到服务的名称,但从上下文菜单中,服务的名称是“拍照”和“扫描文档”。我不确定这些名称是否会起作用,因为它们总是与设备名称相关联( iPhone | iPad )。

我尝试过的事情。

NSPerformSerice( @"Take Photo", [NSPasteboard generalPasteboard] ); NSPerformSerice( @"<Name of the iPhone> Take Photo", [NSPasteboard generalPasteboard] ); NSPerformSerice( @"<Name of the iPhone>/Take Photo", [NSPasteboard generalPasteboard] );

【问题讨论】:

  • 服务菜单项的名称是什么?
  • 服务菜单项名称是 后跟两个选项“拍照”/“扫描文档”。我查看了 com.apple.nsservicescache.plist 文件以找到 Continuity Camera 功能使用的服务的名称,但找不到它的名称。
  • 你尝试了哪些项目名称?
  • 我尝试了以下方法,NSPerformService( @"Take Photo", [NSPasteboard generalPasteboard] ); NSPerformService( @"<Name of the iPhone> Take Photo", [NSPasteboard generalPasteboard] );``NSPerformService( @"<Name of the iPhone>/Take Photo", [NSPasteboard generalPasteboard] ); 但没有一个奏效。 @Willeke 指出上下文菜单方式使用连续性相机服务的链接。我想在单击按钮时执行该服务。

标签: objective-c macos cocoa appkit


【解决方案1】:

我写了一篇关于为您自己的应用添加连续互通相机支持的短文:https://thomas.zoechling.me/journal/2018/10/Continuity.html

您必须实现NSServicesMenuRequestor 以表明您可以处理来自粘贴板的图像:

override func validRequestor(forSendType sendType: NSPasteboard.PasteboardType?, returnType: NSPasteboard.PasteboardType?) -> Any? {
    if let pasteboardType = returnType,
        NSImage.imageTypes.contains(pasteboardType.rawValue) {
        return self
    } else {
        return super.validRequestor(forSendType: sendType, returnType: returnType)
    }
}

通过实现上述方法,当前第一响应者的菜单(例如按钮菜单)将自动填充连续性相机菜单项。

该菜单的项目将启动 CC UI,然后当用户执行捕获时调用 readSelection(from: pasteboard)
您可以从那里阅读粘贴板内容:

func readSelection(from pasteboard: NSPasteboard) -> Bool {
    guard pasteboard.canReadItem(withDataConformingToTypes: NSImage.imageTypes) else { return false }
    guard let image = NSImage(pasteboard: pasteboard) else { return false }

    self.imageView.image = image
    return true
}

还应该可以控制 在哪里 CC 相关的菜单项被插入。有一个相关的NSMenuItemImportFromDeviceIdentifier 常量,但我还没有弄清楚如何使用它。 (此 Twitter 线程中的一些上下文:https://twitter.com/weichsel/status/1052980223891972096

【讨论】:

    猜你喜欢
    • 2015-04-17
    • 1970-01-01
    • 1970-01-01
    • 2019-07-24
    • 2021-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-30
    相关资源
    最近更新 更多