sometimes I copy mixed content to the clipboard like pictures and links. So far I am able to fetch text, but how about pictures ? (eventually audio and video, if possible) ?
I use this code (not written by me):
set the_clipboard to (do shell script "osascript -e 'the clipboard as \"RTF \"' | perl -ne 'print chr foreach unpack(\"C*\",pack(\"H*\",substr($_,11,-3)))'")
basically I wanna create a rtfd document from the clipboard output. I tried to change RTF to RTFD in the code above, but RTFD seems not supported.
I tried in naive way to fetch the contents of the clipboard and put it in an open document of TextEdit, (my clipboard containing a picture and some text), using ApplescriptObjC. The below code was originally written by Shane Stanley.
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit" -- for NSPasteboard
set theClip to its fetchStorableClipboard()
#return theClip
tell document 1 of application "TextEdit"
activate
my restoreClipboard:theClip
end tell
on fetchStorableClipboard()
set aMutableArray to current application's NSMutableArray's array()
-- used to store contents
-- get the pasteboard and then its pasteboard items
set thePasteboard to current application's NSPasteboard's generalPasteboard()
set theItems to thePasteboard's pasteboardItems()
-- loop through pasteboard items
repeat with i from 1 to count of theItems
-- make a new pasteboard item to store existing item's stuff
set newPBItem to current application's NSPasteboardItem's alloc()'s init()
-- get the types of data stored on the pasteboard item
set theTypes to (item i of theItems)'s types()
-- for each type, get the corresponding data and store it all in the new pasteboard item
repeat with j from 1 to count of theTypes
set theData to ((item i of theItems)'s dataForType:(item j of theTypes))'s mutableCopy()
-- mutableCopy makes deep copy
if theData is not missing value then
(newPBItem's setData:theData forType:(item j of theTypes))
end if
end repeat
-- add new pasteboard item to array
(aMutableArray's addObject:newPBItem)
end repeat
return aMutableArray
end fetchStorableClipboard
on restoreClipboard:theArray
-- get pasteboard
set thePasteboard to current application's NSPasteboard's generalPasteboard()
-- clear it, then write new contents
thePasteboard's clearContents()
thePasteboard's writeObjects:theArray
end restoreClipboard: