Hey Francisco,
This issue can be confusing, but as Rich says Text Factory are not designed to run AppleScripts per se – they're designed to run AppleScripts (and other tools) that transform text.
This means the last line of the handler must always return text. Like so:
-------------------------------------------------------------------------------------------
property LF : linefeed
on ApplyTextTransform(bbeditData)
return "Returned Data Start" & LF & bbeditData & LF & "Returned Data End"
end ApplyTextTransform
-------------------------------------------------------------------------------------------
And that means the AppleScript cannot process the front document, beyond its purview.
To do what you want you'll need to run your AppleScript from the BBEdit script menu (or FastScripts,
Keyboard Maestro, or another AppleScript runner app.)
Something like this:
# dCre: 2021/07/06 00:26
# dMod: 2021/07/06 00:51
# Appl: BBEdit, System Events
# Task: Run a BBEdit Text Factory on the Front Document from an AppleScript.
# : Save front document to a file name based on its own line 1's text.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @BBEdit, @System_Events, @Text_Factory, @Save
-------------------------------------------------------------------------------------------
# NOTE – ALWAYS use relative paths whenever possible.
# Hard-coded paths are a recipe for headaches in the future.
-------------------------------------------------------------------------------------------
set myHomePath to path to downloads folder as text
# The way I generally use relative-paths when working with aliases.
# set textFactory to alias ((path to application support from user domain as text) & "BBEdit:Text Filters:Text Factories:TEST ⇢ Change Case to UpperCase.textfactory")
# How to use a relative POSIX Path with System Events.
# In this use-case the file must already exist.
set textFactory to "~/Library/Application Support/BBEdit/Text Filters/Text Factories/TEST ⇢ Change Case to UpperCase.textfactory"
tell application "System Events" to set textFactory to POSIX path of disk item textFactory
tell application "BBEdit"
set theDoc to front text document
apply text factory textFactory to theDoc
set textOfLine1 to contents of line 1 of theDoc
if textOfLine1 ≠ "" then
set newFileName to textOfLine1 & ".txt"
set newFilePath to myHomePath & newFileName
save theDoc to file newFilePath
close front window
make new text document
else
error "Line 1 was blank – file was NOT saved!"
end if
end tell
-------------------------------------------------------------------------------------------
Personally I would not copy and paste the PDF data.
I'd either use the command line utility `pdftotext` from Xpdf Tools or AppleScriptObjC to perform the extraction.
I like `pdftotext`, because it has a `-layout` switch that does a very respectable job of preserving the original PDF's format – unlike copy/paste from Preview.app and extract text with AppleScriptObjC. This can be monumentally useful when parsing the text.