Howdy folks, I am looking for a way to remove images from the clipboard, with or without removing styles (without removing styles is preferable, but not required). I was referred here from this post at the Keyboard Maestro forum.
Currently I am able to do this by saving the clipboard to an RTF file, then reading that file back to the clipboard. But @ccstone mentioned this might be doable using AppleScriptObjC, which I know nothing about.
If anybody has any ideas, I’d really appreciate it. Thanks in advance!
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use framework "AppKit"
use scripting additions
-- get RTFD from clipboard
set thePasteboard to current application's NSPasteboard's generalPasteboard()
set theData to thePasteboard's dataForType:(current application's NSRTFDPboardType)
if theData is missing value then error "No RTFD on the clipboard"
-- create mutable attributed string
set {theRTFD, theAtts} to current application's NSMutableAttributedString's alloc()'s initWithRTFD:theData documentAttributes:(reference)
-- replace attachments with empty string
theRTFD's mutableString()'s replaceOccurrencesOfString:(character id (current application's NSAttachmentCharacter)) withString:"" options:0 range:{0, theRTFD's |length|()}
-- put back on clipboard
thePasteboard's clearContents()
thePasteboard's writeObjects:{theRTFD}
Works like a charm, thank you! It’s far beyond my limited understanding of AppleScript so I don’t really know how it works, but it works and that’s what matters haha.