我创建了一个用于在Automator中切换键盘查看器的代码。
on run {input, parameters}
if application "KeyboardViewer" is running then
quit application "KeyboardViewer"
else
activate application "KeyboardViewer"
end if
return input
end run
但是,键盘查看器成为当前运行的窗口,我无法立即开始键入(我必须切换回上一个窗口)。是否有我可以添加的特定代码,以便再次突出显示上一个窗口?
答案 0 :(得分:6)
您可以使用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)
答案 1 :(得分:1)
在激活应用程序" KeyboardViewer"之后尝试这个。线...
tell application "System Events" to keystroke tab using command down
编辑 :由于上面的原帖没有为你做,所以试试这个。它使用我用来获取最前面运行的应用程序的子例程,就像这样。只需将此代码放入自动器操作的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