Applescript 通过Apple';切换回以前运行的窗口的代码是什么;自动售货机?

Applescript 通过Apple';切换回以前运行的窗口的代码是什么;自动售货机?,applescript,automator,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 但是,键盘查看器成

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

on run {input, parameters}
    if application "KeyboardViewer" is running then
        quit application "KeyboardViewer"
    else
        activate application "KeyboardViewer"
    end if
    return input
end run

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

请在激活应用程序“KeyboardViewer”行后尝试此操作

编辑:既然上面的原始帖子不适合你,那么试试这个。在这种情况下,它使用一个子例程来获取最前面运行的应用程序。只需将此代码放入自动操作程序操作的applescript部分

on run {input, parameters}
    if application "KeyboardViewer" is running then
        quit application "KeyboardViewer"
    else
        set frontAppPath to my getFrontAppPath()
        activate application "KeyboardViewer"
        delay 0.2
        tell application frontAppPath to activate
    end if
    return input
end run

on getFrontAppPath()
    set frontAppPath to (path to frontmost application) as text
    set myPath to (path to me) as text

    if frontAppPath is myPath then
        try
            tell application "Finder" to set bundleID to id of file myPath
            tell application "System Events" to set visible of (first process whose bundle identifier is bundleID) to false

            -- we need to delay because it takes time for the process to hide
            -- I noticed this when running the code as an application from the applescript menu bar item
            set inTime to current date
            repeat
                set frontAppPath to (path to frontmost application) as text
                if frontAppPath is not myPath then exit repeat
                if (current date) - inTime is greater than 1 then exit repeat
            end repeat
        end try
    end if
    return frontAppPath
end getFrontAppPath

您可以使用
launch
而不是
activate

tell application "KeyboardViewer"
    if running then
        quit
    else
        launch
    end if
end tell
如果应用程序未打开,
launch
通常会在其他应用程序上方但最前面的应用程序下方打开一个新窗口。否则,它只是将应用程序保留在后台。在第二种情况下,您可以使用
AXRaise
来提升窗口,但这也会使它们看起来像活动窗口

launch application "Terminal"
tell application "System Events" to tell process "Terminal"
    perform action "AXRaise" of windows
end tell
您还可以将以前的应用程序保存在变量中:

set a to path to frontmost application as text
activate application "Terminal"
activate application a
如果要将焦点移动到后台应用程序,您可以稍后激活最前端的应用程序:

try
    tell application "SystemUIServer"
        display dialog "" default answer ""
    end tell
end try
activate application (path to frontmost application as text)

这是可行的,但键盘现在隐藏在正在运行的应用程序窗口后面。我在我的帖子中添加了一个“编辑”部分,应该对你有用。祝你好运。你必须在可访问性首选项中启用辅助设备的访问。KeyboardViewer不需要AXRaise,但我试图让答案变得通用。
try
    tell application "SystemUIServer"
        display dialog "" default answer ""
    end tell
end try
activate application (path to frontmost application as text)