EDIT: I'm trying to save a session file from the Web Proxy Debugging App Charles (http://www.charlesproxy.com/) using AppleScript. Basically, I select "Export", put in a temp name, and then save it. However, after I click on combo box 2, which is the "Format" area, and then try to click on pop up button "XML Session File (.xml)", the Applescript Editor throws an error saying it can't find it.
At the moment I hacked it with the following code, but for some reason it only works on the Applescript Editor and sometimes in Terminal/my code, especially when I am doing other actions at the same time.
tell application "Charles"
activate
end tell
tell application "System Events"
tell process "Charles"
tell menu bar 1
click menu bar item "File"
tell menu "File"
click menu item "Export..."
end tell
end tell
tell window "Save"
keystroke "tempCharles"
delay 0.5
click combo box 2
delay 0.5
key code 125 -- down arrow
delay 0.2
key code 125
delay 0.2
key code 125
delay 0.2
key code 125
delay 0.2
keystroke return
delay 0.4
keystroke return
delay 0.4
keystroke return
end tell
end tell
end tell
I want my code to look something like this
tell window "Save"
keystroke "tempCharles.xml"
delay 3
click combo box 2
tell combo box 2
click pop up button "XML Session File (.xml)"
end tell
click button "Save"
end tell
Any hack is fine too. Before posting, trying running "osascript" on Terminal to check if it works not through AppleScript Editor.
set value of text field 1 of window 1 didn't seem to work either, but you could try just using keystroke:
delay 0.5 -- time to release modifier keys if the script is run with a shortcut like cmd-R
tell application "System Events" to tell process "Charles"
set frontmost to true
click menu item "Save..." of menu 1 of menu bar item "File" of menu bar 1
keystroke "templog" & return
end tell
Yep that worked! I just added
-- Got rid of the "set text" line
keystroke return
delay 1
click button "Save"
That was very subtle and I've seen that before but now I realize better what it is. Thanks a lot!
Related
As a foreword, I am very new to AppleScript.
I have a very simple, working AppleScript that always fails when run as part of a Workflow/Application in Automator. I know that the script works because I can run it within Script Editor multiple times and have no errors.
Here is the script:
activate application "Chess"
tell application "System Events" to tell process "Chess"
click menu item "Preferences…" of menu "Chess" of menu bar item "Chess" of menu bar 1
tell window 1
repeat until sheet 1 exists
delay 0.1
end repeat
end tell
tell group "Speech" of sheet 1 of window 1
set checkboxOne to checkbox "Allow Player to Speak Moves"
set checkboxTwo to checkbox "Speak Computer Moves"
set checkboxThree to checkbox "Speak Human Moves"
tell checkboxOne
set checkBoxStatus to value of checkboxOne as boolean
if checkBoxStatus is true then click checkboxOne
end tell
tell checkboxTwo
set checkBoxStatus to value of checkboxTwo as boolean
if checkBoxStatus is true then click checkboxTwo
end tell
tell checkboxThree
set checkBoxStatus to value of checkboxThree as boolean
if checkBoxStatus is true then click checkboxThree
end tell
end tell
click button "OK" of sheet 1 of window 1
end tell
Again, when run by itself, no errors. When run in Automator as part of a Workflow/Application, I get the error:
System Events got an error: Can’t get sheet 1 of window 1 of process "Chess". Invalid index.
with the highlighted line being
click button "OK" of sheet 1 of window 1
Is there something I'm missing? Why is this error occurring and how can I fix it?
Thanks in advance for any help!
Try wrapping that part of the code in its own tell statement with a delay. Like this:
tell application "System Events"
delay .2
click button "OK" of sheet 1 of window 1
end tell
Maybe you can try this code. It will toggle checkbox state.
tell group "Speech" of sheet 1 of window 1
tell checkbox 1
perform action "AXPress"
end tell
end tell
I'm using NetShade as a proxy service and thought I could try to automate the switching between the different proxies as a nice start for my first AppleScript script.
The NetShade-app has no AppleScript support, so I have to use UI scripting. After a few tries (and some posts here) I managed to have a script, that switches the proxies via the menu bar item (here is a picture of it, since I can't post it inline due to reputation limit).
Unfortunately my code is extremely slow (≈6sec), which makes it kind of impractical as a script. The first menu opens immediately, but the selection of the sub-menu and the proxy server takes several seconds.
I'm using the following code:
set theProxy to "Netshade US 4"
tell application "System Events" to tell process "NetShade"
tell menu bar item 1 of menu bar 2
click
tell menu item "NetShade Proxy" of menu 1
click
tell menu item theProxy of menu 1
click
end tell
end tell
end tell
end tell
I already tried to add ignoring application responses, like suggested in a different thread (link), but that didn't help.
So finally my questions:
Is there a way to speed the process up? Maybe even a way to do all this in the background, without showing the menu items?
P.S.: I'm running OS X 10.9.1
Summary of the fix
To remove delay you need to do two things:
(I) Identify the click which is causing the delay and enclose only that line in the ignoring application responses block as shown below. In my case, it was click bt after which the execution was going into a wait mode for 5 to 6 seconds.
ignoring application responses
click bt
end ignoring
(II) I then also had to kill System Events to and start it again using the following commands.
do shell script "killall System\\ Events"
delay 0.1
-- Rest of the code to click stuff or send keycodes
This resolved the delay issue.
Details
I was having the same problem where I created a script to connect/disconnect my bluetooth headset through AppleScript. The script is 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
The script was working fine but had a problem where it would wait for 5 to 6 seconds after executing "click bt" above. I modified the code as follows and it is working absolutely fine now without any delay.
tell application "System Events" to tell process "SystemUIServer"
set bt to (first menu bar item whose description is "bluetooth") of menu bar 1
ignoring application responses
click bt
end ignoring
end tell
do shell script "killall System\\ Events"
delay 0.1
tell application "System Events" to tell process "SystemUIServer"
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
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?
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:
Applescript: on clicking Menu Bar item via gui script
Is AppleScript UI Scripting very slow in general, or is it my script, or something else?
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.
I am testing applescripts that I will use later in my OSX app.
I'm getting a 6 sec delay after the click button command below.
After some research it seems that this is a known issue.
What I find interesting is, if i use the commercial app QuicKeys to perform the same
button click there is no delay, so I assume they found a work around.
Anybody have any ideas?
tell application "System Events"
tell process "Pro Tools"
set frontmost to 1
click button "Track List pop-up" of window 1
-- 6 seconds delay before next command is sent
key code 36 -- return key stroke
end tell
end tell
Was having the same problem and resolved it by enclosing the click causing delay in the ignoring application responses block. Here is a quick summary:
OLD CODE (Causes 6 sec delay)
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
NEW CODE (No delay)
tell application "System Events" to tell process "SystemUIServer"
set bt to (first menu bar item whose description is "bluetooth") of menu bar 1
ignoring application responses
click bt
end ignoring
end tell
do shell script "killall System\\ Events"
delay 0.1
tell application "System Events" to tell process "SystemUIServer"
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
Please check detailed answer in the thread listed below.
Speed up AppleScript UI scripting?
Hope this helps.
It seems click or axpress causes a big delay.
Instead - get position and use a third party shell script to do the clicking. Much Much faster.
using clicclik : https://www.bluem.net/en/mac/cliclick/
put in user library/application support/Click
set clickCommandPath to ((path to application support from user domain) as string) & "Click:cliclick"
set clickCommandPosix to POSIX path of clickCommandPath
tell application "System Events"
tell process "Pro Tools"
set frontmost to 1
tell button "Track List pop-up" of window 1
set {xPosition, yPosition} to position
set x to xPosition
set y to yPosition
end tell
do shell script quoted form of clickCommandPosix & " c:" & xPosition & "," & yPosition
key code 36 -- return key stroke
end tell
end tell
Im using system events to control a program that does not have a applescript library.
I am therefor using system events to control it.
I have gotten the program to open a pop up window for it Open File interface and I would like to get it to default to a certain location. Is this possible.
So Far I have :
tell application "App Name"
activate
end tell
tell application "System Events"
tell process "App Name"
tell menu bar 1
tell menu bar item "File"
tell menu "File"
tell menu item "Import"
tell menu "Import"
click menu item "XML..."
delay 4
end tell
end tell
end tell
end tell
end tell
end tell
end tell
The pop up window defaults to its own last visited location. I would like it to default to a given file path like /Users/userabc/Documents/abcd.XML
Thanks
If you have the "posix path" of a location and the dialog box open, you can do the following. Note that the location can be a folder or a file path. If it's a file path then that file will be selected and you would then just have to "keystroke return" to close the dialog box and open that file. Good luck.
set theLocation to path to home folder
set posixLocation to POSIX path of theLocation
tell application "System Events"
keystroke "g" using {command down, shift down}
delay 0.5
keystroke posixLocation
delay 0.5
keystroke return
end tell
The only problem with this method is that autocorrect starts filling in as apple script types into the text box and screws everything up. Work around is to copy/paste into from applescript.
The keystroke command doesn't work for inserting characters that can't be inserted with the current input source. And it doesn't work at all with some input sources.
You could also set the value of the text field:
tell application "System Events" to tell (process 1 where frontmost is true)
keystroke "g" using {shift down, command down}
tell window 1
tell sheet 1
set value of text field 1 to "/usr/share/dict/connectives"
click button 1
end tell
click button "Open"
end tell
end tell