Incorporating an Applescript in a Text Factory

106 views
Skip to first unread message

Eros9

unread,
Nov 2, 2023, 1:30:49 PM11/2/23
to BBEdit Talk
This may be helpful to somebody. I have a text factory that is a chain of "Replace All" commands to create an html proof of an xml file. I wanted to add the file name to the proof, because it is crucial for correct processing of the xml (when it's uploaded to the system in question). Here's the Applescript, which is run at the start of the text factory:

on ApplyTextTransform(htmlTitle)
tell application "BBEdit"
activate
set fileName to (name of document 1) as text
set fileCont to (contents of document 1) as text
return ("<html>
<head>
<title>" & fileName & "</title>
</head>
" & fileCont)
end tell
end ApplyTextTransform

Then a grep replace command in the text factory finds the fileName and adds it as part of the html body as well. (I realize that the htmlTitle variable in the script is superfluous.)

PDeL

unread,
Nov 9, 2023, 8:28:08 PM11/9/23
to BBEdit Talk
Hi Eros9. This is indeed very interesting, and potentially very helpful for a different thing I'm trying to do.
But I don't understand HOW you run the AppleScript in the context of a Text Factory. In the user guide the only mention I see of AS in relation to a TF is the "Run AppleScript Filter" option,  which is documented as shown below. I'm kind of thrown off by the statement "This entry point should return the file’s contents as a Unicode string." Is this what you're using?

When you use the Run AppleScript Filter operation, your script should be written with an entry point named “ApplyTextTransform”. The input parameter to this entry point is a Unicode string containing the file’s contents. This entry point should return the file’s contents as a Unicode string (or something which can be directly coerced to one):
        on ApplyTextTransform (fileData)
        -- do something to fileData
        return fileData -- or some reasonable facsimile thereof
        end

Eros9

unread,
Nov 10, 2023, 8:30:00 AM11/10/23
to BBEdit Talk
Hi, PDeL -- My script returns "some reasonable facsimile thereof". I just skipped defining the variable "fileData" (or "htmlTitle" in my script) and returned the string itself. It might be confusing that the string includes line breaks, which the script editor actually displays as such; the string is bound by the parentheses. The main breakthrough in my grasping how the script works in the context of a text factory is that whatever it returns completely replaces the contents of the file being acted on, so the script has to read all the contents first, do its thing, and return them back. In my case, that was just adding an html title header with the file name. I hope I've been helpful!

Eros9

unread,
Nov 10, 2023, 10:45:06 AM11/10/23
to BBEdit Talk
I should change that variable htmlTitle back to fileData. Even though I don't actually use it, it is misleading.
Reply all
Reply to author
Forward
0 new messages