2

I use Time Tracker For Mac as my, well, time tracker. It has a menu bar item which I want to be able to access via keyboard shortcut.

I found a way to click the item with GUI scripting:

tell application "System Events" to tell process "Time Tracker"
    click menu bar item of menu bar 2
end tell

Unfortunately the script does not return success unless I acted on the menu (i.e. pressing Enter or Esc key). So if I want to trigger the down arrow key...

tell application "System Events" to tell process "Time Tracker"
    click menu bar item of menu bar 2
    -- hangs here forever until menu is closed
    tell application "System Events" to key code 124
end tell

the script just waits forever. If I hit escape the menu bar item closes and only then the down arrow key will be triggered.

It's kind weird. I just need the menu bar item's click to not block further script execution.

Any suggestions?

LeEnno
  • 178
  • 3
  • 13

2 Answers2

2

The click command returns after about 5 seconds for me. One workaround is to use ignoring application responses and terminate System Events:

ignoring application responses
    tell application "System Events" to tell process "Time Tracker"
        click menu bar item 1 of menu bar 2
    end tell
end ignoring
do shell script "killall System\\ Events"
delay 0.1
tell application "System Events" to tell process "Time Tracker"
    tell menu bar item 1 of menu bar 2
        click menu item 2 of menu 1
    end tell
end tell

Related questions:

Community
  • 1
  • 1
Lri
  • 26,768
  • 8
  • 84
  • 82
  • Thanks a lot. This works indeed. I'm not very comfortable to entirely kill the process, but I will try this for a while. – LeEnno May 22 '13 at 09:12
  • 1
    According to [Daemons and Services Programming Guide](http://developer.apple.com/library/mac/#documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/Lifecycle.html), loginwindow sends TERM or KILL signals to background processes and daemons at logout. System Events supports sudden termination, so it should be safe to even send it a KILL signal. – Lri May 22 '13 at 14:31
1

Actually, the key code for the down arrow seems to be 125. Try this:

tell application "System Events" to tell process "Time Tracker"
    click menu bar item of menu bar 2
    key code 125
    key code 36
end tell

There is a short delay (a couple of seconds) after the click menu bar... command, I don't know why.

Michele Percich
  • 1,872
  • 2
  • 14
  • 20