Scripting 我可以使用Applescript控制程序而不将该程序置于前台吗?

Scripting 我可以使用Applescript控制程序而不将该程序置于前台吗?,scripting,background,popup,window,applescript,Scripting,Background,Popup,Window,Applescript,我正在使用Applescript与提供一个键盘快捷键,用于在中启动和停止计时器。这对我跟踪我的自由职业者工作非常有用 以下是我使用的代码: tell application "Billings" to activate tell application "System Events" tell process "Billings" tell menu bar 1 tell menu bar item "Slips"

我正在使用Applescript与提供一个键盘快捷键,用于在中启动和停止计时器。这对我跟踪我的自由职业者工作非常有用

以下是我使用的代码:

tell application "Billings" to activate
tell application "System Events"
    tell process "Billings"
        tell menu bar 1
            tell menu bar item "Slips"
                tell menu "Slips"
                    if menu item "Start Timer" exists then
                        click menu item "Start Timer"
                    else
                        click menu item "Stop Timer"
                    end if
                end tell
            end tell
        end tell
        keystroke "h" using {command down}
    end tell
end tell
我把它弹出到一个Alfred扩展集中,用CTRL+ALT+CMD+T激活

不幸的是,它有点笨重。昨天之前我从未碰过Applescript,所以我担心我的排骨有点不好。Billings窗口瞬间弹出到前景中,然后再次隐藏。如果键盘上按了除H以外的任何修改键,它也不会隐藏

因此:

  • 有没有更好的方法调用Billings,这样它就不会弹出
  • 如果没有,是否有更好的方法隐藏它并返回到具有焦点的前一个应用程序
为清晰起见进行编辑:
Billings是OSX自由职业者时间跟踪和开票软件包。我使用计时器来跟踪我在每项任务上花费的时间,这样我就可以准确地向我的客户开账单。我个人觉得它非常有用。计时器(我正在处理的当前项目)显示在菜单栏中,可以通过单击鼠标进行切换。理想情况下,当Billings不在前台时,我可以用键盘快捷键轻松启动和停止它,但没有内置的功能。看起来是这样的:

这里是另一种方法:

tell application "System Events" to set xxx to (1st process whose frontmost is true)
activate application "Billings"
tell application "System Events"
    tell process "Billings"
        click menu item 7 of menu 1 of menu bar item 7 of menu bar 1
        if visible then
            set visible to false
        end if
    end tell
end tell
set frontmost of xxx to true

下面是一个将应用程序保留在后台的解决方案

-- no activate
tell application "System Events"
    tell group 1 of group 2 of window "Billings" of process "Billings"
        perform action "AXPress" of (get first button whose its name ends with " Timer")
    end tell
end tell
--

如果它不起作用,下面是一个不使用快捷方式而隐藏应用程序的解决方案

activate application "Billings"
tell application "System Events"
    tell process "Billings"
        tell menu "Slips" of menu bar item "Slips" of menu bar 1
            click (get first menu item whose its name ends with " Timer")
        end tell
        set visible to false
    end tell
end tell

不完全是。这将创建一个新的计时器,它已经有一个内置的热键。我想启动和停止当前正在运行的计时器。我将用更多关于我想做什么的上下文信息更新原始问题。