I would like to be able to select output sound device for iTunes from a script (any programming language would be ok in fact).
For the moment I was able to use UI element scripting to get up to clicking on the button which gives the menu to select the speakers:
tell application "System Events"
tell window "iTunes" of process "iTunes"
set chbtn to first UI element whose help is "Choose which speakers to use."
tell chbtn
click
-- tell menu 1 to get every menu item
end tell
end tell
end tell
This works, and menu with possible choices appears. However, the applescript seems to stop after the click command, and further actions (in the place where the comment is in the code) happen only after I click somewhere on the screen myself. How can I prevent this and continue to select the menu item from this menu?
Any solution without reverting to UI scripting is also very welcome!
The solution code is
tell application "iTunes" to activate
tell application "System Events"
tell window "iTunes" of process "iTunes"
click (first UI element whose help is "Choose which speakers to use.")
keystroke "DENON" & return -- Select "DENON" airplay entry
-- keystroke "Computer" & return -- Select standard output
end tell
end tell
However, there is an annoying 4 second delay between the click and the keystroke
I've had that delay problem before when using UI scripting. You might be able to eliminate it by telling the script which elements to click. I don't have external speakers, so the elements' names and properties aren't on my computer. An easy way to get more info on the elements available is to use (not free, but excellent) UI Browser http://pfiddlesoft.com/uibrowser/. A less easy, but free, way to get more info on the elements is to use:
tell application "System Events"
tell window "iTunes" of process "iTunes"
set chbtn to first UI element whose help is "Show or hide item artwork and video viewer."
tell chbtn
entire contents
end tell
end tell
end tell
Related
Actually I have 3 questions about the same problem: controlling a window with applescript.
What should I do if I would press on button "Close Window" of application "Google Chrome"?
Is it possible to check if the window changes? For example, to see if appear a pop-up or something like that...
What about clicking on a specific place into a window? I mean, I know I can use
tell application "System Events"
click at {x,y}
end tell
but this command use the entire screen as reference system, and I want it works only on a specific window. For example, if at "{x,y}" i put "{1,1}", applescript will click on the first item on the menu bar. Is there a way I can say to "System Events" to click at "{1,1}", but on the window "Google Chrome"?
Here are three examples of how to close the front window of Google Chrome using AppleScript:
Note: The following assumes Google Chrome is running with at least one window open when you test each example AppleScript code in Script Editor.
Example one is the most straight forward way:
tell application "Google Chrome" to close front window
Example two directly clicks the close button:
tell application "System Events" to tell ¬
application process "Google Chrome" to ¬
click button 1 of front window
Example three calculates the center of the close button and clicks there:
activate application "Google Chrome"
delay 0.5
tell application "System Events" to tell ¬
application process "Google Chrome" to tell ¬
front window
set posB1 to (position of button 1)
set szB1 to (size of button 1)
set x to (item 1 of posB1) + (item 1 of szB1) / 2 as integer
set y to (item 2 of posB1) + (item 2 of szB1) / 2 as integer
end tell
tell application "System Events" to click at {x, y}
Note that in the first two examples, the front window of Google Chrome doesn't even need to be the frontmost window on the Desktop; however, with the third example it does, otherwise the click at {x, y} will not go to the intended target.
That said, example three really shouldn't be used when there it a straight forward way, as in example one, to get the job done. Example three was just a proof of concept to get the coordinates to click at. This method may be useful in some fringe cases, especially in an app that doesn't directly support AppleScript.
Note: The example AppleScript code is just that and does not contain any error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors.
In applescript GUI scripting you can simply refer to an element by name or index and tell it to click or to perform an action. For instance to click the close button on the first open window in Chrome you could use:
tell application "System Events"
tell process "Google Chrome"
tell window 1
tell button 1
click
end tell
end tell
end tell
end tell
You don't actually need to know its physical position to click one it; you just need to know that the first button in the window is the close button.
System Events always returns the position of any element in screen pixels, so if you want the position of an element in terms of its window, get the position of the element, get the position of the window, and do some addition or subtraction (e.g., if you want to click at {5,5} in a window whose position is {100, 125}, click at {105, 130})
AppleScript isn't really designed to monitor GUI changes, though if you want to be tricky and you know what change you're looking for you can do something like this:
tell application "System Events"
tell process "..."
tell window 1's pop up button 3
repeat until (exists menu 1)
delay 0.2
end repeat
-- menu 1 now exists, so the pop up button is open
end tell
end tell
end tell
...but note that this will hang the script until the menu is opened. A more elegant way to handle that is to write a script application with an idle handler, like so:
on run
-- whatever initialization is needed
end run
on idle
tell application "System Events"
try
tell process "..."
tell window 1's pop up button 3
if exists menu 1 then
-- menu 1 now exists
-- the pop up button is open
-- do what must be done
end if
end tell
end tell
on error errstr
display alert "Something went wrong" message "The script sent this error: " & errstr
end try
end tell
return 0.2
end idle
You can leave that running in the background watching for specific changes in the GUI (the 'try' statement is in case the app you're watching quits, the window closes, or something unexpected happens to the GUI).
If you haven't already, open the System Events scripting definition in Script Editor and look at the Processes Suite. That will show you all the things you can do with GUI scripting.
Trying to click the button "Choose" but no success. I have a code below trying to correct.
Tried this code, but giving me an error.
error "System Events got an error: Can’t get process \"safari\"." number -1728 from process "safari"
'''activate application "Safari"
tell application "System Events" to tell process "safari"
click button "choose" of sheet 1 of window 1
end tell
'''
here is the code I want to correct
'''button "Choose" of sheet 1 of window 1 of application process "Safari" of application "System Events"
'''
I always find it clearer to nest tell blocks; it makes debugging easier, because you can throw in log statements or other checks at different levels. So I'd use this:
tell application "System Events"
tell process "Safari"
set frontmost to true
tell window 1
tell sheet 1
click button "Choose"
end tell
end tell
end tell
end tell
Remember, System Events is picky and case-sensitive. It will find process "Safari" but throw a fit over process "safari"; indexes of objects might change without notice if things are at all dynamic; you might have to hunt through the structure to find what you want. For instance, I'll often throw in a line like properties of every button at one level or another in the code just to see what shows up in the Script Editor's logs, and what the best way to identify what I want might be.
I'm newbie to Appplescript. I need to automate certain actions on my computer related with my Bluetooth keyboards.
I want to be able to click on the remove or connect button of a keyboard in the following dialog window of the System Preferences Panel.
Dialog window
My code until this moment is as follows:
try
tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.keyboard"
end tell
tell application "System Events"
tell process "System Preferences"
click button "Set Up Bluetooth Keyboard…" of window "Keyboard"
end tell
end tell
tell application "System Events"
tell group 1 of window 1 of application process "System Preferences"
click button "remove" of "Home Keyboard"
end tell
end tell
end try
My problem is related with the remove button since is unidentified cell of an unidentified table. With unidentified, I mean without description. Maybe there is an easy solution, but I'm not able to find it. Furthermore, It could happen that more than one keyboard exists, so I need to identify the cell from the Keyboard name.
Do you know any hint related with this issue?
Thanks in advance
Here is a sample script I used to reconnect a specific mouse via Bluetooth :
tell application "System Events"
tell application "System Preferences"
activate
reveal anchor "MouseTab" of pane id "com.apple.preference.mouse"
end tell
tell application process "System Preferences"
click button "Configuration of Bluetooth mouse…" of window 1 -- see note 1
delay 1
select (first row of table 1 of scroll area 1 of sheet 1 of front window whose value of item 1 of static text of UI element 1 contains "Mouse") -- see note 1
get value of item 1 of static text of UI element 1 of row 2 of table 1 of scroll area 1 of sheet 1 of front window
click button "Done" of sheet 1 of front window -- see note 1
end tell
tell application "System Preferences" to quit
end tell
Note 1 : Be careful about the 3 lines with comment 'see note 1' : the value of the string may be different for your local language. Please adjust these 3 values.
I think for keyboard, concept should be very similar. Because it is using GUI scripting, if Apple changes the layout of Bluetooth screen preferences, it must be adjusted. This script works from Yosemite to ElCaptain.I can't test it for next systems.
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
10.7.4 OSX Lion
Applescript
I am working with an application (built in house and has no Applescript dictionary) that has a static text element I want to copy to the clipboard and send to another app but I'm having a hard time getting it to work.
The script I was using for targeting the element looked like this:
Tell application "System Events" to set frontmost of process "*application*" to true
Tell application "System Events"
Tell process "*application*"
Tell static text 1 of tab view 1 scroll area 1 of splitter group 1 of splitter group 1 of splitter group 1 of window 1
keystroke "a" using command down
delay 0.1
keystroke "c" using command down
delay 0.1
end tell
end tell
end tell
end tell
What would happen was that the wrong text from the wrong element was copied to the clipboard every time I clicked in a different spot on the application (there are numerous text fields).
I noticed in UI Accessor/Accessibility Accessor that each UI element in the application has a unique AXIdentifier value when you mouse over them.
Is there anyway to accomplishing what I am trying to do, using AXIdentifier values to target that element and copy the text from it?
Thanks for all the help this is my first post and I hope it was worthy! ~TheLarkInn
You can do this by using AppleScript's filtering. For example, to get the From: pop-up menu in a message composition window in Apple Mail, there is no accessibility description you can match on, however there is a unique AXIdentifier which you can match as follows:
tell application "System Events"
tell application process "Mail"
tell window 1
get first pop up button whose value of attribute "AXIdentifier" is "popup_from"
end tell
end tell
end tell
This is more efficient than looping in AppleScript as it only involves sending one Apple Event to System Events.
I don't think there is a way to directly do what you're trying to do. It seems like you can only access attributes once you have a handle on an element via selector. Here is a very ugly solution that does what you're asking by iterating over all UI elements, but it is really slow with bigger UIs and probably not ideal for any production level code.
tell application "System Events"
tell process "Some Process"
set tElements to entire contents of window "Some Window"
repeat with tElement in tElements
if (exists attribute "AXIdentifier" of tElement) then
if value of attribute "AXIdentifier" of tElement = "Some AXIdentifier" then set tText to value of tElement
end if
end repeat
end tell
end tell
tText
I think using UIElementInspector or Accessibility Inspector from Xcode to build a selector string is the way to go!
Tell application "*application*" to activate
Tell application "System Events"
Tell application process "*application*"
set textStaticTextValue to value of static text 1 of tab view 1 scroll area 1 of splitter group 1 of splitter group 1 of splitter group 1 of window 1
end tell
end tell