David,

A couple of reasons your script is not working:

  • `if vDocument is FTP then`
    
    As this is not in a `tell application "BBEdit` block, AppleScript 
interprets
    the `is` as the equals operator `=` as in `if Document = FTP then`.
    To interpret the `is FTP` as a BBEdit `Document` property AppleScript 
needs the 
    BBEdit terminology which is imported by the `tell application "BBEdit"`.
    
    There is a second issue. `is FTP` is a property of `vDocument`.
    This should be stated as `is FTP of vDocument` or in the shorter 
possessive
    form `vDocument's is FTP` (note the `'s`).
    
  • `FTPInfo`
    
    This property of `Document` should be spelled `FTP info` in two words 
as 
    indicated in the BBEdit terminology.
    
Here is a snippet that seems to work on my setup:

    use AppleScript version "2.7"
    use scripting additions
    --
    on trace(aMessage)
        set vDesktopFolder to POSIX path of (path to desktop folder as 
string)
        set vTraceFile to vDesktopFolder & "trace.txt"
        do shell script ("echo" & space & (the quoted form of aMessage) & 
">>" & (the quoted form of vTraceFile))
    end trace
    --
    on documentDidSave(vDocument)
        try
            tell application "BBEdit"
                if class of vDocument is text document then
                    if vDocument's is FTP then
                        set vFTPInfo to FTP Info of vDocument
                        set vFile to vFTPInfo's file
                        set vHost to vFTPInfo's host
                        set vURL to vDocument's URL
                        my trace("is FTP file" & linefeed & vHost & 
linefeed & vURL)
                    else
                        my trace("is NOT FTP file")
                        set vFile to vDocument's file
                    end if
                    set vPosixPath to POSIX path of (vFile as string)
                    my trace(vPosixPath)
                    my trace("")
                end if
            end tell
        on error aMessage
            display alert aMessage
        end try
    end documentDidSave

HTH,

Jean Jourdain

On Monday, May 9, 2022 at 5:02:17 PM UTC+2 kdb...@gmail.com wrote:

> Jean, your script works when run as an osascript, however I'm trying to 
> run the script as a BBEdit OnDocumentSave
>
> In that case I've fixed the syntax errors (as best I can, probably 
> incorrectly) and it only runs to the first 'do shell script', nothing after 
> that executes.
>
> $HOME/asecho is just "echo $* >> $HOME/Desktop/trace" for logging purposes.
>
>      David
>
>
> -- text document doc
>
> *on* documentDidSave(vDocument)
>
>    *do shell script* "$HOME/asecho run"
>
>    *if* vDocument *is* FTP *then*
>
>        *do shell script* "$HOME/asecho is FTP file"
>
>        *set* vFTPInfo *to* FTPInfo *of* vDocument
>
>        *set* vFile *to* vFTPInfo's *file*
>
>        *set* vHost *to* vFTPInfo's host
>
>        -- set vURL to vFTPInfo's URL -- WARNING: triggers an error even 
> though the property exists and is visible in the results pane.
>
>        *set* vURL *to* vDocument's *URL*
>
>        *log* {vHost, vURL}
>
>        *do shell script* "$HOME/asecho is FTP file" & vHost & " " & vURL
>
>    *else*
>
>        *do shell script* "echo is NOT FTP file"
>
>        *if* vDocument's modified *then*
>
>            *save* vDocument
>
>        *end* *if*
>
>        *set* vFile *to* vDocument's *file*
>
>        *set* vPosixPath *to* POSIX path *of* (vFile *as* *string*)
>
>        *do shell script* "$HOME/asecho is NOT FTP file" & *the* quoted 
> form *of* vPosixPath
>
>    *end* *if*
>
>    *log* vFile
>
>    *set* vPosixPath *to* POSIX path *of* (vFile *as* *string*)
>
>    *log* vPosixPath
>
>    *do shell script* "$HOME/asecho run ls on " & *the* quoted form *of* 
> vPosixPath
>
>    *do shell script* "ls -al" & space & *the* quoted form *of* vPosixPath
>
> *end* documentDidSave
>
> On Sunday, May 8, 2022 at 2:12:53 AM UTC-7 jj wrote:
>
>> Hi David,
>>
>> `try ... on error`  blocks  are very useful for debugging scripts.
>>
>> Before porting it to a Document Script, test your logic in the Script 
>> Editor where you can use the `log` and `display alert/dialog` commands 
>> to debug.
>>
>> Use `the quoted form of`  for paths included  in your `do shell script` 
>> commands, otherwise any space in the path will break your command.
>>
>>
>>>

-- 
This is the BBEdit Talk public discussion group. If you have a feature request 
or need technical support, please email "supp...@barebones.com" 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 bbedit+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/bbedit/e0486b00-4e3a-41e9-8918-21d8240c538an%40googlegroups.com.

Reply via email to