Can I use Applescript to manage a program without bringing this program to the forefront?

I use Applescript in combination with Alfred to provide a keyboard shortcut to start and stop the timer in Billings . This is really useful for my tracking of my freelance work.

Here is the code I'm using:

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

I got it in the Alfred extension for activation using CTRL + ALT + CMD + T.

Unfortunately, this is a little awkward. Until yesterday, I never touched Applescript, so I'm afraid my chops are a bit poor. The Billings window instantly appears in the foreground and then hides again. It will also not hide if modifier keys other than H are pressed on the keyboard.

In this way:

  • Billings, ?
  • , , .

: - OSX. , , , . . ( , ) . , Billings , . : A picture of the timer

+3
2

, .

-- 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
+4

:

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
+2

All Articles