使用AppleScript和键盘快捷键

时间:2017-02-17 07:24:09

标签: applescript cycle launch automator

如何使用AppleScript创建键盘快捷键:

  • 如果应用程序未运行,请启动它。
  • 如果应用程序正在运行,请将其置于前台。
  • 如果我一直按下快捷键,请仅浏览 应用程序的 窗口。

例如,当我按下Option-Y时,它将启动终端或将其带到前台,如果我一直按下Option-Y,它将循环通过多个打开的终端窗口,如果我有多个打开的话。 / p>

我已使用Automator作为服务成功创建了AppleScript。我已成功将此脚本映射到键盘快捷方式,方法是转到系统偏好设置,键盘,快捷方式,服务和常规,检查并为Automator服务BringTerminalToTheForeground指定键盘快捷方式。

以下是启动终端的脚本:

tell application "Terminal"
    activate
end tell

我想只使用默认配备Mac的工具(例如AppleScript,Automator)。我正在驾驶El Capitan。

1 个答案:

答案 0 :(得分:0)

Command-“`”将在应用程序的窗口中循环。

tell application "Terminal" -- try other applications. This script might work in other cases. It happens to work with Terminal but I haven't tested it.
    activate
    set c to count of windows -- we need to know how many windows the application has so we only cycle through all of them once.
    tell application "System Events"
        repeat c times
            keystroke "`" using command down --  this is what cycles through the windows. This doesn't work in every application.
            delay 1 -- this is a 1-second delay. Change this to whatever delay you want.
        end repeat
    end tell
end tell