5

I'm new to AppleScript, and am just diving into UI Scripting. I'm attempting to create a script for the program TypeIt4Me, which is a menu bar utility (text expansion) that has no AppleScript support.

My proposed script would utilize System Events to click its menu bar icon, type down five times, right once, and Return. However, AppleScript pauses for a long time between clicking the icon and performing the first keystroke, to the point that this is impractical as a script. Below is the version that has the first click and one down press.

tell application "System Events"

click menu bar item 1 of menu bar 2 of application process "TypeIt4Me"
key code 125 -- ie "down"

end tell

Could this be a problem with my code? A setting somewhere? AppleScript in general? Thanks.

  • Reminds me of this: http://stackoverflow.com/questions/16126027/applescript-delay-issue Basically yeah, GUI Scripting often does a big delay after `click`. – matt May 04 '13 at 02:18

2 Answers2

2

Your script also takes about 5 seconds to run for me. Delays like that are not very common though.

This made the script return after about 0.05 seconds, but I couldn't figure out how to run a key code command after it without the delay.

ignoring application responses
    tell application "System Events" to tell process "TypeIt4Me"
        click menu bar item 1 of menu bar 2
    end tell
end ignoring

This didn't work either:

with timeout of 0.2 seconds
    try
        tell application "System Events" to tell process "TypeIt4Me"
            click menu bar item 1 of menu bar 2
        end tell
    end try
end timeout
tell application "System Events"
    key code 125
end tell

Terminating System Events between the commands did work though:

ignoring application responses
    tell application "System Events" to tell process "TypeIt4Me"
        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"
    key code 125
end tell
Lri
  • 26,768
  • 8
  • 84
  • 82
  • I am facing the same problem of exactly 5 sec delay after "click" on bluetooth before the next menu item gets clicked. Is there any way to avoid this delay. Code given below `tell application "System Events" to tell process "SystemUIServer" set bt to (first menu bar item whose description is "bluetooth") of menu bar 1 click bt tell (first menu item whose title is "SBH80") of menu of bt click tell menu 1 if exists menu item "Disconnect" then click menu item "Disconnect" else click menu item "Connect" end if end tell end tell end tell` – Aviral Bansal Apr 01 '16 at 19:36
  • Was able to fix it by enclosing the "click bt" command in an "ignoring application response" block. It works perfectly fine now without delay. So the command causing UI delay only should be enclosed in the ignore block. Thanks a lot for showing the direction. – Aviral Bansal Apr 02 '16 at 04:46
  • Have posted a detailed answer in this thread --> [link](http://stackoverflow.com/questions/16126027/applescript-delay-issue/36370778#36370778) – Aviral Bansal Apr 02 '16 at 08:07
0

What icon do you click? Do you mean double-click? Perhaps you mean "starting the script"? If you mean that it takes a long time for your script to actually start running, this is typical of AppleScript and is not caused by your script. If TypeIt4Me can run apps instead of scripts, or if compiling and running the script as an application is an option, you should be able to speed it up a little. It would still need some time to start up but the script won't need to compile first.

Mark
  • 2,380
  • 11
  • 29
  • 49