我试图找出最前面的应用程序 x 是什么,运行一个脚本,然后使用 applescript 将 x 设置为最前面的应用程序。
tell application "System Events"
set frontApp to displayed name of first process whose frontmost is true
set apptemp to frontApp
end tell
Run Code Online (Sandbox Code Playgroud)
[代码在这里]
tell application "System Events"
set frontmost of process whose name is apptemp to true
end tell
Run Code Online (Sandbox Code Playgroud)
尽管此代码不会返回任何错误,但它不起作用。我也尝试过这段代码
tell application "System Events"
set apptemp to application "Google Chrome"
set frontmost of process "Google Chrome" to true
end tell
Run Code Online (Sandbox Code Playgroud)
但同样,虽然没有错误,但它不会工作。
另外,请有人告诉管理员,以便更容易显示代码。我在这个网站上显示代码最困难。我必须为每行代码缩进四个空格,这太疯狂了。
处理这个问题的一个可靠方法是使用path to frontmost application如下:
# Save which application is frontmost (active),
# as an absolute, HFS-style path string pointing to the application bundle.
set frontmostAppPath to (path to frontmost application) as text
# Activate and work with another application.
activate application "Reminders"
delay 2
# Make the previously frontmost (active) application frontmost again.
activate application frontmostAppPath
Run Code Online (Sandbox Code Playgroud)
使用应用程序的特定路径可确保重新激活完全相同的应用程序,而不管重复的应用程序和进程名称不同的应用程序。
注意:很想frontmost application 直接保存和恢复对象引用,但这实际上不起作用:令人费解的是,这样保存的引用被视为与当前应用程序(运行代码的应用程序)相同。