MacOS – AppleScript bug: I am unable to convert rich text to plain text

applescriptmacostext;

I created an AppleScript (.scpt) file entitled "Type Clipboard As Single Line Plain Text." The script is triggered by a keyboard shortcut set by FastScripts.

Desired behavior:

I want this script to take the clipboard contents, remove all formatting, and then remove any line breaks or tabs from this text. Finally, I want the script to type the new text. I want to preserve — not overwrite — the original clipboard contents.

The specific problem:

The specific error is that my script fails to remove all formatting from some rich text.

I cannot include full rich text content in a Stack Exchange post. Therefore, to witness my exact issue, please download this .rtf file via Dropbox. Open this file up in TextEdit.app. Highlight the sentence and copy it to the clipboard. Then, trigger my script while your cursor is in a form that supports and shows rich text (so that you can see that the script will type rich text).

You will notice that the typed sentence is rich text content and still contains formatting elements. These elements include the original text font (Helvetica) and the original font size (12). These elements should have been discarded. Thus, either my code is negligent or I have found a genuine bug within AppleScript itself. I am assuming that it is the latter.

The shortest code necessary to reproduce the error:

use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"

-- Back up clipboard contents:
set savedClipboard to my fetchStorableClipboard()

(*
    Converting the clipboard text to plain text to remove any formatting:
    From: http://lifehacker.com/127683/clear-text-formatting-on-os-x
*)
set theClipboardTextWithoutAnyFormatting to (the clipboard as text)

(*
    Removing line breaks and indentations in clipboard text:
    From: http://stackoverflow.com/a/12546965 
*)
set AppleScript's text item delimiters to {return & linefeed, return, linefeed, character id 8233, character id 8232}
set theClipboardTextWithoutAnyFormatting to text items of (theClipboardTextWithoutAnyFormatting as text)
set AppleScript's text item delimiters to {" "}
set theClipboardTextWithoutAnyLineBreaksOrFormatting to theClipboardTextWithoutAnyFormatting as text

set the clipboard to theClipboardTextWithoutAnyLineBreaksOrFormatting
tell application "System Events" to keystroke "v" using {command down}
delay 0.1 -- Without this delay, may restore clipboard before pasting.
-- Restore the original clipboard:
my putOnClipboard:savedClipboard

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()
    -- loop through pasteboard items
    repeat with anItem in thePasteboard's pasteboardItems()
        -- 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 anItem's types()
        -- for each type, get the corresponding data and store it all in the new pasteboard item
        repeat with aType in theTypes
            set theData to (anItem's dataForType:aType)'s mutableCopy()
            if theData is not missing value then
                (newPBItem's setData:theData forType:aType)
            end if
        end repeat
        -- add new pasteboard item to array
        (aMutableArray's addObject:newPBItem)
    end repeat
    return aMutableArray
end fetchStorableClipboard

on putOnClipboard: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 putOnClipboard:

First, can someone confirm that the stated issue occurs on their computer?

If so, how can I remove all formatting of rich text in AppleScript, accounting for this bug that I've discovered?

Best Answer

Because the clipboard command add others types automatically, test this script:

set the clipboard to "hello" as string
delay 1
return clipboard info

the result is --> {{Unicode text, 10}, {string, 5}, {scrap styles, 22}, {«class utf8», 5}, {«class ut16», 12}, {scrap styles, 22}}


To avoid the styles, use the NSPasteboard's methods:

-- *** add the missing lines from your script here  ***
--- set the clipboard to theClipboardTextWithoutAnyLineBreaksOrFormatting -- don't use this command to avoid the scrap styles type.
my putTextOnClipboard:theClipboardTextWithoutAnyLineBreaksOrFormatting -- use this method to put some string in the clipboard.

tell application "System Events" to keystroke "v" using {command down}
delay 0.1 -- Without this delay, may restore clipboard before pasting.
-- Restore the original clipboard:
my putOnClipboard:savedClipboard


on putTextOnClipboard:t
    set thePasteboard to current application's NSPasteboard's generalPasteboard()
    thePasteboard's clearContents()
    thePasteboard's declareTypes:{current application's NSPasteboardTypeString} owner:(missing value)
    thePasteboard's setString:t forType:(current application's NSPasteboardTypeString)
    --> now the clipboard contains these types only: («class utf8», «class ut16», string and Unicode text)
end putTextOnClipboard: