Search code examples
applescriptcoda

Remove Line Endings in Coda (or other apps) with Applescript


I've been using BBEdit in conjunction to develop for a while now. I use BBEdit frequently for its find and replace. Sometimes, I want to remove all line endings and tabs from a selection of text, which I have easily done with a regex find in BBEdit since its find and replace is scriptable. Coda has the ability to perform grep find and replace, but I don't think that it is scriptable. So, I have approached this from two ways: 1) See if I can do a grep find and replace in Coda with Applescript (which I don't think is possible), or 2) Pass my text to the command line and do it that way. Unless someone has an example of the former, this question will be related to doing it via the command line.

I'm using one of Coda's built in scripts as a template in combination with some other similar threads about this issue. I am NOT an Applescript or a regex expert, so please go easy on me if this is a simple mistake.

The text that I am inputing can vary wildly, but it is typically HTML and/or JS code.

This script will run, but nothing happens. Any ideas?

-- script settings
on CodaScriptSettings()
    return {displayName:"Remove Line Endings", inContextMenu:"yes"}
end CodaScriptSettings

-- actual script
tell application "Coda"

try

    tell current split of front document

        if selected text is not equal to "" then
            set someText to selected text
        else
            set someText to contents
        end if

    end tell

on error
    beep
    return
end try

end tell

set shellscriptString to "echo " & quoted form of someText & "|sed \"s/[\\t\\r\\n\\x]+/ /g\"" as string

set shellresult to do shell script shellscriptString without altering line endings

tell application "Coda"
try
    tell current split of document 1

        if selected text is not equal to "" then
            set selected text to shellresult
        else
            set contents to shellresult
        end if

    end tell

on error
    beep

end try
end tell

Solution

  • Try this:

    set shellscriptString to "echo " & quoted form of someText & "|tr -d '\\\t\\r\\n\\x'" as string