67848

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

Question:

Im currently using a script in MarsEdit.app which has a flaw. It checks the HTML document for cases where paragraphs are wrapped with

tags as follows:

-- If already starts with

, don't prepend another one if not {oneParagraph starts with "

"} then set newBodyText to newBodyText & "

" 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

tag the script wraps

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

, don't append another one if not (oneParagraph ends with "

") then set newBodyText to newBodyText & "

" end if set newBodyText to newBodyText & return

Example:

<h5>Foobar </h5>

becomes

<h5>Foobar </h5>

In another question <a href="https://stackoverflow.com/questions/5925439/applescript-and-starts-with-operator" rel="nofollow">Applescript and "starts with" operator</a>, @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 <a href="http://www.cs.helsinki.fi/u/lranta/pastebin/wrap_lines_with_tag.applescript" rel="nofollow">Wrap lines with tags on applescript</a>

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

I uploaded the entire script on pastebin. <a href="http://pastebin.com/mS3wyYQX" rel="nofollow">Pastebin: MarsEdit.app, wrap line with

tags script

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

Answer1:

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 "

" & v & "

" 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
<hr />

<a href="http://appscript.sourceforge.net/rb-appscript/index.html" rel="nofollow">Ruby appscript</a>:

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] = "

#{lines[i]}

" end doc.current_text.set(lines.join("\n"))
<hr />

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

Answer2:

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

\"$0\"

\" } ' 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"

Recommend

  • MongoDB ObjectIds exposure
  • Permission screen appears every time
  • Bokeh 0.7.1: Dynamically Add Plot to Bokeh-Server Generated Existing Page
  • I can't get setInterval() to work in Javascript
  • Better file search algorithm than creating a list of files
  • Adding a new element into the DOM with angularjs does not initiate it
  • How to save dynamically created textboxes and their values
  • Jquery UI Sortable, move item automatically
  • Most efficient way to replace lowest list values in dataframe in R
  • How to populate html table with info from list in django
  • List comprehension with if conditional to get list of files of a specific type
  • passing a default argument to a browserify module
  • NHibernate manually control fetching
  • vectorized indexing/slicing in numpy/scipy?
  • pip in virtualenv gets ConnectTimeoutError
  • Angular2 Response for preflight is invalid (redirect) from some GET requests
  • Why cepheus don't send int without quotes to orion?
  • RxJava debounce by arbitrary value
  • Run multiple queries from 1 SQL file showing result in multiple tables
  • Silverlight DependencyProperty.SetCurrentValue Equivalent
  • wxPython: displaying multiple widgets in same frame
  • Zurb Foundation _global.scss meta styles for js?
  • How can I enlarge video fullscreen without the affected interface project in as3?
  • Installed module is empty
  • Yii2: Config params vs. const/define
  • Ajax Loaded meta Tags
  • Excel - Autoshape get it's name from cell (value)
  • Check if a string to interpolate provides expected placeholders
  • Join two tables and save into third-sql
  • How to model a transition system with SPIN
  • How to make Safari send if-modified-since header?
  • RestKit - RKRequestDelegate does not exist
  • Traverse Array and Display in markup
  • How to delete a row from a dynamic generate table using jquery?
  • Python: how to group similar lists together in a list of lists?
  • KeystoneJS: Relationships in Admin UI not updating
  • how does django model after text[] in postgresql [duplicate]
  • Cant find why the layout is getting smaller
  • Is there any way to bind data to data.frame by some index?
  • Django query for large number of relationships