I’m in the process of setting up a new text-heavy website and recognize I need some scripted routines to automate the process. Assistance in the scripting appreciated.
The word processing is being done in Scrivener, an excellent Mac-only writing environment, superior to MS Word and similar environments. Scrivener exports into not only Word format but also HTML, and its automated HTML generates pretty good CSS. However, with 50 to 100 very similar documents I’d like them all to point to the same CSS style formating file where I can tweak the CSS standards beyond the typography possible in Scrivener.
Despite the fact I have very low level skills in AppleScript, I’ve created an AppleScript to open the HTML files in BBEdit and automate some standardization. As well this routine, it needs to go further. Here’s a fragment of coding that does work. It’s a simple search-and-replace routine.
tell application "BBEdit 6.5.3"
activate
replace "class=\"p1\"" using "class=\"issue\"" searching in text 1 of text window 1 options {search mode:literal, starting at top:false, wrap around:false, reverse:false, case sensitive:false, match words:false, extend selection:false}
end tell
I need to amplify this to look for unpredictable stuff that sits between two predictable delimiters ” say, “<style” and “” and either replace all the stuff between the delimiters with something else, or remove the delimiters and the stuff in between.
I’ve been told by some I need to learn and use Regular Expressions (RegEx) to accomplish this. However, that’s something like learning to fly a jet fighter just to go down to the corner store for a litre of milk ” way too much power and complexity for a fairly simple task.
Others have told me “offset” tools in AppleScript will accomplish the task. I sense they may be right, but my knowledge of AppleScript syntax is so primitive I don’t know how to adapt the coding snippets they’ve supplied me ” specifically how to identify the two delimiters, how to identify what should replace what lies between the delimiters and how to get the code to work within BBEdit.
Here’s one of two “offset” routines I’ve been supplied. I need help in identifying whether this code is actually useful for the task at hand, and equally important, how to adapt it appropriately. My experiments have all run into inexplicable error messages. To repeat: how to identify the two delimiters, how to identify what should replace what lies between the delimiters and how to get the code to work within BBEdit, applying the routine to 50 and more HTML documents.
on run -- example
display dialog "\"" & (GetSubText of "This is some testing text" from "some " to " text") & "\""
end run
to GetSubText of SomeText from StartItem to enditem
(*
get a substring from SomeText, from StartItem to EndItem
parameters - SomeText [mixed]: the text to get the substring from
StartItem [mixed]: the starting item (or 1 for the beginning)
EndItem [mixed]: the ending item (or -1 for the end)
returns [text]: the substring, or "" if not found
*)
set SomeText to SomeText as text
if StartItem is in {1, "1", ""} then
set Here to 1
else
offset of StartItem in SomeText
if result is 0 then
return ""
else
set Here to result + (length of StartItem)
end if
end if
if enditem is in {-1, "-1", ""} then
set There to -1
else
offset of enditem in (text Here thru -1 of SomeText)
if result is 0 then
return ""
else
set There to (Here + result) - 2
end if
end if
return (text Here thru There of SomeText)
end GetSubText
Much appreciated.