【发布时间】:2010-09-07 03:15:00
【问题描述】:
我打开了2个“Finder”窗口A&B,A在前面,B在下面,下面的sn-p把B带到最前面:
tell application "Finder"
activate
activate window 2
end tell
但对于不支持脚本的应用程序,刚才提到的代码将无济于事。
关于激活非脚本应用程序窗口的任何想法。
【问题讨论】:
标签: applescript
我打开了2个“Finder”窗口A&B,A在前面,B在下面,下面的sn-p把B带到最前面:
tell application "Finder"
activate
activate window 2
end tell
但对于不支持脚本的应用程序,刚才提到的代码将无济于事。
关于激活非脚本应用程序窗口的任何想法。
【问题讨论】:
标签: applescript
在这些情况下,您通常可以求助于系统事件。系统事件知道正在运行的进程的窗口,您通常可以操纵这些窗口。这样的事情会告诉你一些你可以做的事情。随便玩弄代码,看看你能不能做你想做的事。
tell application "System Events"
tell process "Whatever"
properties of windows
end tell
end tell
编辑: 窗口的属性之一是它的“标题”。所以你也许可以使用它。这种方法使用的事实是,许多应用程序都有一个“窗口”菜单,并且在该菜单下多次列出窗口的名称,您可以通过单击适当的菜单项来切换窗口。所以这样的事情可能会起作用......我的示例使用 TextEdit。
tell application "TextEdit" to activate
tell application "System Events"
tell process "TextEdit"
set windowTitle to title of window 2
click menu item windowTitle of menu 1 of menu bar item "Window" of menu bar 1
end tell
end tell
【讨论】:
您对不可编写脚本的定义是什么?几乎所有内容在某种程度上都是可编写脚本的,但为了举例说明,我们可以使用,不包含 AppleScript dictionary,例如AppName.sdef 在其应用程序包中。
例如,macOS 包含的 Stickies 应用程序 不包含 Stickies.sdef 文件,当尝试将其添加到 Script Editor 中的 Library 时,会显示“无法添加该项目,因为它不可编写脚本。”
在这种情况下,则需要系统事件与应用程序进程对话,例如:
示例 AppleScript 代码:
if running of application "Stickies" then
tell application "System Events"
tell application process "Stickies"
set frontmost to true
if exists window 2 then ¬
perform action "AXRaise" of window 2
end tell
end tell
end if
注意事项:
我在 示例 AppleScript 代码 中包含了错误处理,如果您愿意,可以将其删除.
【讨论】: