Saturday, July 4, 2020

#1 2020-04-24 09:36:00 am

ChangeAgent
Member
From:: Scotland
Registered: 2008-03-07
Posts: 333

script no longer quitting all open apps under 10.15

Applescript:

-- get list of open apps
tell application "System Events" to set theapps to name of every application process whose visible is true and name is not "Finder"
set keepapp to {"AppleScript Editor"}
-- quit each app
repeat with closeapp in theapps
   if closeapp is not in keepapp then quit application closeapp
end repeat

used to work fine but does not touch certain applications running under 10.15.4. 

any input as to why?

Offline

 

#2 2020-04-24 09:45:25 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 530

Re: script no longer quitting all open apps under 10.15

AppleScript Editor should probably be Script Editor, but otherwise the script works OK for me. What apps don't quit and what error messages or prompts, if any, do you receive? Also, how are you running the script?

I don't think it's the issue, but you may want to try the following, which I've been using under Catalina without issue:

Applescript:

set doNotQuit to {"Finder", "Script Editor"}

tell application "System Events"
   set activeApps to name of every process whose background only is false
end tell

repeat with anApp in activeApps
   try
       if anApp is not in doNotQuit then tell application anApp to quit
   end try
end repeat

Last edited by peavine (2020-04-24 10:16:04 am)


2018 Mac mini - macOS Catalina

Online

 

#3 2020-04-24 10:09:57 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4533

Re: script no longer quitting all open apps under 10.15

ChangeAgent wrote:

Applescript:

-- get list of open apps
tell application "System Events" to set theapps to name of every application process whose visible is true and name is not "Finder"
set keepapp to {"AppleScript Editor"}
-- quit each app
repeat with closeapp in theapps
   if closeapp is not in keepapp then quit application closeapp
end repeat

used to work fine but does not touch certain applications running under 10.15.4. 

any input as to why?




May you check the contents of the list theapps?

Maybe the name of some process doesn't match the real name of the application.
Here having LibreOffice running I tested with:

Applescript:

-- get list of open apps
tell application "System Events" to set theapps to name of every application process whose visible is true and name is not "Finder"
--> {"fake_mojave", "TextEdit", "uBlock", "Script Editor", "Safari", "Mail", "Hex Fiend", "soffice"}
set keepapp to {"fake_mojave", "TextEdit", "uBlock", "Script Editor", "Safari", "Mail", "Hex Fiend"}
repeat with closeapp in theapps
   if closeapp is not in keepapp then quit application closeapp
   --> choose application as «class aprl» with prompt "Où se trouve soffice ?"
   --> error number -128
end repeat

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 24 avril 2020  18:04:10

Offline

 

#4 2020-04-24 11:12:06 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 1259

Re: script no longer quitting all open apps under 10.15

I wrote this enhanced version. It is much flexible and stable. It works with "strange" processes (as process "soffice" of LibreOffice.app) too. It will work as script or as application equally nice:

Applescript:


property keepApps : {name of current application, "Finder"}

tell application "System Events" to ¬
   set appNames to displayed name of (file of every application process whose visible is true)

repeat with appName in appNames
   if not (appName is in keepApps) then tell application appName to quit
end repeat

Last edited by KniazidisR (2020-04-24 11:31:14 pm)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#5 2020-04-25 04:46:18 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4533

Re: script no longer quitting all open apps under 10.15

KniazidisR wrote:

I wrote this enhanced version. It is much flexible and stable. It works with "strange" processes (as process "soffice" of LibreOffice.app) too. It will work as script or as application equally nice:

Applescript:


property keepApps : {name of current application, "Finder"}

tell application "System Events" to ¬
   set appNames to displayed name of (file of every application process whose visible is true)

repeat with appName in appNames
   if not (appName is in keepApps) then tell application appName to quit
end repeat




Are you sure ?
I ran it and I got :

Applescript:


-- don't click that, it's just a history lo !
tell application "System Events"
   get displayed name of file of every application process whose visible = true
       --> {"tout quitter.app", "Finder.app", "fake_mojave.app", "uBlock.app", "Xcode.app", "BBEdit.app", "Mail.app", "Safari.app", "LibreOffice.app", "Éditeur de script.app"}
end tell
tell application "tout quitter"
   quit

and entered an infinity loop ended by a force quit.
tout quitter.app is an applet.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) samedi 25 avril 2020 12:46:02

Offline

 

#6 2020-04-25 05:43:57 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 1259

Re: script no longer quitting all open apps under 10.15

Yvan Koenig wrote:

Are you sure ?


I am sorry - I forgot that from the last topic I still have the Finder setting "not to show name extensions". The correct script is as follows:

Applescript:


property keepApps : {name of current application, "Finder"}

tell application "System Events" to ¬
   set appNames to name of (file of every application process whose visible is true)

repeat with appName in appNames
   set appName to text 1 thru -5 of appName
   if not (appName is in keepApps) then tell application appName to quit
end repeat


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#7 2020-04-25 07:07:28 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4533

Re: script no longer quitting all open apps under 10.15

KniazidisR wrote:
Yvan Koenig wrote:

Are you sure ?


I am sorry - I forgot that from the last topic I still have the Finder setting "not to show name extensions". The correct script is as follows:

Applescript:


property keepApps : {name of current application, "Finder"}

tell application "System Events" to ¬
   set appNames to name of (file of every application process whose visible is true)

repeat with appName in appNames
   set appName to text 1 thru -5 of appName
   if not (appName is in keepApps) then tell application appName to quit
end repeat



It changed nothing. With two added informative instructions it returned:

Applescript:

tell application "System Events"
   get name of file of every application process whose visible = true
       --> {"tout quitter.app", "Finder.app", "fake_mojave.app", "TextEdit.app", "uBlock.app", "Xcode.app", "BBEdit.app", "Mail.app", "Safari.app", "LibreOffice.app", "Script Editor.app"}
end tell
(*appName before : tout quitter.app*)
(*appName stripped : tout quitter*)
tell application "tout quitter"
   quit

and continue its infinite execution forcing me to force quit the Editor.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) samedi 25 avril 2020 15:07:18

Offline

 

#8 2020-04-25 07:36:12 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6395

Re: script no longer quitting all open apps under 10.15

ChangeAgent wrote:

any input as to why?



If an app supports App Nap, a feature introduced in Mojave where an app can be put into a paused state, it will not be regarded as visible while napping.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#9 2020-04-25 07:46:43 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6395

Re: script no longer quitting all open apps under 10.15

KniazidisR wrote:

Applescript:


   set appName to text 1 thru -5 of appName



There's no guarantee that the app name and its file name are the same. Acrobat is a good example of this.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#10 2020-04-25 07:52:57 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6395

Re: script no longer quitting all open apps under 10.15

Here's an ASObjC version:

Applescript:

use AppleScript version "2.5" -- macOS 10.11 or later
use framework "Foundation"
use framework "AppKit"
use scripting additions

set theApps to current application's NSWorkspace's sharedWorkspace()'s runningApplications()
set thePred to current application's NSPredicate's predicateWithFormat:"activationPolicy == 0 AND NOT (localizedName IN %@)" argumentArray:{{"Finder", "Script Editor"}} -- edit to suit
set theApps to theApps's filteredArrayUsingPredicate:thePred
repeat with anApp in theApps
   anApp's terminate()
end repeat

It gets the list of apps much faster than System Events's every process whose background only is false, but its main advantage is that app/process names aren't required (except for the exceptions), so any differences are don't matter.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#11 2020-04-25 08:05:52 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 1259

Re: script no longer quitting all open apps under 10.15

Yvan Koenig wrote:



It changed nothing. With two added informative instructions it returned:

Applescript:

tell application "System Events"
   get name of file of every application process whose visible = true
       --> {"tout quitter.app", "Finder.app", "fake_mojave.app", "TextEdit.app", "uBlock.app", "Xcode.app", "BBEdit.app", "Mail.app", "Safari.app", "LibreOffice.app", "Script Editor.app"}
end tell
(*appName before : tout quitter.app*)
(*appName stripped : tout quitter*)
tell application "tout quitter"
   quit

and continue its infinite execution forcing me to force quit the Editor.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) samedi 25 avril 2020 15:07:18



You should keep 1 app at least - the current application.

Last edited by KniazidisR (2020-04-25 08:25:39 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#12 2020-04-25 08:40:03 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 1259

Re: script no longer quitting all open apps under 10.15

I really liked the solution from Shane Stanley. It is truly more universal and faster than mine. I will just take it to my library without having to edit the name of the editor or applet itself:

Applescript:


use AppleScript version "2.5" -- macOS 10.11 or later
use framework "Foundation"
use framework "AppKit"
use scripting additions
property keepApps : {name of current application, "Finder"}

set theApps to current application's NSWorkspace's sharedWorkspace()'s runningApplications()
set thePred to current application's NSPredicate's predicateWithFormat:"activationPolicy == 0 AND NOT (localizedName IN %@)" argumentArray:{keepApps}
set theApps to theApps's filteredArrayUsingPredicate:thePred
repeat with anApp in theApps
   anApp's terminate()
end repeat

Last edited by KniazidisR (2020-04-25 08:40:53 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#13 2020-04-25 09:04:02 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4533

Re: script no longer quitting all open apps under 10.15

Hello Shane.
Reading your post pushed me to give the content of my applet "tout quitter".

Applescript:

use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
property forTesting : true
-- true --> execute a subset of the script plus informative instructions
-- false --> execute the normal tasks

property cleanApplicationState : false
-- true --> empty the folder "Saved Application State:"
-- false --> doesn't empty the folder -- my current setting

if forTesting then set {everyApps, filteredApps} to {{}, {}}

set runningApplications to current application's NSWorkspace's sharedWorkspace()'s runningApplications()
set keepApps to {"Finder", "tout quitter"}
set regularActivationPolicy to current application's NSApplicationActivationPolicyRegular
repeat with anApplication in runningApplications
   set appName to anApplication's localizedName() as text
   if forTesting then set end of everyApps to appName
   if anApplication's activationPolicy() is regularActivationPolicy and appName is not in keepApps then
       if forTesting then
           set end of filteredApps to appName
       else
           anApplication's terminate()
       end if
   end if
end repeat
if forTesting then return {everyApps, linefeed, linefeed, filteredApps}

# Now the applications are cleanly closed
if cleanApplicationState then
   # We may delete the Saved Application State datas
   try
       set p2AS to (path to library folder from user domain as text) & "Saved Application State:"
       tell application "System Events" to tell folder p2AS
           delete every folder -- some items are folders
           delete every file -- some items are aliases
       end tell # System Events …
   end try
end if

set p2pane to ((path to system preferences as text) & "StartupDisk.prefPane") as «class furl»
# Get four localized strings
set cancelBtn to localized string "CANCEL" in bundle p2pane
set restartBtn to localized string "RESTART" in bundle p2pane
set shutdownBtn to localized string "SHUTDOWN" in bundle p2pane
set prompt to localized string "RESTART?" in bundle p2pane

tell application "SystemUIServer" to display dialog prompt buttons {cancelBtn, restartBtn, shutdownBtn} default button 3 cancel button 1
set |éteindre| to (button returned of result) is shutdownBtn
# Now we may trigger shutdown or restart cleanly. Open documents were saved before.
tell application "System Events"
   if |éteindre| then
       shut down
   else
       restart
   end if
end tell # System Events

In real life the property forTesting is set to false.
When I set it to true it return :

{{"loginwindow", "universalaccessd", "tout quitter", "talagent", "SystemUIServer", "Dock", "Finder", "Spotlight", "com.apple.dock.extra", "Centre de notifications", "Box Finder Extension", "imklaunchagent", "com.apple.PressAndHold", "CoreLocationAgent", "Wi-Fi", "Agent Photos", "FolderActionsDispatcher", "AirPlayUIAgent", "popCalendar", "Box", "TISwitcher", "SMARTReporter", "FastScripts", "fake_mojave", "TextEdit", "nbagent", "HP Device Monitor", "Box Helper", "ViewBridgeAuxiliary", "Legacy Color Picker Extensions (TextEdit)", "uBlock", "storeuid", "Box UI", "LaterAgent", "Xcode", "com.apple.CoreSimulator.CoreSimulatorService", "BBEdit", "Legacy Color Picker Extensions (BBEdit)", "Aperçu", "com.apple.speech.speechsynthesisd", "Mail", "Safari", "Safari Networking", "uBlock Safari Icon", "Contenu web Safari (préchauffé)", "LibreOffice", "CoreServicesUIAgent", "Éditeur de script", "com.apple.security.pboxd", "Événements système"}, "
", "
", {"fake_mojave", "TextEdit", "uBlock", "Xcode", "BBEdit", "Mail", "LibreOffice", "Éditeur de script"}}

In real life, after several months of use I disabled the cleaning of Saved Application State.
Given that, when I reStart the machine, "tout quitter.app" is executed and installed in the Dock.
It's why it's  listed in previous tests.
I assume that it's  because it has no provision to respond to a quit call that the previous tests brought an infinite running requiring to force quit the Script Editor.

I wish to add a comment about your script.
I feel that it requires some enhancement because, as is, it would fail to keep the Script Editor running.

Applescript:

use AppleScript version "2.5" -- macOS 10.11 or later
use framework "Foundation"
use framework "AppKit"
use scripting additions

set theApps to current application's NSWorkspace's sharedWorkspace()'s runningApplications()
set thePred to current application's NSPredicate's predicateWithFormat:"activationPolicy == 0 AND NOT (localizedName IN %@)" argumentArray:{{"Finder", "Script Editor"}} -- edit to suit
set theApps to theApps's filteredArrayUsingPredicate:thePred

repeat with anApp in theApps
   --anApp's terminate()
   log (localizedName() of anApp) as string -- EDITED according to Shane Stanley's comment
end repeat

return:
(*tout quitter*)
(*fake_mojave*)
(*TextEdit*)
(*uBlock*)
(*Xcode*)
(*BBEdit*)
(*Mail*)
(*Safari*)
(*LibreOffice*)
(*Éditeur de script*)

I had to edit it as :

Applescript:

use AppleScript version "2.5" -- macOS 10.11 or later
use framework "Foundation"
use framework "AppKit"
use scripting additions

property |NSURL| : a reference to current application's NSURL
property NSPredicate : a reference to current application's NSPredicate
property NSWorkspace : a reference to current application's NSWorkspace

set POSIXPath to POSIX path of (path to application "Script Editor")
(*
set theURL to current application's NSURL's fileURLWithPath:POSIXPath
--> error "Erreur dans Script Editor : NSURL ne comprend pas le message « fileURLWithPath_ »." number -1708 from NSURL
*)

set theURL to |NSURL|'s fileURLWithPath:POSIXPath
set {theResult, theValue, theError} to theURL's getResourceValue:(reference) forKey:"NSURLLocalizedNameKey" |error|:(reference)
set scriptEditor_loc to theValue as text
if scriptEditor_loc ends with ".app" then set scriptEditor_loc to text 1 thru -5 of scriptEditor_loc -- must drop the name extension
(*
set theApps to current application's NSWorkspace's sharedWorkspace()'s runningApplications()
--> error "Erreur dans Script Editor : NSWorkspace ne comprend pas le message « sharedWorkspace »." number -1708 from NSWorkspace
*)

set theApps to NSWorkspace's sharedWorkspace()'s runningApplications()
(*
set thePred to current application's NSPredicate's predicateWithFormat:"activationPolicy == 0 AND NOT (localizedName IN %@)" argumentArray:{{"Finder", scriptEditor_loc}} -- edit to suit
--> error "Erreur dans Script Editor : NSPredicate ne comprend pas le message « predicateWithFormat_argumentArray_ »." number -1708 from NSPredicate
*)

set thePred to NSPredicate's predicateWithFormat:"activationPolicy == 0 AND NOT (localizedName IN %@)" argumentArray:{{"Finder", "Script Debugger", scriptEditor_loc}} -- edit to suit
set theApps to theApps's filteredArrayUsingPredicate:thePred

repeat with anApp in theApps
   --anApp's terminate()
   log (localizedName() of anApp) as string -- EDITED according to Shane Stanley's comment
end repeat

I'm really puzzled by the need to define some properties.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) samedi 25 avril 2020 17:02:08

Added "Script Debugger" which is not localized in the list of 'protected' applications.

Last edited by Yvan Koenig (2020-04-26 04:17:42 am)

Offline

 

#14 2020-04-25 09:14:20 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4533

Re: script no longer quitting all open apps under 10.15

KniazidisR wrote:

I really liked the solution from Shane Stanley. It is truly more universal and faster than mine. I will just take it to my library without having to edit the name of the editor or applet itself:

Applescript:


use AppleScript version "2.5" -- macOS 10.11 or later
use framework "Foundation"
use framework "AppKit"
use scripting additions
property keepApps : {name of current application, "Finder"}

set theApps to current application's NSWorkspace's sharedWorkspace()'s runningApplications()
set thePred to current application's NSPredicate's predicateWithFormat:"activationPolicy == 0 AND NOT (localizedName IN %@)" argumentArray:{keepApps}
set theApps to theApps's filteredArrayUsingPredicate:thePred
repeat with anApp in theApps
   anApp's terminate()
end repeat



Are you sure of that ?

When I run this test version from Script Editor,

Applescript:


use AppleScript version "2.5" -- macOS 10.11 or later
use framework "Foundation"
use framework "AppKit"
use scripting additions
property keepApps : {name of current application, "Finder"}

set theApps to current application's NSWorkspace's sharedWorkspace()'s runningApplications()
set thePred to current application's NSPredicate's predicateWithFormat:"activationPolicy == 0 AND NOT (localizedName IN %@)" argumentArray:{keepApps}
set theApps to theApps's filteredArrayUsingPredicate:thePred
repeat with anApp in theApps
   --anApp's terminate()
   log (localizedName() of anApp) as string -- EDITED according to Shane Stanley's comment
end repeat

I get :
(*tout quitter*)
(*fake_mojave*)
(*TextEdit*)
(*uBlock*)
(*Xcode*)
(*BBEdit*)
(*Mail*)
(*Safari*)
(*LibreOffice*)
(*Éditeur de script*)

Once more you forgot that name of current application is not localized.
Here it returns "Script Editor"
while Shane's script lists localizedName of running applications.
As a result, Script Editor would be quitted.

I'm afraid that you will often miss this kind of behavior because you run your system in English.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) samedi 25 avril 2020 17:12:46

Last edited by Yvan Koenig (2020-04-26 04:16:52 am)

Offline

 

#15 2020-04-25 09:28:45 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 1259

Re: script no longer quitting all open apps under 10.15

Yvan Koenig wrote:


As a result, Script Editor would be quitted.


So, what to do? I would not like to correct the name of the editor or applet in the code each time. Give a solution for non-English systems, if possible.


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#16 2020-04-25 09:32:35 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4533

Re: script no longer quitting all open apps under 10.15

Just take time to read what I posted to Shane in message #13 were I wrote about this feature before seeing your post.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) samedi 25 avril 2020 17:32:28

Offline

 

#17 2020-04-25 10:05:34 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 1259

Re: script no longer quitting all open apps under 10.15

Well, Yvan, thanks for this. I tried your script in the post #13 and it worked for me fine. I just replaced the path to "Script Editor" with path to current application. May you test like me with path to current application and tell me if it works for your non English system?

I want to not hardcode the name of current application. Because I use multilpy choices for my scripts: Script Editor, Script Debugger, applet, droplet, mail rule, and so on... You understand me.

Last edited by KniazidisR (2020-04-25 10:12:51 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#18 2020-04-25 10:12:02 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4533

Re: script no longer quitting all open apps under 10.15

As I assume that your goal is to protect the editor with which you are running I make some enhancement.

Applescript:

use AppleScript version "2.5" -- macOS 10.11 or later
use framework "Foundation"
use framework "AppKit"
use scripting additions

property NSPredicate : a reference to current application's NSPredicate
property NSWorkspace : a reference to current application's NSWorkspace

set protectedName to name of current application
if protectedName is not "Script Debugger" then
   set theBundle to path to application "Script Editor"
   set protectedName to localized string "CFBundleName" from table "InfoPlist" in bundle theBundle
   -- set protectedName to localized string "CFBundleDisplayName" from table "InfoPlist" in bundle theBundle -- same result
end if
(*
set theApps to current application's NSWorkspace's sharedWorkspace()'s runningApplications()
--> error "Erreur dans Script Editor : NSWorkspace ne comprend pas le message « sharedWorkspace »." number -1708 from NSWorkspace
*)

set theApps to NSWorkspace's sharedWorkspace()'s runningApplications()
(*
set thePred to current application's NSPredicate's predicateWithFormat:"activationPolicy == 0 AND NOT (localizedName IN %@)" argumentArray:{{"Finder", scriptEditor_loc}} -- edit to suit
--> error "Erreur dans Script Editor : NSPredicate ne comprend pas le message « predicateWithFormat_argumentArray_ »." number -1708 from NSPredicate
*)

set thePred to NSPredicate's predicateWithFormat:"activationPolicy == 0 AND NOT (localizedName IN %@)" argumentArray:{{"Finder", protectedName}} -- edit to suit
set theApps to theApps's filteredArrayUsingPredicate:thePred

repeat with anApp in theApps
   --anApp's terminate()
   log (localizedName() of anApp) as string -- EDITED according to Shane Stanley's comment
end repeat

It's the running editor which will be protected.
At this time, the code in message #13 protect both of them.

As you may see, I use a cleaner code to grab the localized name of the Script Editor.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) samedi 25 avril 2020 18:11:52

Last edited by Yvan Koenig (2020-04-26 04:16:22 am)

Offline

 

#19 2020-04-25 10:13:42 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 1259

Re: script no longer quitting all open apps under 10.15

I want to not hardcode the name of current application at all. Because I use multilpy choices for my scripts: Script Editor, Script Debugger, applet, droplet, folder action, mail rule, and so on... You understand me.

I tested this:

Applescript:


use AppleScript version "2.5" -- macOS 10.11 or later
use framework "Foundation"
use framework "AppKit"
use scripting additions
property |NSURL| : a reference to current application's NSURL
property NSPredicate : a reference to current application's NSPredicate
property NSWorkspace : a reference to current application's NSWorkspace

set theURL to |NSURL|'s fileURLWithPath:(POSIX path of (path to current application))
set {theResult, theValue, theError} to theURL's getResourceValue:(reference) forKey:"NSURLLocalizedNameKey" |error|:(reference)
set currentApps_loc to theValue as text
if currentApps_loc ends with ".app" then set scriptEditor_loc to text 1 thru -5 of scriptEditor_loc

set theApps to NSWorkspace's sharedWorkspace()'s runningApplications()
set thePred to NSPredicate's predicateWithFormat:"activationPolicy == 0 AND NOT (localizedName IN %@)" argumentArray:{{currentApps_loc, "Finder"}}
set theApps to theApps's filteredArrayUsingPredicate:thePred

repeat with anApp in theApps
   anApp's terminate()
end repeat

Can you test is too, as is?

Last edited by KniazidisR (2020-04-25 10:40:09 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#20 2020-04-25 10:59:50 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4533

Re: script no longer quitting all open apps under 10.15

KniazidisR wrote:

Well, Yvan, thanks for this. I tried your script in the post #13 and it worked for me fine. I just replaced the path to "Script Editor" with path to current application. May you test like me with path to current application and tell me if it works for your non English system?

I want to not hardcode the name of current application. Because I use multilpy choices for my scripts: Script Editor, Script Debugger, applet, droplet, mail rule, and so on... You understand me.



I tested with both editors and applet running :

Applescript:

use AppleScript version "2.5" -- macOS 10.11 or later
use framework "Foundation"
use framework "AppKit"
use scripting additions

property |NSURL| : a reference to current application's NSURL

set POSIXPath to POSIX path of (path to current application)
(*
set theURL to current application's NSURL's fileURLWithPath:POSIXPath
--> error "Erreur dans Script Editor : NSURL ne comprend pas le message « fileURLWithPath_ »." number -1708 from NSURL
*)

set theURL to |NSURL|'s fileURLWithPath:POSIXPath
set {theResult, theValue, theError} to theURL's getResourceValue:(reference) forKey:"NSURLLocalizedNameKey" |error|:(reference)
set protectedName to theValue as text
-- stripping aways required here
if protectedName ends with ".app" then set protectedName to text 1 thru -5 of protectedName -- must drop the name extension

display dialog protectedName

It returned the correct values:
Script Debugger (which is never localized)
Éditeur de script on my machine running in French
fakeApplet when running an applet by a double click (this one was not localized)
I didn't try with a mail rule but (1) I don't see why it would fail (2) at least in French the name of Mail.app is not localized so I would see nothing if it didn't work.

I guess that the best thing to do is to set, at least temporarily, your machine to run in Greek and make some tests on your side.

I was glad to have found a cleaner way to grab the localized name for Script Editor but, it doesn't apply to your 3rd, 4th… cases
I hope that Shane will be able to give a cleaner code.
I searched in wCode help for infos about localizedName but found nothing allowing me to clean the code.
As you saw, I was a bit puzzled by the fact that the syntax used by Shane fails on my machine.
What a surprise, after a simple copy then paste in a blank window, the original instructions worked flawlessly so the current code is :

Applescript:

use AppleScript version "2.5" -- macOS 10.11 or later
use framework "Foundation"
use framework "AppKit"
use scripting additions

set POSIXPath to POSIX path of (path to current application)

set theURL to current application's NSURL's fileURLWithPath:POSIXPath
set {theResult, theValue, theError} to theURL's getResourceValue:(reference) forKey:"NSURLLocalizedNameKey" |error|:(reference)
set protectedName to theValue as text
-- always required here
if protectedName ends with ".app" then set protectedName to text 1 thru -5 of protectedName -- must drop the name extension

set theApps to current application's NSWorkspace's sharedWorkspace()'s runningApplications()
set thePred to current application's NSPredicate's predicateWithFormat:"activationPolicy == 0 AND NOT (localizedName IN %@)" argumentArray:{{"Finder", protectedName}} -- edit to suit
set theApps to theApps's filteredArrayUsingPredicate:thePred

repeat with anApp in theApps
   --anApp's terminate()
   log (localizedName() of anApp) as string -- EDITED according to Shane Stanley's comment
end repeat

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) samedi 25 avril 2020 18:59:43

Last edited by Yvan Koenig (2020-04-26 04:15:35 am)

Offline

 

#21 2020-04-25 12:03:57 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 1259

Re: script no longer quitting all open apps under 10.15

Thank you, Yvan, for testing and your amendment. I see, it works as expected.


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#22 2020-04-25 12:47:55 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4533

Re: script no longer quitting all open apps under 10.15

Thank's.

At last, I found the clean way to get the localizedName of the current application.

Applescript:

use AppleScript version "2.5" -- macOS 10.11 or later
use framework "Foundation"
use framework "AppKit"
use scripting additions

set curName to current application's NSRunningApplication's currentApplication()'s localizedName() as string

set theApps to current application's NSWorkspace's sharedWorkspace()'s runningApplications()
set thePred to current application's NSPredicate's predicateWithFormat:"activationPolicy == 0 AND NOT (localizedName IN %@)" argumentArray:{{"Finder", curName}} -- edit to suit
set theApps to theApps's filteredArrayUsingPredicate:thePred

repeat with anApp in theApps
   --anApp's terminate()
   log (localizedName() of anApp) as string -- EDITED according to Shane Stanley's comment
end repeat

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) samedi 25 avril 2020 20:47:09

Last edited by Yvan Koenig (2020-04-26 04:14:48 am)

Offline

 

#23 2020-04-25 05:34:18 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6395

Re: script no longer quitting all open apps under 10.15

Yvan Koenig wrote:

as is, it would fail to keep the Script Editor running.



It certainly should keep the current application running -- but that may not be Script Editor. And I suppose the name of Finder could be localized in some locales. So:

Applescript:

use AppleScript version "2.5" -- macOS 10.11 or later
use framework "Foundation"
use framework "AppKit"
use scripting additions

set theFinder to (current application's NSRunningApplication's runningApplicationsWithBundleIdentifier:"com.apple.finder")'s firstObject()
set currApp to current application's NSRunningApplication's currentApplication()
set theApps to current application's NSWorkspace's sharedWorkspace()'s runningApplications()
set thePred to current application's NSPredicate's predicateWithFormat:"activationPolicy == 0 AND NOT (SELF IN %@)" argumentArray:{{theFinder, currApp}}
set theApps to theApps's filteredArrayUsingPredicate:thePred
repeat with anApp in theApps
   anApp's terminate()
end repeat

I'm really puzzled by the need to define some properties.



It's not a need -- more a convenience, in terms of avoiding repeated "current application's".


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#24 2020-04-25 05:41:32 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6395

Re: script no longer quitting all open apps under 10.15

Yvan Koenig wrote:

At last, I found the clean way to get the localizedName of the current application.



It's generally safer to treat ASObjC properties as methods, so better to use localizedName(), with the parentheses.It doesn't matter in the code you posted, but it can be a trap, so it's a good habit.

Curiously, there are still some processes where System Events returns something different. AdobeAcrobat vs Acrobat Pro DC is one example. It looks like System Events uses the name of the executable in the app's bundle.

Last edited by Shane Stanley (2020-04-25 05:42:16 pm)


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#25 2020-04-26 04:08:55 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4533

Re: script no longer quitting all open apps under 10.15

Shane Stanley wrote:
Yvan Koenig wrote:

as is, it would fail to keep the Script Editor running.



It certainly should keep the current application running -- but that may not be Script Editor. And I suppose the name of Finder could be localized in some locales. So:

Applescript:

use AppleScript version "2.5" -- macOS 10.11 or later
use framework "Foundation"
use framework "AppKit"
use scripting additions

set theFinder to (current application's NSRunningApplication's runningApplicationsWithBundleIdentifier:"com.apple.finder")'s firstObject()
set currApp to current application's NSRunningApplication's currentApplication()
set theApps to current application's NSWorkspace's sharedWorkspace()'s runningApplications()
set thePred to current application's NSPredicate's predicateWithFormat:"activationPolicy == 0 AND NOT (SELF IN %@)" argumentArray:{{theFinder, currApp}}
set theApps to theApps's filteredArrayUsingPredicate:thePred
repeat with anApp in theApps
   anApp's terminate()
end repeat


It's KniazidisR which asked for a code protecting the current application.
So I posted a code responding to its requirements.

For the Finder, you are right.
I missed that here is one language for which Finder is not spelled "Finder" but "访达"

Applescript:

----------------------------------------------------------------
use AppleScript version "2.5"
use framework "Foundation"
use scripting additions

-- Build an array of the localized names of the application Finder
-- Some languages are defined twice because they aren't named the same according to the running OS
-- Yvan KOENIG (VALLAURIS, France) dimanche 29 mars 2020 17:42:48
----------------------------------------------------------------

property |⌘| : a reference to current application

set theLanguages to paragraphs of "ar
ca
cs
da
de
el
en_AU
en_GB
en
es_419
es
fi
fr_CA
fr
he
hi
hr
hu
id
it
ja
ko
ms
nl
no
pl
pt_PT
pt
ro
ru
sk
sv
th
tr
uk
vi
zh_CN
zh_HK
zh_TW
Dutch
English
French
German
Italian
Japanese
Spanish"


set root to (path to application "Finder" as text) & "Contents:Resources:"
--set root to "resources YK:resources trois:Catalina:System:Library:CoreServices:Finder.app:" & "Contents:Resources:"
set fullTable to "InfoPlist.strings"

set allLocales to {"language" & tab & "CFBundleDisplayName" & tab & "CFBundleName"}
-- Now extract the values for every language available
repeat with usedLanguage in theLanguages
   set anURL to (|⌘|'s NSURL's fileURLWithPath:(POSIX path of (root & usedLanguage & ".lproj:" & fullTable)))
   set theDict to (|⌘|'s NSDictionary's dictionaryWithContentsOfURL:anURL)
   if theDict is not missing value then
       set val1 to (theDict's valueForKey:"CFBundleDisplayName")
       set val2 to (theDict's valueForKey:"CFBundleName")
   else
       set {val1, val2} to {"??", "??"}
   end if
   set end of allLocales to (usedLanguage as string) & tab & val1 & tab & val2
end repeat

set theData to my concatlist:allLocales usingString:linefeed
-- save data to new file
set reportName to "Finder local name.txt"
set hfsPath to (path to desktop as text) & reportName
set targetFile to hfsPath as «class furl»
(theData's writeToURL:targetFile atomically:true encoding:(|⌘|'s NSUTF8StringEncoding) |error|:(missing value))

tell application "TextEdit"
   activate
   if exists window reportName then close window reportName
   open targetFile
end tell

#=====

on concatlist:theList usingString:d1
   set anArray to current application's NSArray's arrayWithArray:theList
   return (anArray's componentsJoinedByString:d1) -- as text
end concatlist:usingString:

#=====

Here without my special path I get:
language    CFBundleDisplayName    CFBundleName
ar    Finder    Finder
ca    Finder    Finder
cs    Finder    Finder
da    Finder    Finder
de    ??    ??
el    Finder    Finder
en_AU    ??    ??
en_GB    ??    ??
en    ??    ??
es_419    Finder    Finder
es    ??    ??
fi    Finder    Finder
fr_CA    ??    ??
fr    ??    ??
he    Finder    Finder
hi    Finder    Finder
hr    Finder    Finder
hu    Finder    Finder
id    Finder    Finder
it    ??    ??
ja    ??    ??
ko    Finder    Finder
ms    Finder    Finder
nl    ??    ??
no    Finder    Finder
pl    Finder    Finder
pt_PT    Finder    Finder
pt    Finder    Finder
ro    Finder    Finder
ru    Finder    Finder
sk    Finder    Finder
sv    Finder    Finder
th    Finder    Finder
tr    Finder    Finder
uk    Finder    Finder
vi    Finder    Finder
zh_CN    访达    访达
zh_HK    ??    ??
zh_TW    Finder    Finder
Dutch    Finder    Finder
English    Finder    Finder
French    Finder    Finder
German    Finder    Finder
Italian    Finder    Finder
Japanese    Finder    Finder
Spanish    Finder    Finder

because several lprojs are missing (or named differently) in High Sierra.
In Mojave or Catalina, the double question marks would be replaced by "Finder" and the late seven descriptors would be ??    ??.

I'm really puzzled by the need to define some properties.



It's not a need -- more a convenience, in terms of avoiding repeated "current application's".



No, in message #13, you may see that I was forced to use the properties and this is why I was puzzled.
I don't understand why it behaved this way because, as I wrote in an other message, after a copy/paste process it was no longer required.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 26 avril 2020 12:07:18

Last edited by Yvan Koenig (2020-04-26 07:58:44 am)

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)