Create a New Note from Clipboard minus Formatting

This is very simple, but I have wanted a simple script that will create a new note from the clipboard but use my chosen font and size, not that of the source.

(*

This simple script does essentially what the command-N currently does in DEVONthink, with the exception that the new information is stripped of its formatting and pasted in with the default formatting.

*)

tell application "System Events"
    keystroke "n" using {control down, command down}
    --delay 0.2
    keystroke return
    keystroke tab using {control down}
    keystroke "v" using {command down, option down, shift down}
end tell

The only problem with this script is that this does not maintain the italics and bold in the original. There are interesting ideas that I tried to work with here:

viewtopic.php?f=20&t=11491&start=0

I built on the excellent work of the two posters there for to produce this code below.

It will create a new rich text file, paste in the clipboard information in whatever font/size is assigned in the script but retain bold/italic information:

tell application id "com.devon-technologies.thinkpro2"
    
    tell application "System Events"
        keystroke "n" using command down
        keystroke return
        keystroke tab using control down
        keystroke "v" using command down
    end tell
    
    
    tell text of think window 1
        
        --name of new default font family (must include italic named and not oblique named typefaces)
        set newFont to "Georgia"
        
        repeat with theAttribute in attribute runs
            set oldFont to font of theAttribute --determine current typeface
            
            --convert old typeface names to new typeface names then set new fonts (typefaces) in font family
            if oldFont contains "bolditalic" or oldFont contains "BoldItalic" or oldFont contains "oblique" or oldFont contains "Oblique" then
                set font of theAttribute to newFont & "-BoldItalic"
            else
                if oldFont contains "bold" or oldFont contains "Bold" then
                    set font of theAttribute to newFont & "-Bold"
                else
                    if oldFont contains "italic" or oldFont contains "Italic" or oldFont contains "oblique" or oldFont contains "Oblique" then
                        set font of theAttribute to newFont & "-Italic"
                    else
                        set font of theAttribute to newFont --remainder change font family name only
                    end if
                end if
            end if
        end repeat
        
        --set overall default properties
        set properties to {color:{5000, 5000, 5000}, background:{65535, 65535, 65535}, size:18, alignment:left, line spacing:1.0, paragraph spacing:8.0}
        
    end tell
end tell

This works fine with most things but often, when I copy text from a PDF, I get an error like this:

For example here is the “clipboard info” of some text (including narrative text and a bit of indented code in a manual on applescript) that creates this error:

here is the “clipboard info” copied from another PDF which worked fine:

I can’t quite figure out why text from one PDF won’t work while that from another will work - there is something in the data which is screwing things up but the classes within the text seem to be similar, no? Any ideas?

K. M. Lawson

Modifying an array (e.g. attribute runs/paragraphs) while enumerating it can cause index of out range errors. Adding try… end try will usually “fix” this error but depending on the script the results might be unexpected.