On Oct 04, 2021, at 18:30, Sonic Purity <[email protected]> wrote:
> 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.
Hey Sonic,
That's easy enough if you already know how, but not so much if you don't.
Recording is relatively useless – except for discovering the syntax for some
things that are really difficult to suss out. A recorded script very often
takes a lot of editing to convert from a very verbose and literal recording of
actions to something that rationally makes sense to user, computer, and task.
Here's a sample template. Note the underlined bold tags – these are
placeholders you can tab to in the open document.
<!DOCTYPE html>
<html>
<head>
<title><#TITLE#></title>
<meta name="generator" content="BBEdit Prerelease" />
</head>
<body>
<#HTML_BODY#>
</body>
</html>
You can use an AppleScript dialog to enter the title, but placeholders are
easier.
This script uses the template-path defined with property templateFilePath.
It also contains code to demonstrate the use of an embedded template
(commented-out).
--
Best Regards,
Chris
--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2021/10/04 20:07
# dMod: 2021/10/04 20:07
# Appl: BBEdit
# Task: Create a New HTML Document Using a Template File.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @ASObjC, @BBEdit, @Create, @HTML, @Document,
@Template
--------------------------------------------------------
use AppleScript version "2.4" --» Yosemite or later
use framework "Foundation"
use scripting additions
--------------------------------------------------------
property templateFilePath : "~/Documents/BBEdit
Documents/Templates/HTML_Template_01.txt"
property defaultWindowBounds : {202, 45, 1238, 900} --» {X1, Y1, X2, Y2}
Upper-Left, Lower-Right
--------------------------------------------------------
set templateFilePath to ((current application's NSString's
stringWithString:templateFilePath)'s stringByExpandingTildeInPath) as text
set templateText to read templateFilePath as «class utf8»
tell application "BBEdit"
# Create from a template FILE.
set newDoc to make new document with properties {text:templateText}
# Create using embedded template TEXT in handler getTemplateText().
# set newDoc to make new document with properties {text:my
getTemplateText()}
tell newDoc
if its source language ≠ "HTML" then set its source language to "HTML"
tell its window
if its bounds ≠ defaultWindowBounds then
set its bounds to defaultWindowBounds
end if
end tell
end tell
end tell
--------------------------------------------------------
# Template Text
--------------------------------------------------------
on getTemplateText()
return text 2 thru -1 of "
<!DOCTYPE html>
<html>
<head>
<title><#TITLE#></title>
<meta name=\"generator\" content=\"BBEdit Prerelease\" />
</head>
<body>
<#HTML_BODY#>
</body>
</html>
"
end getTemplateText
--------------------------------------------------------
--
This is the BBEdit Talk public discussion group. If you have a feature request
or need technical support, please email "[email protected]" rather than
posting here. Follow @bbedit on Twitter: <https://twitter.com/bbedit>
---
You received this message because you are subscribed to the Google Groups
"BBEdit Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/bbedit/623ADE4F-E213-4277-A714-3AA6FC838F44%40gmail.com.