2015-03-25 29 views
0

我在AppleScriptObjC上找到了方法,並發現Shane Stanley的Everyday AppleScriptObjC有幫助。但我仍然很難知道從哪裏開始NSPasteboard如何使用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 

任何指針將不勝感激。

+0

任何理由不只是使用加標'的clipboard'命令? – foo 2015-03-25 07:41:24

+0

我應該圍繞從剪貼板中檢索程序特定數據並將其作爲文本讀取來構建問題。 '剪貼板'不這樣做。 – user3803526 2015-03-25 18:26:24

回答

0

NSPasteboard是一個容器,它能容納許多不同種類的不同物品。有幾個標準粘貼板,如NSGeneralPboard,NSRulerPboardNSDragPboard。對於複製和粘貼操作,使用通用粘貼板。
存儲在一般粘貼板內的項目也可能不同。可以將public.rtf,public.jpeg等存儲在粘貼板中。

全部放在一起,我們得到這樣的解決方案:

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

玩得開心,邁克爾/漢堡

+0

不錯。這非常有幫助。如果剪貼板上有特定於應用程序的項目,例如'com.apple.flexo.proFFPasteboardUTI',會怎麼樣?我如何指定UTI以及如何將其作爲文本讀取?這會不會涉及'readingOptionsForType'方法? – user3803526 2015-03-25 15:57:00

+0

這取決於數據如何寫入剪貼板。大多數命令c操作將多個數據外觀寫入剪貼板。示例:如果您在Finder中選擇了一個文件,文件本身以及文件路徑都將被複制。如果將其粘貼到Finder中,它將使用該文件,如果將其粘貼到Outlook中,則會粘貼文件路徑。我可以想象你的應用程序也會填充字符串值! – ShooTerKo 2015-03-25 16:01:30

+0

這是來自Final Cut Pro X的數據,據我使用[ClipboardViewer](https://developer.apple.com/library/mac/samplecode/ClipboardViewer/Introduction/Intro.html)可以看到,只有一個項目在剪貼板上。它似乎主要是xml。 – user3803526 2015-03-25 16:39:06