如何在 OSX 中激活外部应用程序的特定窗口?
我正在尝试从我的应用程序控制 Safari 窗口。
我使用此代码通过 ScriptingBridge 从 Safari 获取窗口列表:
SafariApplication *saf = [SBApplication ApplicationWithProcessIdentifier:myPid];
SBElementArray *safariWindows = [saf windows];
到目前为止效果很好。 我还可以使用索引属性来管理窗口的顺序,如下所示:
SafariWindow *firstSafWin = [saf.windows objectAtIndex:0];
[firstSafWin setIndex: 1];
Now I try to activate Safari with:NSRunningApplication *runApp = [NSRunningApplication runningApplicationWithProcessIdentifier: myPid];
[runApp activateWithOptions: NSApplicationActivateAllWindows];
我的问题来了:
Safari 被激活,我想要的窗口现在位于顶部,但它没有获得焦点(或“mainWindow”)。因此,用户输入的焦点仍然在 Safari 窗口上,在我尝试更改 Safari 窗口的顺序之前,该窗口具有焦点。
OSX 中有没有办法将焦点转移到我想要的 Safari 窗口?
I'm trying to control Safari windows from my application.
I use this code to get the window list from Safari via ScriptingBridge:
SafariApplication *saf = [SBApplication ApplicationWithProcessIdentifier:myPid];
SBElementArray *safariWindows = [saf windows];
This works fine so far.
I also can manage the order of the windows with the index property something like this:
SafariWindow *firstSafWin = [saf.windows objectAtIndex:0];
[firstSafWin setIndex: 1];
Now I try to activate Safari with:
NSRunningApplication *runApp = [NSRunningApplication runningApplicationWithProcessIdentifier: myPid];
[runApp activateWithOptions: NSApplicationActivateAllWindows];
So here comes my problem:
Safari gets activated and the window I wanted is on top now, but it doesn't get the focus (or "mainWindow"). So the focus of the user input is still on the safari window which had the focus before I tried to change the order of the Safari windows.
Is there a way in OSX to get the focus to the Safari window I want?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Accessibility API 可以为您提供这种级别的控制,但必须在用户的计算机上启用它。
The Accessibility API can give you this level of control but it must be enabled on the user's machine.
FWIW,你正在做的事情应该有效。您应该针对 Safari 提交错误报告。
FWIW, what you are doing should work. You should file a bug report against Safari.