Converting everything but bold and italics to plain text

I found this code:

set the clipboard to �class ktxt� of ((the clipboard as text) as record)

which converts the clipboard to plaintext.

I’d like to alter this script to make everything plain text except bold and italics which would remain bold and italics. Everything else would be dropped (color, font, indents, etc.).

I found this script: remove line breaks. which I thought might help me figure it out, but I don’t understand it. I am very much a beginner with applescript. Here is the remove line breaks script:

set clipText to the clipboard as "TEXT"
set theLines to TextToList(clipText, return)
set trimChars to {" ", ASCII character 9}
set previousLineEmpty to true
set firstLine to true
repeat with aLine in theLines
	set lineLen to length of aLine
	set startPos to 1
	set foundEOFwd to false
	repeat while not foundEOFwd and startPos ? lineLen
		set theChar to text startPos thru startPos of aLine
		if not (trimChars contains theChar) then
			set foundEOFwd to true
		else
			set startPos to startPos + 1
		end if
	end repeat
	if startPos ? lineLen then
		set contents of aLine to text startPos thru -1 of aLine
	else
		set contents of aLine to ""
	end if
	set endPos to length of aLine
	set foundEOFwd to false
	repeat while not foundEOFwd and endPos ? 1
		set theChar to text endPos thru endPos of aLine
		if not (trimChars contains theChar) then
			set foundEOFwd to true
		else
			set endPos to endPos - 1
		end if
	end repeat
	if endPos ? 1 then
		set contents of aLine to text 1 thru endPos of aLine
	else
		set contents of aLine to ""
	end if
	set currentLineEmpty to (contents of aLine = "")
	if previousLineEmpty then
		if firstLine then
			set firstLine to false
		else
			set contents of aLine to return & contents of aLine
		end if
	else
		if currentLineEmpty then
			set contents of aLine to return
		else
			set contents of aLine to " " & contents of aLine
		end if
	end if
	set previousLineEmpty to currentLineEmpty
end repeat
set clipText to ListToText(theLines, "")
set the clipboard to clipText

on TextToList(theText, theDelimiter)
	set saveDelim to AppleScript's text item delimiters
	try
		set AppleScript's text item delimiters to {theDelimiter}
		set theList to every text item of theText
	on error errStr number errNum
		set AppleScript's text item delimiters to saveDelim
		error errStr number errNum
	end try
	set AppleScript's text item delimiters to saveDelim
	return (theList)
end TextToList

on ListToText(theList, theDelimiter)
	set saveDelim to AppleScript's text item delimiters
	try
		set AppleScript's text item delimiters to {theDelimiter}
		set theText to theList as text
	on error errStr number errNum
		set AppleScript's text item delimiters to saveDelim
		error errStr number errNum
	end try
	set AppleScript's text item delimiters to saveDelim
	return (theText)
end ListToText

Any help is much appreciated! Thanks.

One way might be to work with the text in a scriptable app that supports text styling. Where was the data before you got it into the clipboard?

Usually I need this to take an article in Microsoft Word and place it in InDesign. 99% of the time, the writers add unnecessary formatting that I waste my time removing. That’s what I really want this script for.

I agree with the first reply: your best bet is to use a text editor that supports styled text, since bold and italic are attributes that require such an editor to display or manipulate readlily. I’d suggest the $15 shareware app, Tex-Edit Plus. It has a superb scripting dictionary and comes with many sample AppleScripts.

After reading the above post, I did a search for MS Word Scripts and found this one.

tell application "Microsoft Word"
    do Visual Basic "
        Dim rngWord As Range
        Dim i As Integer
        With ActiveDocument
        With .Range
                    With .Font
                            .Bold = False
                            .Italic = False
                            .Underline = wdUnderlineNone ' and so forth
                    End With
                With .ParagraphFormat
                    .Alignment = wdAlignParagraphLeft ' and more
                End With
        End With
        For Each rngWord In .Words
            With rngWord
                If .Text <> vbTab Then
                                If .Text <> vbNewLine Then
                        rngWord.Font.Bold = True
                                        i = i + 1
                                End If
                            End If
                    End With
                    If i = 2 Then Exit For
        Next rngWord
        End With
            "
end tell

Unfortunately, It uses visual basic which I don’t understand. Anyone know how to modify this to suite my needs? That is, make it keep bold and italics formatting and delete all other formatting?

I know exactly the problem you’re talking about – I had to do this in Quark, but I was using QuarkTags, so I just stripped out any tags that I didn’t want.

Since you’re going to put the text into InDesign, and that’s scriptable, how about placing the text into a frame and then stripping out any bad formatting there?

I have ID 2. On that version, InDesign’s scripting manual is on the install CD under Adobe Technical Info/Scripting/InDesign Scripting Guide.pdf

Word’s VB would be the best way to walk. I’ve done some tests, but it doesn’t work allways the same way.
You can try the following: open a new script editor window, hit “record” and go to Word. Make a “replace” (apple+f) searching for “*” and replacing with… (choose “font” in more options and check-uncheck what you need, etc.).
Go back to the script editor and hit “stop”. Your script is done (though most probably when you hit “run” to test it again, it won’t work :x)

You can take a look to this file:
http://files.macscripter.net/scriptbuilders/ScriptTools/sttr2.sit

It is a library to manipulate styled text on-the-fly. It is included an example to convert styled text to plain text, but keeping bolds. You can adaptate it easily to keep also italics…

Cheers!

jj, thank you for the tip. this looks very promising. i downloaded the file you mentioned.

I got the sample keep bold script to work. I replaced the two instances of the word “bold” with “italic” thus changed this into a keep italic script. but i’m having trouble getting it to keep both bold and italic. here’s the sample keep bold script:

property sttr : missing value

--> modify the following as needed
if sttr is missing value then
	set sttr to load script alias "Macintosh HD:Library:Scripts:StyledTextToRecord:StyledText <-> Record.scpt"
end if


display dialog "I will take the contents of the clipboard (styled text, please) and convert everything to plain text, except for bolds" with icon note

set clipContents to (the clipboard) as styled text

tell sttr to copy StyledTextToRecord(clipContents) to styleInfo

--> get default styles to reset all
tell sttr to copy StyledTextToRecord("x" as Unicode text) to defaultStyleInfo

repeat with i from 1 to count styleInfo's stylesOn
	set x to styleInfo's stylesOn's item i
	if x is not {plain} then
		if x contains bold then
			set styleInfo's stylesOn's item i to {bold}
		else
			set styleInfo's stylesOn's item i to {plain}
		end if
	end if
	--> colors and others out!
	set styleInfo's stylesHeights's item i to defaultStyleInfo's stylesHeights's item 1
	set styleInfo's stylesAscents's item i to defaultStyleInfo's stylesAscents's item 1
	set styleInfo's stylesFontIDs's item i to defaultStyleInfo's stylesFontIDs's item 1
	set styleInfo's stylesSizes's item i to defaultStyleInfo's stylesSizes's item 1
	set styleInfo's stylesColors's item i to {0, 0, 0}
end repeat

set the clipboard to (sttr's RecordToStyledText(styleInfo))

beep 2

the problem is: i don’t really know applescript. I thought I could just copy:


		if x contains bold then
			set styleInfo's stylesOn's item i to {bold}
		else
			set styleInfo's stylesOn's item i to {plain}
		end if

…and paste it right underneath itself and change the two instances of “bold” to “italic” but that didn’t work. I kept trying to copy and paste more and more and i thought I had to change the variable “i” to something else. argggg. i bought the o’reilly book on applescript so i’m going to learn this stuff. but if anyone can help me get this working i’d very much appreciate it. thanks.

with the help of some friends, I figured it out:

property sttr : missing value

--> modify the following as needed
if sttr is missing value then
	set sttr to load script alias "Macintosh HD:Library:Scripts:StyledTextToRecord:StyledText <-> Record.scpt"
end if


display dialog "I will take the contents of the clipboard (styled text, please) and convert everything to plain text, except for bolds and italics" with icon note

set clipContents to (the clipboard) as styled text

tell sttr to copy StyledTextToRecord(clipContents) to styleInfo

--> get default styles to reset all
tell sttr to copy StyledTextToRecord("x" as Unicode text) to defaultStyleInfo

repeat with i from 1 to count styleInfo's stylesOn
	set x to styleInfo's stylesOn's item i
	if x is not {plain} then
		if x contains bold and x contains italic then
			set styleInfo's stylesOn's item i to {bold, italic}
		else if x contains bold then
			set styleInfo's stylesOn's item i to {bold}
		else if x contains italic then
			set styleInfo's stylesOn's item i to {italic}
		else
			set styleInfo's stylesOn's item i to {plain}
		end if
	end if
	--> colors and others out!
	set styleInfo's stylesHeights's item i to defaultStyleInfo's stylesHeights's item 1
	set styleInfo's stylesAscents's item i to defaultStyleInfo's stylesAscents's item 1
	set styleInfo's stylesFontIDs's item i to defaultStyleInfo's stylesFontIDs's item 1
	set styleInfo's stylesSizes's item i to defaultStyleInfo's stylesSizes's item 1
	set styleInfo's stylesColors's item i to {0, 0, 0}
end repeat

set the clipboard to (sttr's RecordToStyledText(styleInfo))

beep 2

if something is two formatting types like bold and underlined, this will not work. but it’s good enough for me for now.

Works for me with any styles combination…