【问题标题】:set application to frontmost in applescript在applescript中将应用程序设置为最前面
【发布时间】:2017-05-17 06:47:39
【问题描述】:

我试图找出最前面的应用程序 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

[代码在这里]

tell application "System Events"
set frontmost of process whose name is apptemp to true
end tell

虽然这段代码没有返回任何错误,但它不起作用。我也试过这段代码

tell application "System Events"
set apptemp to application "Google Chrome"
set frontmost of process "Google Chrome" to true
end tell

但同样,虽然没有错误,但它不会起作用。

另外,请有人告诉管理员,以便更轻松地显示代码。我在这个网站上显示代码最困难。每行代码都要缩进四个空格,太疯狂了。

【问题讨论】:

  • 关于帮助中的代码缩进:您也可以选择文本并按 CTRL+K 切换缩进为代码
  • 他们需要在网页上更清楚地说明这一点。

标签: applescript


【解决方案1】:

比其他两种方法更好的是使用作为唯一键的包标识符。

tell application "System Events"
    set frontAppID to bundle identifier of first process whose frontmost is true
end tell

-- Sample code... can be anything else
activate application "Finder"
delay 3
-- End of sample code

activate application id frontAppID

【讨论】:

  • 您可以缩短为id of application ((path to frontmost application) as text),但即便如此,我也不认为这比仅使用路径更好,这也是独一无二的。您提到了两种方法——另一种是什么?
  • 如何在 if 子句中使用捆绑标识符?像“如果最前面是“com.apple.safari”的第一个进程的包标识符的frontAppID那么......”这样的东西不起作用。
  • 如果(最前面为真的第一个进程的捆绑标识符是“com.apple.safari”)然后...
【解决方案2】:

处理此问题的一种可靠方法是使用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

使用应用程序的特定路径可确保重新激活完全相同的应用程序,而不管进程名称不同的重复应用程序和应用程序。

注意:很想直接保存和恢复对象引用frontmost application,但这实际上不起作用:莫名其妙地,这样一个保存的引用被视为与 当前应用程序(运行代码的那个)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多