Applescript 使用 HTML 标签换行和 MarsEdit.app 脚本问题

发布于 2024-11-05 12:41:26 字数 1722 浏览 0 评论 0原文

我目前在 MarsEdit.app 中使用的脚本有一个缺陷。它检查 HTML 文档中是否有段落用

标记包裹的情况,如下所示:

-- If already starts with <p>, don't prepend another one

if not {oneParagraph starts with "<p>"} then
           set newBodyText to newBodyText & "<p>"
end if
set newBodyText to newBodyText & oneParagraph

这里的问题是,如果段落(或单行)用除 a 之外的任何其他 HTML 标记包裹,

标签 该脚本全面包裹

标签。

脚本的另一部分检查段落末尾的结束标签,其作用几乎相同。

-- If already ends with </p>, don't append another one

if not (oneParagraph ends with "</p>") then
    set newBodyText to newBodyText & "</p>"
end if

set newBodyText to newBodyText & return

示例:

Foobar

变为

Foobar
;

在另一个问题中Applescript 和“开头为”运算符,@lri 很友善地为我提供了一个与它。

on startswith(txt, l)
repeat with v in l
    if txt starts with v then return true
end repeat
false
end startswith

startswith("abc", {"a", "d", "e"}) -- true

他的另一个建议也可以在此网站上找到用applescript 上的标签

使用 MarsEdit.app 实施这些建议对我来说是另一个问题。

我将整个脚本上传到pastebin 上。 Pastebin:MarsEdit.app,用 换行

标签脚本 如果有人可以帮助我根据 @lri 的建议编辑脚本,那就太好了。提前致谢。

Im currently using a script in MarsEdit.app which has a flaw. It checks the HTML document for cases where paragraphs are wrapped with <p> tags as follows:

-- If already starts with <p>, don't prepend another one

if not {oneParagraph starts with "<p>"} then
           set newBodyText to newBodyText & "<p>"
end if
set newBodyText to newBodyText & oneParagraph

The problem here is that if the paragraph (or single line) is wrapped with any other HTML tag other than a <p> tag the script wraps <p> tags across the board.

Another portion of the script, which checks for ending tags at the end of the paragraph does pretty much the same thing.

-- If already ends with </p>, don't append another one

if not (oneParagraph ends with "</p>") then
    set newBodyText to newBodyText & "</p>"
end if

set newBodyText to newBodyText & return

Example:

<h5>Foobar </h5>

becomes

<p><h5>Foobar </h5></p>

In another question Applescript and "starts with" operator, @lri was kind enough to provide me a solution related to it.

on startswith(txt, l)
repeat with v in l
    if txt starts with v then return true
end repeat
false
end startswith

startswith("abc", {"a", "d", "e"}) -- true

and another of his recommendations can be found on this website as well Wrap lines with tags on applescript

Implementing these recommendations with MarsEdit.app is another issue for me.

I uploaded the entire script on pastebin. Pastebin: MarsEdit.app, wrap line with

tags script If anyone can help me edit the script to @lri's recommendations that would be great. Thanks in advance.

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

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

发布评论

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

评论(2

勿忘初心 2024-11-12 12:41:26

AppleScript:

tell application "MarsEdit" to set txt to current text of document 1
set paras to paragraphs of txt

repeat with i from 1 to (count paras)
    set v to item i of paras
    ignoring white space
        if not (v is "" or v starts with "<") then
            set item i of paras to "<p>" & v & "</p>"
        end if
    end ignoring
end repeat

set text item delimiters to ASCII character 10
tell application "MarsEdit" to set current text of document 1 to paras as text

Ruby appscript

require 'appscript'; include Appscript

doc = app('MarsEdit').documents[0]
lines = doc.current_text.get.gsub(/\r\n?/, "\n").split("\n")

for i in 0...lines.size
    next if lines[i] =~ /^\s*$/ or lines[i] =~ /^\s*</
    lines[i] = "<p>#{lines[i]}</p>"
end

doc.current_text.set(lines.join("\n"))

这些假设以(空格和)< 开头的任何内容code>< 是一个标签。

AppleScript:

tell application "MarsEdit" to set txt to current text of document 1
set paras to paragraphs of txt

repeat with i from 1 to (count paras)
    set v to item i of paras
    ignoring white space
        if not (v is "" or v starts with "<") then
            set item i of paras to "<p>" & v & "</p>"
        end if
    end ignoring
end repeat

set text item delimiters to ASCII character 10
tell application "MarsEdit" to set current text of document 1 to paras as text

Ruby appscript:

require 'appscript'; include Appscript

doc = app('MarsEdit').documents[0]
lines = doc.current_text.get.gsub(/\r\n?/, "\n").split("\n")

for i in 0...lines.size
    next if lines[i] =~ /^\s*$/ or lines[i] =~ /^\s*</
    lines[i] = "<p>#{lines[i]}</p>"
end

doc.current_text.set(lines.join("\n"))

These assume that anything starting with (white space and) < is a tag.

稚然 2024-11-12 12:41:26

你可以使用另一种更强大的语言通过在applescript中运行shell命令来完成这个过程

基本上你可以在终端窗口中运行任何你想运行的东西,就像这样

假设你的桌面上有一个test.txt文件你可以运行它并且它会包装所有带有 p 标签的行

set dir to quoted form of POSIX path of (path to desktop)
set results to do shell script "cd " & dir & "
awk ' { print \"<p>\"$0\"</p>\" } ' test.txt"

,如果你想运行一个 php 文件,你只需这样做

set dir to quoted form of POSIX path of 'path:to:php_folder")
set results to do shell script "cd " & dir & "
php test.php"

you could do this process using another stronger language by running shell commands in applescript

basiclly you can run anything that you would in a terminal window like this

lets assume you have a test.txt file on your desktop you could run this and it would wrap all the lines with p tag

set dir to quoted form of POSIX path of (path to desktop)
set results to do shell script "cd " & dir & "
awk ' { print \"<p>\"$0\"</p>\" } ' test.txt"

and if you want to run a php file you just do

set dir to quoted form of POSIX path of 'path:to:php_folder")
set results to do shell script "cd " & dir & "
php test.php"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文