Applescript wrap lines with HTML tags and MarsEdit.app script problem
Asked Answered
G

2

0

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.

Glaucescent answered 9/5, 2011 at 5:8 Comment(8)
it seems to me that you have picked the wrong tool IMHO you should use javascript,jquery,php,perl or another stronger language to parse htmlPlantaineater
@Plantaineater true say, but MarsEdit.app is limited to scripts compiled in applescript. Using php or jquery to strip/append html tags is easy to accomplish in a different context. From what i know the app uses jquery for specific operations, but thats a different story.Glaucescent
that said you could always use apple script to run javascript,perl,phpPlantaineater
@Plantaineater do you have any clue on how i can implement that?Glaucescent
@Glaucescent That question was way too detailed — I tried to encapsulate it to a few sentences. Hope you don't mind that.Logan
@Logan no i dont mind at all. Its exactly what i was looking for. Its way better than the script i was using. Clean and readable.Glaucescent
@Glaucescent Apparently the edit got rejected. Shouldn't be playing moderator with < 200 rep.Logan
Use Python + Appscript. It's the best of both worlds. You get the fantastic text processing abilities of Python with full Applescript like scripting. A lot of us always do our "Applescript" scripting in Python.Conscious
L
1

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.

Logan answered 10/5, 2011 at 13:49 Comment(2)
i dont know what to tell you Lri, its flawless. It works like a charm. Do you mind if i send this to the developer, so you can get credit for it? perhaps the dev can add it to the MarsEdit.app script library. Thanks again for looking out.Glaucescent
@Glaucescent Don't send it to the developers — they have better things to do.Logan
P
1

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"
Plantaineater answered 9/5, 2011 at 19:1 Comment(4)
im some what fluent in PHP, so assuming thats the case, how would that work out?Glaucescent
i'm looking to see how to do it with php I've done it before just don't remember and by default on mac os you can interact with php as much as you can run a php file and get the results unless you change some setting see this thread #4837415Plantaineater
testing both right now. The first suggestion wrapped the entire text with <p> and </p> tags rather than each paragraph. Ill tweak it a bit and let you know how it goes. Thanks for looking out. +1Glaucescent
@Glaucescent I'm not sure what kind of returns awk is expecting but when I ran it i got each line wrappedPlantaineater
L
1

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.

Logan answered 10/5, 2011 at 13:49 Comment(2)
i dont know what to tell you Lri, its flawless. It works like a charm. Do you mind if i send this to the developer, so you can get credit for it? perhaps the dev can add it to the MarsEdit.app script library. Thanks again for looking out.Glaucescent
@Glaucescent Don't send it to the developers — they have better things to do.Logan

© 2022 - 2024 — McMap. All rights reserved.