AppleScript to Save, but only if file has been saved before (aka is not “untitled text”?

96 views
Skip to first unread message

TJ Luoma

unread,
Aug 23, 2018, 11:22:20 AM8/23/18
to BBEdit List
For a long time I have used this AppleScript that I use with Keyboard Maestro to automatically save the front-most document:

if application "BBEdit" is running then
    try
   tell application "BBEdit"
  tell document 1
save
  end tell
   end tell
    end try
end if

(I use this with Keyboard Maestro so that when BBEdit “deactivates”, the file will be saved. I have since learned that BBEdit has a feature to do this itself, but I've been using Keyboard Maestro for a long time, so I figured I'd stick with it.)

The problems with this are two-fold:

1. If the file has not been saved before, BBEdit will (understandably) bounce in the dock to tell me to name the file which it is trying to save. I would rather not bother trying to save as-yet-unsaved documents when BBEdit deactivates.

2. Perhaps more significantly, this AppleScript only saves the front document, and I'd really much prefer that it do an “Save All”… but, again, only for files which can be saved without user-intervention.

Despite all my best efforts, I still don't understand AppleScript very well (and my efforts at Googling have not been productive), but I assume there’s a way to accomplish #1, but I can’t figure it out. There must be some way to tell BBEdit to save if the window title doesn’t start with “untitled text” or something like that.

I assume that #2 is so complicated as to be un-attainable, but I figured I’d ask, and hope that maybe someone who is great at AppleScript might have a chance to prove me wrong ;-)

Thanks for any advice you may be able to offer.

Tj

--
TJ Luoma
TJ @ MacStories
Personal Website: luo.ma (aka RhymesWithDiploma.com)
Twitter: @tjluoma

Christopher Stone

unread,
Aug 23, 2018, 11:37:56 AM8/23/18
to BBEdit-Talk
On 08/23/2018, at 10:21, TJ Luoma <luo...@gmail.com> wrote:
(I use this with Keyboard Maestro so that when BBEdit “deactivates”, the file will be saved. I have since learned that BBEdit has a feature to do this itself, but I've been using Keyboard Maestro for a long time, so I figured I'd stick with it.)

The problems with this are two-fold:

1. If the file has not been saved before, BBEdit will (understandably) bounce in the dock to tell me to name the file which it is trying to save. I would rather not bother trying to save as-yet-unsaved documents when BBEdit deactivates.

2. Perhaps more significantly, this AppleScript only saves the front document, and I'd really much prefer that it do an “Save All”… but, again, only for files which can be saved without user-intervention.


Hey TJ,

Something like this should do the job:

----------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2018/08/23 10:35
# dMod: 2018/08/23 10:35 
# Appl: BBEdit
# Task: Save modified on-disk documents.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @BBEdit, @Save, @Modified, @On-Disk, @OnDisk, @Documents
----------------------------------------------------------------

tell application "BBEdit"
    set docList to (documents whose on disk is true and modified is true)
    repeat with theDoc in docList
        save theDoc
    end repeat
end tell

----------------------------------------------------------------

NOTE – this has only had light testing.

--
Best Regards,
Chris

TJ Luoma

unread,
Aug 23, 2018, 2:07:32 PM8/23/18
to BBEdit List
That seems to work perfectly! Thank you! 

Because the macro can run after BBEdit has _quit_ (KM can't always tell the difference, and although I could make a check for "if BBEdit is running" in Keyboard Maestro, I'd rather do it in AppleScript), I modified it to be this:

if application "BBEdit" is running then

try

tell application "BBEdit"

set docList to (documents whose on disk is true and modified is true)

repeat with theDoc in docList

save theDoc

end repeat

end tell

end try

end if


I'm not sure why the 'try / end try' was there in my original Keyboard Maestro macros, but it doesn't seem to hurt, so I've left it here too.


Many thanks!
--
This is the BBEdit Talk public discussion group. If you have a
feature request or would like to report a problem, please email
"sup...@barebones.com" rather than posting to the group.
Follow @bbedit on Twitter: <http://www.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+un...@googlegroups.com.
To post to this group, send email to bbe...@googlegroups.com.
Visit this group at https://groups.google.com/group/bbedit.

Rich Siegel

unread,
Aug 23, 2018, 2:39:25 PM8/23/18
to bbe...@googlegroups.com
On 8/23/18 at 2:06 PM, luo...@gmail.com (TJ Luoma) wrote:

>*set* docList *to* (*documents* *whose* on disk *is* *true* *and* modified
>*is* *true*)

I recommend that you use "text documents" here, in order to
avoid unintended consequences.

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

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

Christopher Stone

unread,
Aug 23, 2018, 10:04:39 PM8/23/18
to BBEdit-Talk
On 08/23/2018, at 13:39, Rich Siegel <sie...@barebones.com> wrote:
On 8/23/18 at 2:06 PM, luo...@gmail.com (TJ Luoma) wrote:

*set* docList *to* (*documents* *whose* on disk *is* *true* *and* modified
*is* *true*)

I recommend that you use "text documents" here, in order to avoid unintended consequences.


Hey Rich,

I left that broad intentionally, because TJ didn't specify he wanted to avoid same, nevertheless you're probably right.  :)



I mostly use text documents and shell worksheets myself.

Where do you see the most likelihood of unintended consequences occurring?



One could take safety a step further and require an Auto-Save-ON setting in the document to be saved.

----------------------------------------------------------------

tell application "BBEdit"

    

    set docList to (documents whose on disk is true and modified is true)

    

    repeat with theDoc in docList
        try
            if (contents of lines 1 thru 5 of theDoc) contains "Auto-Save-ON" then
                save theDoc
            end if
        end try
    end repeat

    

end tell

----------------------------------------------------------------

I'm limiting the “header text” to the first 5 lines above, but that's easily adjusted.

One could automate the emplacement of the auto-save-setting:

----------------------------------------------------------------

tell application "BBEdit"
    tell front text document
        tell (first line whose contents is "")
            set its contents to "# Auto-Save-ON" & linefeed
        end tell
    end tell
end tell

----------------------------------------------------------------

One could check the doc-type and use the appropriate comment character for that type.

Many things are possible with BBEdit and a little bit of AppleScript.

--
Take Care,
Chris

Reply all
Reply to author
Forward
0 new messages