Focusing App with activateWithOptions - only works if non-miniaturized

血红的双手。 提交于 2019-12-25 14:26:06

问题


I am trying to focus all windows of another process, I know the PID of it.

When I run the following code:

app = [NSRuningApplication runningApplicationWithProcessIdentifier: 400];
[app activateWithOptions: (NSApplicationActivateAllWindows | NSApplicationActivateIgnoringOtherApps)]

All windows that are NOT minimized/miniaturized are shown. HOWEVER if all of the windows are minimized/miniaturized none of the windows are shown.

How can I un-minimize all windows on activate? I also tried SetFrontProcess but that had the exact same issue.

Here is a screen-cast I made of the issue: https://www.youtube.com/watch?v=sR5uf4eR8js

Edit - whacky not so much a solution

I found one whacky semi-solution, which was to run killall Dock, this un-minimizes all minimized windows however. I just need to un-minimize windows of a specific PID.


回答1:


First, just to be clear, -activateWithOptions: is activating the other instance of Firefox. You can tell by, for example, looking at its Window menu. The only issue is that it hasn't unminimized any windows. That's not necessarily incorrect behavior. For example, if you use Command-Tab to switch to the other Firefox, instead of clicking on its Dock icon, it would become active but would not unminimize its window.

So, you should consider maybe just leaving it how it is.

If you really want to change it, though, try doing the following instead of -activateWithOptions::

[[NSWorkspace sharedWorkspace] launchApplicationAtURL:app.bundleURL
                                              options:NSWorkspaceLaunchAsync | NSWorkspaceLaunchWithoutAddingToRecents
                                        configuration:@{}
                                                error:NULL];

That's approximately equivalent to clicking on the app's Dock icon. Or opening the app from the Finder. Since it's already running, the existing instance will be activated and sent the reopen application (kAEReopenApplication a.k.a. 'rapp') Apple event. The standard behavior will unminimize a window if all of its windows are minimized. (If it has no open windows, it will typically create a new, default window. In a document editor, this would be a new, untitled document. In Firefox, this would be the home page.)

This will not unminimize all windows, but that's generally not desired, anyway.



来源:https://stackoverflow.com/questions/34721737/focusing-app-with-activatewithoptions-only-works-if-non-miniaturized

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!