您可以使用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)