【问题标题】:How can I read the clipboard using AppleScriptObjC and NSPasteboard如何使用 AppleScriptObjC 和 NSPasteboard 读取剪贴板
【发布时间】:2015-03-25 18:02:27
【问题描述】:

我正在寻找 AppleScriptObjC 的方法,并发现 Shane Stanley 的 Everyday AppleScriptObjC 很有帮助。但我仍然很难知道从NSPasteboard 开始。

这是我目前所拥有的:

use AppleScript version "2.4"
use framework "Foundation"
use framework "AppKit"
use scripting additions

on readClipboard(theClipboard)
    set theClipboard to current application's NSPasteboard
    return theClipboard as text
end readClipboard

set theContents to missing value
its readClipboard(theContents)

这当然行不通。但它似乎确实在做点什么。

返回:

error "Can’t make «class ocid» id «data optr000000002852F974FF7F0000» into type text." number -1700 from «class ocid» id «data optr000000002852F974FF7F0000» to text

任何指针将不胜感激。

【问题讨论】:

  • 有什么理由不只是使用标准添加的the clipboard 命令?
  • 我应该围绕从剪贴板检索程序特定数据并将其作为文本读取的问题提出框架。 the clipboard 不会那样做。

标签: cocoa applescript applescript-objc nspasteboard


【解决方案1】:

NSPasteboard 是一个容器,可以容纳许多不同种类的物品。定义了一些标准粘贴板,例如 NSGeneralPboardNSRulerPboardNSDragPboard。对于复制和粘贴操作,使用通用粘贴板。
存储在一般粘贴板中的项目也可能不同。可以将public.rtfpublic.jpeg 等存储在粘贴板中。

综合起来,我们得到了这个解决方案:

    set theClipboard to current application's NSPasteboard's generalPasteboard's stringForType:(current application's NSPasteboardTypeString)

玩得开心,迈克尔/汉堡

【讨论】:

  • 不错。这非常有帮助。如果剪贴板上有特定于应用程序的项目,例如com.apple.flexo.proFFPasteboardUTI,该怎么办?如何指定该 UTI 以及如何将其作为文本阅读?这会以某种方式涉及readingOptionsForType 方法吗?
  • 这取决于数据如何写入剪贴板。大多数 command-c 操作会将不止一次出现的数据写入剪贴板。示例:如果您在 Finder 中选择一个文件,则会复制文件本身以及文件路径。如果将其粘贴到 Finder 中,它会使用该文件,如果将其粘贴到 Outlook 中,它会粘贴文件路径。我可以想象你的应用程序也填充了字符串值!
  • 这是来自 Final Cut Pro X 的数据,据我使用 ClipboardViewer 所见,剪贴板上只有一项。它似乎主要是 xml。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-09-11
  • 2015-08-13
  • 2019-09-25
  • 1970-01-01
  • 2013-11-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多