David,

This works 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"
                my trace("run")
                if vDocument's is FTP then
                    my trace("is FTP file")
                    set vFTPInfo to FTPInfo 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" & vHost & " " & 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 tell
        on error aMessage
            display alert aMessage
        end try
    end documentDidSave

HTH,

Jean

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/f317a6c4-1f6b-42a0-bb67-c9e28943997fn%40googlegroups.com.

Reply via email to