TextFactory and then AppleScript works; AppleScript in TextFactory doesn't

163 views
Skip to first unread message

Francisco Hirsch

unread,
Jul 5, 2021, 5:13:57 PM7/5/21
to bbe...@googlegroups.com
I have a TextFactory and an AppleScript.
I do the following:
Open pdf in Acrobat
Select all, copy, paste in a new BBEdit Window, process with the TextFactory, run the AppleScript clicking in BBEdit Scripts,
And everything works.
But if I include the AppleScript as the last line in the TextFactory:
It saves the original text copied from the pdf with it’s first line as it’s name.
In other words, it’s as if the script (up to AppleScript line) did not work or the text was substituted in the AppleScript.
I’m baffled.
The AppleScript:
tell application "BBEdit"
activate
set firstLine to line 1 of document of text window 1
copy firstLine
set FileName to the clipboard
set FileName to FileName & ".txt"
set FileName to "iMac HD:Users:MyUser:Dropbox:Salta 2021 candidatos:" & FileName
save text document 1 to file FileName without saving as stationery
close project window 1 saving no
make new text document
end tell

Regards,,
Francisco
--
Lic. Francisco A. Hirsch
https://www.fiscaldemesa.com.ar
Twitter: https://twitter.com/fiscaldemesa
 

Rich Siegel

unread,
Jul 5, 2021, 5:20:22 PM7/5/21
to bbe...@googlegroups.com
On 5 Jul 2021, at 17:13, Francisco Hirsch wrote:

> I have a TextFactory and an AppleScript.
> I do the following:
> Open pdf in Acrobat
> Select all, copy, paste in a new BBEdit Window, process with the
> TextFactory, run the AppleScript clicking in BBEdit Scripts,
> And everything works.
> But if I include the AppleScript as the last line in the TextFactory:
> It saves the original text copied from the pdf with it’s first line
> as it’s name.
> In other words, it’s as if the script (up to AppleScript line) did
> not work or the text was substituted in the AppleScript.
> I’m baffled.

Each step in a text factory is intended to process the text that
resulted from the previous step (or the file's contents, if it's the
first step).
The user manual says:

===

• 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

====

Thus, text factories are not a way to chain together the execution of
arbitrary AppleScripts, *unless* the scripts you're running explicitly
process text provided as input to the text factory.

R.

--
Rich Siegel Bare Bones Software, Inc.
<sie...@barebones.com> <https://www.barebones.com/>

Someday I'll look back on all this and laugh... until they sedate me.

Francisco Hirsch

unread,
Jul 6, 2021, 4:35:35 PM7/6/21
to bbe...@googlegroups.com
Thanks!, Rich

Christopher Stone

unread,
Jul 7, 2021, 2:26:30 PM7/7/21
to BBEdit-Talk
On Jul 05, 2021, at 16:13, Francisco Hirsch <listas...@gmail.com> wrote:
I have a TextFactory and an AppleScript.
I do the following:
Open pdf in Acrobat
Select all, copy, paste in a new BBEdit Window, process with the TextFactory, run the AppleScript clicking in BBEdit Scripts, and everything works.
But if I include the AppleScript as the last line in the TextFactory:
It saves the original text copied from the pdf with it’s first line as it’s name.
In other words, it’s as if the script (up to AppleScript line) did not work or the text was substituted in the AppleScript.


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 FastScriptsKeyboard Maestro, or another AppleScript runner app.)

Something like this:

-------------------------------------------------------------------------------------------
# Auth: Christopher Stone <script...@thestoneforge.com>
# 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.

--
Best Regards,
Chris

Reply all
Reply to author
Forward
0 new messages