【问题标题】:plain text URL to HTML code (Automator/AppleScript)HTML 代码的纯文本 URL (Automator/AppleScript)
【发布时间】:2012-12-02 15:36:15
【问题描述】:

假设我在 TextEdit 等文本编辑器中有一个纯 txt 文件:

title 1
http://a.b/c

title 2
http://d.e/f

...

我想将所有以 http:// 开头的行转换为 URL 的 HTML 代码,这样上述内容将变为:

title 1
<a href="http://a.b/c">http://a.b/c</a>

title 2
<a href="http://d.e/f">http://d.e/f</a>

...

如何在 Automator 或 AppleScript 中完成这项工作? (我目前的解决方案是使用 Gmail,但它涉及多步复制粘贴。)

非常感谢您。

【问题讨论】:

    标签: applescript automator


    【解决方案1】:

    这将使您避免使用其他编辑器:

    set inFile to "/Users/you/Desktop/Urls.txt"
    set outFile to "/Users/you/Desktop/Urls2.txt"
    
    do shell script "sed 's/\\(http[^ ]*\\)/<a href=\"\\1\">\\1<\\/a>/g' " & quoted form of inFile & " >" & quoted form of outFile
    

    【讨论】:

      【解决方案2】:

      只需在文本编辑器或终端中进行正则表达式搜索和替换:

      sed -E 's|^(http:.*)|<a href="\1">\1</a>|g' file.txt
      

      【讨论】:

      • 非常感谢您快速而有帮助的回复!由于终端方法只替换了第一个 URL,因此我寻找了支持正则表达式的 Mac 应用程序。我得到了 MacVim。 MacVim 的“语法”菜单中有一个“转换为 HTML”菜单命令,可以完美地完成我想要的替换。谢谢。
      猜你喜欢
      • 1970-01-01
      • 2015-08-01
      • 1970-01-01
      • 2018-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-06
      • 2021-08-29
      相关资源
      最近更新 更多