Hi Peter,

Good to know it worked and you could achieve what you were after.

Here is a version using a whose clause and error handling:

*use* AppleScript version "2.8"

*use* *scripting additions*

--

*try*

*tell* *application* "BBEdit"

*set* vDocuments *to* *its* *text documents* *whose* modified *is* *true*

*repeat* *with* vDocument *in* vDocuments

*save* vDocument

*end* *repeat*

*end* *tell*

*on* *error* aMessage number aErrorNumber

*if* aErrorNumber ≠ -128 *then* -- Error number -128 (User Cancelled).

*display dialog* aMessage

*end* *if*

*return*

*end* *try*


HTH


Jean
On Saturday, August 16, 2025 at 4:42:29 PM UTC+2 Peter Steiner wrote:

> Hello Jean
>
> Thanks for the hint, it worked! And I managed to create a script that does 
> what I want. If there are better or more elegant ways to achieve saving of 
> all open files I welcome all feedbacks!
>
> *on* applicationWillSwitchOut(theApp)
> *tell* *application* "BBEdit"
> *set* allProjects *to* *every* *project document*
> *repeat* *with* project *in* allProjects
> *set* currentlyOpenDocuments *to* *every* *text document* *of* *window* 
> *of* project
> *repeat* *with* doc *in* currentlyOpenDocuments
> *save* doc
> *end* *repeat*
> *end* *repeat*
> *end* *tell*
> *end* applicationWillSwitchOut
>
> Regards, Peter
>
> On Wednesday, August 13, 2025 at 7:32:22 PM UTC+2 jj wrote:
>
>> Hi Peter,
>>
>> Remove the colon and it should work.
>>
>> *use* AppleScript version "2.8"
>>
>> *use* *scripting additions*
>>
>> --
>>
>> *on* applicationWillSwitchOut(theApp)
>>
>> *tell* *application* "TextEdit"
>>
>> *activate*
>>
>> *make* new *document*
>>
>> *set* *text* *of* *front* *document* *to* "hello 
>> applicationWillSwitchOut"
>>
>> *end* *tell*
>>
>> *end* applicationWillSwitchOut
>>
>> HTH,
>>
>> Jean Jourdain
>> On Wednesday, August 13, 2025 at 6:22:10 PM UTC+2 Peter Steiner wrote:
>>
>>> Hello, I'm a seasoned programmer but pretty new to Applescript and 
>>> BBEdit, so please bear with me if I'm asking obvious stuff. 
>>>
>>> I asked support about auto-save and got a quick answer to look into the '
>>> applicationWillSwitchOut' attachment point.
>>>
>>> I then found these old threads mentioning applicationWillSwitchOut:
>>>
>>>    - https://www.mail-archive.com/[email protected]/msg14492.html 
>>>    <https://www.mail-archive.com/[email protected]/msg14492.html>
>>>    - https://www.mail-archive.com/[email protected]/msg15547.html 
>>>    <https://www.mail-archive.com/[email protected]/msg15547.html>
>>>
>>>
>>> but without examples (*"This is largely an exercise for the reader :-) 
>>> [...], and the group can probably help you with the scripting itself."*)
>>>
>>> To start dabbling with AppleScript I put this compiled script into the 
>>> mentioned folder (~/Library/Application Support/BBEdit/Attachment 
>>> Scripts/Application.scpt):
>>>
>>> on applicationWillSwitchOut:(theApp)
>>> tell application "TextEdit"
>>> activate
>>> make new document
>>> set text of front document to "hello applicationWillSwitchOut"
>>> end tell
>>> end applicationWillSwitchOut:
>>>
>>> (I made similar subroutines for the other 4 application attachment 
>>> points)
>>>
>>> I then started BBEdit, switched focus, then back to BBEdit and then 
>>> quit. Nothing happened with TextEdit, so I'm not sure if the scripts were 
>>> even run.
>>>
>>> Am I doing this wrong? How can I debug attachment scripts? I can't run 
>>> them directly in the Script Editor, can I? The manual is silent on this, 
>>> probably because mac users are supposed to know that?
>>>
>>> Or even better, has somebody already a script that saves open files when 
>>> quitting or switching out?
>>>
>>> Thanks in advance!
>>>
>>> Peter
>>>
>>

-- 
This is the BBEdit Talk public discussion group. If you have a feature request 
or believe that the application isn't working correctly, please email 
"[email protected]" rather than posting here. Follow @bbedit on Mastodon: 
<https://mastodon.social/@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 visit 
https://groups.google.com/d/msgid/bbedit/fa10aa20-9e14-44c8-9069-9a79cc68c92dn%40googlegroups.com.

Reply via email to