Every application get/set specific types for pasteboard.
ex.
Safari set the first types of copy selected text to webarchive. Apple Mail support webarchive
as paste object. When we copy data and paste it to other application it could be we want
something else. The first script could answer the question what kind of types are supported
for the target application. The second script setData forType so the choosen type become the
first and only type.
The first Script make a array/list of supported types for pasteboard of specific application.
The interesting part about understanding NSPasteboard method and applications different types.
Some application could output to html. We could save this to a file. Open the html file in Safari
Make webarchive and send it to Apple mail. The reason it could be a working solution is the layout
of the data is much better. But not only that Apple mail also let us to edit the data before we
send it.
ex. It would be possible to send styled Excel data from Apple mail, that is interesting. (webarchive)
Other approach could be: Lets say you have got a email with a nice layout of html5 email.
And you like to use that code to build your template. You could select all in Apple mail of target
email and save it to file as html. To do that we will use pasteboard and type public.html.
Now we could edit the file and add/remove to be used as template for email.
use framework "Foundation"
use framework "AppKit"
use scripting additions
(**
* [Instance Property]: types
* An array of the receiver’s supported data types.
**
* @property(nullable, readonly, copy) NSArray<NSPasteboardType> *types;
*)
set pasteboard to (current application's NSPasteboard's generalPasteboard())'s types() as list
return (choose from list pasteboard) as text
The second script set the pasteboard type to the first type.
use framework "Foundation"
use framework "AppKit"
use scripting additions
set pasteboardTypeList to {"url", "color", "contents", "file-url", "html", "text-selection", "pdf", "png", "rtf", "rtfd", "sound", "string", "tabular-text", "tiff", "webarchive"}
my setPasteboardType:((choose from list pasteboardTypeList) as text)
on setPasteboardType:_pasteboardType
set pasteboard to current application's NSPasteboard's generalPasteboard()
if (_pasteboardType is "url") then
set theData to pasteboard's dataForType:(current application's NSPasteboardTypeURL)
pasteboard's clearContents()
pasteboard's setData:theData forType:(current application's NSPasteboardTypeURL)
end if
if (_pasteboardType is "color") then
set theData to pasteboard's dataForType:(current application's NSPasteboardTypeColor)
pasteboard's clearContents()
pasteboard's setData:theData forType:(current application's NSPasteboardTypeColor)
end if
if (_pasteboardType is "contents") then
set theData to pasteboard's dataForType:(current application's NSFileContentsPboadType)
pasteboard's clearContents()
pasteboard's setData:theData forType:(current application's NSFileContentsPboadType)
end if
if (_pasteboardType is "file-url") then
set theData to pasteboard's dataForType:(current application's NSPasteboardTypeFileURL)
pasteboard's clearContents()
pasteboard's setData:theData forType:(current application's NSPasteboardTypeFileURL)
end if
if (_pasteboardType is "html") then
set theData to pasteboard's dataForType:(current application's NSPasteboardTypeHTML)
pasteboard's clearContents()
pasteboard's setData:theData forType:(current application's NSPasteboardTypeHTML)
end if
if (_pasteboardType is "text-selection") then
set theData to pasteboard's dataForType:(current application's NSPasteboardTypeMultipleTextSelection)
pasteboard's clearContents()
pasteboard's setData:theData forType:(current application's NSPasteboardTypeMultipleTextSelection)
end if
if (_pasteboardType is "pdf") then
set theData to pasteboard's dataForType:(current application's NSPasteboardTypePDF)
pasteboard's clearContents()
pasteboard's setData:theData forType:(current application's NSPasteboardTypePDF)
end if
if (_pasteboardType is "png") then
set theData to pasteboard's dataForType:(current application's NSPasteboardTypePNG)
pasteboard's clearContents()
pasteboard's setData:theData forType:(current application's NSPasteboardTypePNG)
end if
if (_pasteboardType is "rtf") then
set theData to pasteboard's dataForType:(current application's NSPasteboardTypeRTF)
pasteboard's clearContents()
pasteboard's setData:theData forType:(current application's NSPasteboardTypeRTF)
end if
if (_pasteboardType is "rtfd") then
set theData to pasteboard's dataForType:(current application's NSPasteboardTypeRTFD)
pasteboard's clearContents()
pasteboard's setData:theData forType:(current application's NSPasteboardTypeRTFD)
end if
if (_pasteboardType is "sound") then
set theData to pasteboard's dataForType:(current application's NSPasteboardTypeSound)
pasteboard's clearContents()
pasteboard's setData:theData forType:(current application's NSPasteboardTypeSound)
end if
if (_pasteboardType is "string") then
set theData to pasteboard's dataForType:(current application's NSPasteboardTypeString)
pasteboard's clearContents()
pasteboard's setData:theData forType:(current application's NSPasteboardTypeString)
end if
if (_pasteboardType is "tabular-text") then
set theData to pasteboard's dataForType:(current application's NSPasteboardTypeTabularText)
pasteboard's clearContents()
pasteboard's setData:theData forType:(current application's NSPasteboardTypeTabularText)
end if
if (_pasteboardType is "tiff") then
set theData to pasteboard's dataForType:(current application's NSPasteboardTypeTIFF)
pasteboard's clearContents()
pasteboard's setData:theData forType:(current application's NSPasteboardTypeTIFF)
end if
if (_pasteboardType is "webarchive") then
set theData to pasteboard's dataForType:"com.apple.webarchive"
pasteboard's clearContents()
pasteboard's setData:theData forType:"com.apple.webarchive"
end if
end setPasteboardType:
And its also possible to write the pasteboard data to a file, like in this example of copy
text on website with Safari to save it as webarchive. I know Shane did a similar thing with
WebView, here is my code https://macscripter.net/viewtopic.php?id=47908
Here is Shanes code: https://macscripter.net/viewtopic.php?pid=194944#p194944
That said, this approach for webarchive is very limited, better to use Shanes code.
This approach do not use WebView or WebKit framework
use framework "Foundation"
use framework "AppKit"
use scripting additions
set thePath to POSIX path of (path to desktop) & "somefile.webarchive"
set theURL to current application's |NSURL|'s fileURLWithPath:thePath
my pasteboardToURL:theURL dataForType:"com.apple.webarchive"
on pasteboardToURL:_theURL dataForType:_theDataForType
set pasteboard to current application's NSPasteboard's generalPasteboard()
set theData to pasteboard's dataForType:_theDataForType
if theData = missing value then error "No data found on clipboard"
set theResult to (theData's writeToURL:_theURL atomically:true)
return theResult
end pasteboardToURL:dataForType: