I'm trying to script the Save Document command

31 views
Skip to first unread message

David Barto

unread,
May 7, 2022, 5:00:19 PM5/7/22
to BBEdit Talk

What I've got will run to the first 'asecho ' command and not reach the second.

What I'd like to do is create a backup of any file being edited on my local host even if it is a remote document. I'm quite sure that the fault is in my script and not in BBEdit. I searched the archives here and found nothing related to this problem.

Anyone smarter about AppleScript (and almost everyone here would fit that statement) care to point out the error of my ways?

Thanks

       David

-- text document doc

on documentDidSave(doc)

   -- do shell script "~/asecho 1 running"

   set fromFTP to doc is FTP

   -- do shell script "~/asecho 2 post fromFTP"

   if fromFTP then

       -- set path to URL of doc

       set _ftpinfo to FTPInfo of doc

       set _path to path of _ftpinfo

       set _host to host of _ftpinfo

       do shell script "~/asecho 3 " & quoted form of _host

       set u to URL of doc

       do shell script "~/asecho 4 " & u

       set p to POSIX path of _path

       do shell script "~/UnixEnvironment/src/script/backuptext " & p

   else

       set f to file of doc

       set p to POSIX path of f

       do shell script "~/UnixEnvironment/src/script/backuptext " & p

   end if

end documentDidSave


jj

unread,
May 8, 2022, 5:12:53 AM5/8/22
to BBEdit Talk
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 example snippet run from the Script Editor might help you.

    tell application "BBEdit"
        try
            set vDocument to first text document
            if vDocument's is FTP then
                set vFTPInfo to (vDocument's FTP Info) as record
                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}
            else
                if vDocument's modified then
                    save vDocument
                end if
                set vFile to vDocument's file
            end if
            log vFile
            set vPosixPath to POSIX path of (vFile as string)
            log vPosixPath
            do shell script "ls -al" & space & the quoted form of vPosixPath
        on error aMessage
            display alert aMessage
        end try
    end tell

HTH,

Jean Jourdain

David Barto

unread,
May 9, 2022, 11:02:17 AM5/9/22
to BBEdit Talk

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

Message has been deleted

jj

unread,
May 10, 2022, 2:32:17 AM5/10/22
to BBEdit Talk
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
Reply all
Reply to author
Forward
0 new messages