用于通过Apple的Automator切换回以前运行的窗口的代码是什么?

NLe*_*Led 2 applescript automator

我创建了一个在Automator中切换键盘查看器的代码.

on run {input, parameters}
    if application "KeyboardViewer" is running then
        quit application "KeyboardViewer"
    else
        activate application "KeyboardViewer"
    end if
    return input
end run
Run Code Online (Sandbox Code Playgroud)

但是,键盘查看器成为当前运行的窗口,我无法立即开始键入(我必须切换回上一个窗口).是否有我可以添加的特定代码,以便再次突出显示上一个窗口?

Lri*_*Lri 6

您可以使用launch而不是activate:

tell application "KeyboardViewer"
    if running then
        quit
    else
        launch
    end if
end tell
Run Code Online (Sandbox Code Playgroud)

如果应用程序未打开,launch通常会在其他应用程序上方打开一个新窗口,但在最前面的应用程序下方.否则它只是将应用程序保留在后台.您可以使用AXRaise在第二种情况下引发窗口,但它也使它们看起来像活动窗口.

launch application "Terminal"
tell application "System Events" to tell process "Terminal"
    perform action "AXRaise" of windows
end tell
Run Code Online (Sandbox Code Playgroud)

您还可以将以前的应用程序保存在变量中:

set a to path to frontmost application as text
activate application "Terminal"
activate application a
Run Code Online (Sandbox Code Playgroud)

如果您将焦点转移到后台应用程序,则可以稍后激活最前面的应用程序:

try
    tell application "SystemUIServer"
        display dialog "" default answer ""
    end tell
end try
activate application (path to frontmost application as text)
Run Code Online (Sandbox Code Playgroud)