Activate application showing all windows

What’s the best way to activate an application showing all its windows?

Al my attempts end in failure including the following:

(current application's NSRunningApplication's runningApplicationsWithBundleIdentifier:"com.apple.finder")'s firstObject()'s activateWithOptions:1
1 Like

@ccstone, you liked the post: does it means it’s working on your system? (If I remember well, you’re on Mojave?)

Any one else has a clue for Monterey?

Hi @ionah. With the appropriate framework declarations, your script works in Mojave’s Script Editor, but not in either the Script Menu or Script Debugger. It does work in them all if I use option 2 (current application’s NSApplicationActivateIgnoringOtherApps) instead.

@NigelGarvey

Thanks for testing.
When you say it’s working with option 1 from SE, does it means it shows all Finder windows or just activates the Finder? Because here in Monterey (even from SE) it’s only the front window that is showing…

Hi @ionah. Testing with Mojave’s SE, all of the Finder’s open Finder and information windows come to the front of whatever else is on the screen, even if the Finder starts out as the rearmost visible app. With Script Debugger and the Script Menu, neither the Finder nor any of its windows comes to the front.

So we can say it’s a macOS related bug, I guess?
Or at list a broken feature…

In the meantime, I think I’ve found a workaround:

tell application "Finder"
	activate
	set visible to true
	activate
end tell

Or using System Events if the targeted app can’t be scripted:

tell application "Finder" to activate
tell application "System Events" to set visible of application process "Finder" to true
tell application "Finder" to activate

Simply telling the Finder to activate is enough here — even in SD if “Bring Script Debugger to foreground when scripts end” is disabled in SD’s “Execution” preferences.

The point is: does it work for any one running Monterey 12.6.2?

  • macOS 10.14.6 Mojave

My experience mirrors Nigel’s…

The code works perfectly in Apple’s Script Editor, but it does not work properly in Script Debugger 5, Script Debugger 8, FastScripts, or Keyboard Maestro.

Edit – 2023/01/06 01:43 CST

I just tried with CodeRunner 4, and it too failed – as expected, since like Keyboard Maestro it’s using osascript to run AppleScript.

Thanks Chris for reporting.
Here it does not work even in SE.

1 Like

I tested Jonas’ script with 3 Finder windows open, and it raised all 3 windows.

tell application "Finder"
	activate
	set visible to true
	activate
end tell

However, I then tried this same script with Safari and received the error quoted below. The reason for the difference may be that the Finder application has a visible property but the Safari application does not (it does have a window visible property, though).

Safari got an error: Can’t set visible to true.

I also tested Jonas’ second script, and this worked with both Finder and Safari. The System Events Processes Suite does have a visible property, so this makes sense.

tell application "Safari" to activate
tell application "System Events" to set visible of application process "Safari" to true
tell application "Safari" to activate

FWIW, another alternative is GUI scripting with the “Bring All to Front” menu command, but the System Events script seems a better choice.

BTW, running the Finder activate command alone raises only 1 of 3 Finder windows on my computer. I’m running the current version of Monterey. All testing done with a saved script run by way of FastScripts 3.

@peavine,
Could you please make the same test* with the AppleScriptObjC script version and tell us if it shows all windows?

use framework "Foundation"
use framework "AppKit"
use scripting additions

(current application's NSRunningApplication's runningApplicationsWithBundleIdentifier:"com.apple.finder")'s firstObject()'s activateWithOptions:1

*(run the script from Script Debugger 8 and from FastScripts 3)

Jonas. With 3 Finder windows open, I ran your script in Script Debugger 8. It returned false but did nothing else. I then ran the script a second time and one Finder window was brought to the front.

Same scenario but with Script Editor, one Finder window was brought to the front.

Same scenarioo but with FastScripts, nothing happened.

I tried com.apple.Safari and got the same results.

Ok, thank you Peavine for this complete test.
You get the exact same results as me in the same situations.
Bug is confirmed.

Jonas. It’s unfortunate that code doesn’t work as expected. It would have been useful, although the System Events solution does work well.

I played around with this a bit more, and the following run in Script Editor does bring to the front all Script Editor windows. However, it doesn’t work when run by way of FastScripts or if saved as an app (with background-only option enabled), so it’s of no actual use. BTW, I used the actual option rather than “1” just for clarity–they both work.

use framework "AppKit"
use framework "Foundation"

set activeAppID to current application's NSWorkspace's sharedWorkspace()'s frontmostApplication()'s processIdentifier()
(current application's NSRunningApplication's runningApplicationWithProcessIdentifier:activeAppID)'s activateWithOptions:(current application's NSApplicationActivateAllWindows)