Use font style and font color from text editor

MacScripter shows AppleScript code very well with the AppleScript tag. However through the time we have shared code in other languages too like Objective-C because it works closely with AppleScriptObjC. Objective-C code was also needed when using the method call command to make calls to custom ObjC objects/classes. Multi line bash scripts comes by often to invoke using do shell script commands. Much more unrelated to this forum, C code for command line utilities has also been shared. However the code tag does differs the code from the discussion but still sometimes complicated or hard to read. Maybe I’m relying to much on syntax coloring nowadays but it makes read the code much better.

The solution:

set textRuns to getAttributeRuns(1)
set styleRuns to getAttributeProperties(1)

set trRef to a reference to textRuns
set tsRef to a reference to styleRuns

repeat with x from 1 to count trRef
	set theText to item x of trRef
	set theStyle to item x of tsRef
	
	if font of theStyle ends with "bold" then set theText to "[b]" & theText & "[/b]"
	if font of theStyle ends with "italic" or font of theStyle ends with "oblique" then set theText to "[i]" & theText & "[/i]"
	
	set c to HTMLColors(color of theStyle)
	if c is not "#000000" then set theText to "[" & "color=" & HTMLColors(color of theStyle) & "]" & theText & "[/color]"
	
	set item x of textRuns to theText
end repeat

set the clipboard to textRuns as string
return the clipboard

on HTMLColors(rgb)
	set theColor to "#"
	repeat with currentColor in rgb
		set theColor to theColor & character ((currentColor div 257 div 16) + 1) of "0123456789ABCDEF"
		set theColor to theColor & character ((currentColor div 257 mod 16) + 1) of "0123456789ABCDEF"
	end repeat
	return theColor
end HTMLColors

on getAttributeRuns(docRef)
	tell application "TextEdit" to return attribute run of document docRef
end getAttributeRuns

on getAttributeProperties(docRef)
	tell application "TextEdit" to return properties of every attribute run of document docRef
end getAttributeProperties

I’ve chosen to post an plain AppleScript solution but it can also be done with AppleScriptObjC or pure Objective-C using attribute runs too and will be a lot faster too when using larger document. Since the average copied document text or code sharing doesn’t have more than a thousand attribute runs the difference wouldn’t be noticeable. So why bother.

Because it relies on attribute runs it can be easily modified to work with other applications as well, in some cases you only have to change to the name of the application like AppleScript Editor (pointless because the AppleScript tag is better) to make it work with other apps. I’ve put it in handler because to make this work directly in Xcode (tested it with version 3), mail or safari you have to change more than the name of the app.

Usage:
I’ve created a new cocoa project in Xcode, copied the text from main.m and pasted it into text edit. Then I ran this script (which copies the result to the clipboard) and pasted the code in an quote tag. The result is then:

I’m putting it between quote tags so it’s even more clearer that it differs from the discussion. But you can also use it to copy text from an tetx editor document and post it on MS and keep some of it’s markup; it doesn’t have to be used for code sharing only.

How nice! :smiley:

Thanks. I am dead sure this will come in handy, and it looks so nice too!

Of course I had to set TextEdit to rich text. And I imported the shell script into XCode first of course, that is, I opened it. The file is called AllowCoredumps:

From BBEdit I can save a file as style html, then copy from the window, and paste it into a Text Edit document, before running the script.

This is a good assert macro for Objective C, written by Mike Ash

Thank you!

Hello.

I’ll create a table formatter, with hard spaces, that uses the “quote” tag the same way, to get nice looking timing tables, or other tables as well, the usage of the quote tag, without any quotee was a brilliant idea, apart from the syntax-colouring of course.

Now we’ll have to make them use it in the AsObjC forum. :smiley: