On Jul 05, 2021, at 16:13, Francisco Hirsch <[email protected] 
<mailto:[email protected]>> 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 FastScripts <https://www.red-sweater.com/fastscripts/>, Keyboard 
Maestro <https://www.keyboardmaestro.com/main/>, or another AppleScript runner 
app.)

Something like this:

-------------------------------------------------------------------------------------------
# Auth: Christopher Stone <[email protected] 
<mailto:[email protected]>>
# 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

-- 
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/0CB0A87A-4BAE-4B3E-927C-327FDB783E1E%40gmail.com.

Reply via email to