I cannot figure out how to use AppleScript to create a new HTML document from an existing template file, as i easily do manually via ^⌘N then select my template then type in the title in the Title field and tap Create.
Thank you Chris and Jean for your responses. Jean’s in particular suggests to me it’s time to take a step or several back and share with you a big picture description of the overall automation project, since i may be going about things inefficiently due to my inexperience in some areas.
Here is an overview me and of what i’m doing, in English:
My Background
Despite my EECS degree, i’m more EE than CS. It was long enough ago that the languages i dealt with were Basic and Fortran. From those, i have an understanding of loops, conditional branches, variables, constants, etc. Apart from AppleScript, i haven’t dealt with object-oriented programming. I learned some Unix at UC Berkeley decades before Apple got into it, hence i’m comfortable on the command line. I’ve written some very simple, very short shell scripts.
I’m good with HTML and CSS, but for various reasons have avoided Javascript, having done almost no work with it. I haven’t used any other programming or scripting languages.
In terms of AppleScript, i’m at neophyte level, or just beyond. I’ve made a few short, basic ones on my own. This project is by far the most ambitious i’ve attempted. Even with resources like the BBEdit User Manual’s section on scripting the clipboard, i still can’t get a basic clipboard copy to work (for one example).
Project: Big Picture Overview
Convert my master fiction novel manuscripts into web pages for my personal site.
1) Write, edit, finalize story in TextEdit (TE). Result: master manuscript in RTF format.
2) Use TE to convert to HTML. Result: One HTML document which is syntactically-correct and validates, but is semantically a mess.
3) Process TE HTML document to make it semantic and have all the classes etc. i require for my CSS.
4) Create a new HTML document in BBEdit for each story chapter from a template, with all the header and navigation stuff.
5) Paste the chapter HTML from the TE HTML document into the body HTML area of the newly-created BBEdit individual chapter HTML document. Populate some navigation information.
6) Repeat 4 & 5 for each chapter.
7) Create a story main/TOC page from a different BBEdit HTML document template. Paste in Chapter titles and links from a document made around step 4.
8) Populate remaining navigation information in all the chapter HTML files from finalized information from the TOC document.
Note: i write my stories in TE—that’s non-negotiable. Having experimented with working with the original TE RTF in BBEdit, i quickly found that wholly unworkable. Having experimented with various options to get from TE RTF into HTML, i found TE’s “built in” Cocoa HTML Writer the least-worst option.
Project: Manual or Semi-Automated Processing
Step 1) above is straightforward and outside the scope of this discussion—no automation possible nor needed.
Step 2) above is one fast and straightforward menu command—no automation needed nor desired.
Step 3) is a Very Big Deal. I start with TE HTML that looks generally like this sample:
<p class="p1"><b>Dating Dilemmas</b></p>
<p class="p2"><br></p>
<p class="p4"><br></p>
<p class="p1">***** The Big Step *****</p>
<p class="p3">Nate was nervous—no question. […long paragraph text here…]</p>
<p class="p3">He wasn’t shy about his FAness and wasn’t shy at all behind a technology wall.[…long paragraph text here…]</p>
<p class="p4"><br></p>
<p class="p3">This was the 3rd. time he’d come out to the monthly Gravitational Goddesses Get-Together. […long paragraph text here…]</p>
… on and on for some 700,000 characters/130,000 words
Once i’m done, before splitting into chapters, this same sample looks like:
<h1>Dating Dilemmas</h1>
<div class="subhead">by Sonic Purity</div>
<hr>
<main>
<h2>The Big Step</h2>
<p class="narr">Nate was nervous—no question. […long paragraph text here…]</p>
<p class="narr">He wasn’t shy about his FAness and wasn’t shy at all behind a technology wall.[…long paragraph text here…]</p>
<p class="narr gap">This was the 3rd. time he’d come out to the monthly Gravitational Goddesses Get-Together.[…long paragraph text here…]</p>
There are about 34 steps in Step 3), covering all sorts of special CSS formatting not shown in my brief example. I’ve already automated what i can with Text Factories. A number of things have to be done manually.
When i’m done, there’s only one <h1> in the entire TE HTML document, which is the story title. Each chapter starts with <h2> and runs until the next opening <h2>—and Chris will be familiar with this from a question i asked here several weeks ago.
Steps 4-8 i’ve been doing manually/partially automated. They’re what i’m trying to more fully automate now, as most of it is repetitive work with predictable, repeatable actions. Here’s how i do it manually:
Somewhere between Steps 3 & 4: Find All chapter headings <h2>[content]</h2>, Extract to new document. Do some pre-processing for their eventual use on the TOC page and to get a story total chapter count from the line count. (This part is already automated.)
Step 4: New HTML document in BBEdit, selecting my individual story chapter template and entering the story title in the dialog. Using Includes, BBEdit fills in the current year (for copyright) and the story title in 3 locations.
I create a new folder in Finder for the new story (at a predictable location), using the story name adjusted to be URI-friendly (remove spaces and disallowed characters), sometimes shortened. (I’ve already successfully automated this part of the process [in this paragraph] in AppleScript.) I save the new HTML chapter document with the same name as the enclosing folder, other than C1.html appended at the end.
Step 5: I select some placeholder text in the just-saved document (which really ought to be just one Include-style placeholder word in the template). Switch back to the source TE HTML document. Select all the first chapter text (working great, Chris!), copy. Switch back to the new chapter document, paste and save.
Remaining in the new chapter HTML document: Fill in the current chapter number in one place, and the previous and upcoming chapter numbers (current - 1 and current +1) in 2 places, for navigation. For the first chapter, remove the 2 Previous chapter navigation links. For the last chapter remove the 2 Next chapter navigation links.
Step 6: Repeat all the above in Steps 4 & 5.
Step 7: Open New HTML document with template for story home/main/TOC (one entity—can’t decide what to call it) page. Finish formatting the extracted chapters document into list item entries, copy/paste into placeholder area of HTML list in this new document.
Manually update information that cannot be automated (e.g. exciting story synopsis that so far to this point is only in my head).
Do some more find/replace (which can be automated).
Manual work inserting a picture relevant to the story (because manual adjustments for visual aesthetics are needed).
Step 8: Do a bunch of multi-file Find/Replace in the individual chapter and TOC files to complete navigation links throughout. (This is partly automated and i look forward to making it fully so.)
*******************
That’s how i’ve been doing things manually/semi-automated. It’s clear to me that re-sequencing the steps is in some cases necessary for automation and in many other cases is more efficient. For example things can easily be re-jiggered to first complete all the automated parts of Steps 7 & 8, leaving all the manual stuff for me to do at one time at the end.
My Question, related to Jean’s suggestion to use AppleScripts called from BBEdit templates
Is there any point in doing that, with my initial conditions and this sort of workflow? How would that even work? To me, my mind jumps to doing as much as possible with a couple of AppleScripts (more than one because of some essential manual work that cannot be done at the start or end of automated processing), having those call the templates or otherwise plant template content into newly-created HTML documents.
Is my current approach to automation wrong-headed/inefficient? If so, what would be a more optimized approach?
I’ve read the AppleScript Language Guide all the way through. Problem is, until i tried to look up the use control statement, i didn’t realize there was anything newer than the 2008 version for 10.5 Leopard. I was looking for PDFs, because i get nervous with Apple-provided documentation online, since it sometimes vanishes when Apple decides no one needs it any more.
Chris: where does that impressive NSString's stringWithString stuff come from? I haven’t seen documentation on that anywhere.
Thanks in advance for any suggestions.
))Sonic((
On Oct 07, 2021, at 23:11, Sonic Purity <sonic...@gmail.com> wrote:
Note: i write my stories in TE—that’s non-negotiable.