使用 Applescript 删除 Coda(或其他应用程序)中的行结尾

发布于 2024-11-07 23:04:54 字数 1407 浏览 0 评论 0原文

我已经使用 BBEdit 进行开发有一段时间了。我经常使用 BBEdit 进行查找和替换。有时,我想从选定的文本中删除所有行结尾和制表符,我可以使用 BBEdit 中的正则表达式查找轻松完成此操作,因为它的查找和替换是可编写脚本的。 Coda 能够执行 grep 查找和替换,但我不认为它是可编写脚本的。因此,我通过两种方式解决了这个问题:1)看看我是否可以在 Coda 中使用 Applescript 进行 grep 查找和替换(我认为这是不可能的),或者 2)将我的文本传递到命令行并执行此操作那样。除非有人有前者的示例,否则这个问题将与通过命令行执行此操作有关。

我使用 Coda 的内置脚本之一作为模板,并结合有关此问题的其他一些类似线程。我不是 Applescript 或正则表达式专家,所以如果这是一个简单的错误,请对我宽容一点。

我输入的文本可能会有很大差异,但通常是 HTML 和/或 JS 代码。

该脚本将运行,但什么也没有发生。有什么想法吗?

-- 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

装纯掩盖桑 2024-11-14 23:04:54

试试这个:

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

Try this:

set shellscriptString to "echo " & quoted form of someText & "|tr -d '\\\t\\r\\n\\x'" as string
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文