如何使用 AppleScriptObjC 和 NSPasteboard 读取剪贴板

How can I read the clipboard using AppleScriptObjC and NSPasteboard

我正在寻找解决 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)

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

它returns:

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

任何指点将不胜感激。

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

将所有这些放在一起我们得到这个解决方案:

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

玩得开心,迈克尔/汉堡