Coda: search and replace for special characters

UPDATE: Doh! text -> Encode Entities will also work, though the below script will give you a bit more control over what you want to encode to.
UPDATE2: I actually like my homespun script better. The above built-in functionality will encode ALL entities including your html tags if you don't have anything selected. My script only encodes specific chars, so I can quickly encode a whole document w/o worrying about selecting.

Lately, Coda has been my main text editor of choice. I switched from TextMate, which I still love, but Coda just seems to be more natural for me.

However, Coda is lacking features that TextMate has that I have missed greatly. Namely, the ability to wrap selection with tag. Through a google search, I found a bundle of Applescripts that mimic the wrap features in TextMate, "Textmate Emulation Applescripts for Coda"

Looking at those scripts got me to thinking: Often, I need to convert special characters like curly quote, em dashes, trademark symbols from text that I've cut and paste from word or other into decimal entities (like an em dash to — and so on). Wouldn't it be nice to do this in one fell swoop? Applescript to the rescue.

Now, this probably isn't the best, most efficient script in the world, and I've basically performed a frankenstein-type of operation using scripts I've found elsewhere, but if this helps anyone, have at it (and feel free to add/remove/customize the special chars you want to encode.)

-- CHANGE THIS IF YOU INSTALL TO A NON-STANDARD DIRECTORY
-- MAKE SURE TO INCLUDE THE TRAILING SLASH AT THE END
set installDirectory to "~/Library/Scripts/Applications/Coda/HTML/"

-- script settings
on CodaScriptSettings()
	return {displayName:"clean up special chars to decimal entities", inContextMenu:"yes"}
end CodaScriptSettings

-- this sub from: http://foolsworkshop.com/applescript/2008/05/an-applescript-replace-text...
on replaceText(find, replace, subject)
	set prevTIDs to text item delimiters of AppleScript
	set text item delimiters of AppleScript to find
	set subject to text items of subject
	
	set text item delimiters of AppleScript to replace
	set subject to "" & subject
	set text item delimiters of AppleScript to prevTIDs
	
	return subject
end replaceText

tell application "Coda"
	
	try
		tell current split of document 1
			if selected text is equal to "" then
				set someText to contents
			else
				set someText to selected text
			end if
			set cleanedText to my replaceText("’", "’", someText)
			set cleanedText to my replaceText("“", "“", cleanedText)
			set cleanedText to my replaceText("”", "”", cleanedText)
			set cleanedText to my replaceText("–", "—", cleanedText)
			set cleanedText to my replaceText("®", "®", cleanedText)
			set cleanedText to my replaceText("™", "™", cleanedText)
			set cleanedText to my replaceText(" & ", " & ", cleanedText)
			if selected text is equal to "" then
				set contents to cleanedText
			else
				set selected text to cleanedText
			end if
		end tell
	on error errmsg
		-- beep
		display dialog errmsg
		return
	end try
end tell

Instructions:

  1. Open Script Editor
  2. Cut and paste into new file in Script Editor
  3. Save file to where your Coda scripts are. Example: /Users//Library/Scripts/Applications/Coda/HTML
  4. Restart Coda
  5. Run like you would any script w/in Coda

USE AT YOUR OWN RISK! Do some testing first on test copy and feel free to comment.

These are other optimizations for Coda that I have discovered:

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
7 + 8 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.