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:
- Open Script Editor
- Cut and paste into new file in Script Editor
- Save file to where your Coda scripts are. Example: /Users/
/Library/Scripts/Applications/Coda/HTML - Restart Coda
- 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:
- TextExander, the mac shortcut utility, has been a huge productivity booster for coding within Coda.
- Also, this article, "Panic Coda: What’s Missing? 19 Free Apps to Fill the Gaps", is also promising. Still digging through that one...

Post new comment