How to click button in AppleScript? - applescript

Writing an AppleScript to open Image Capture and click the Import All button.
tell application "Image Capture"
activate
tell application "System Events"
tell process "Image Capture"
click button "Import All" of group 1 of splitter group 1 of window 1
end tell
end tell
end tell
Image Capture opens but the script throws an error message saying it couldn't find the button "Import All".
Have followed advice on other threads on how to check the location in Accessibility Inspector and how to translate that to AppleScript instructions.
What's missing?

To get the button and group numbers, you have 2 ways: use the utility aplication provided by Apple in the developper toolkit "Accessibility Inspector" or use small scripts to find the number yourselves.
I prefer using script method. it is a bit longer sometime, but it always works. Just write a script with instruction get UI elements. Here is a small example of such script:
-- return lis of UI elements of active application and paste result in Excel
property tab : ASCII character 9
global T
-- to find last active application
tell application "System Events"
set frontmostProcess to first process where it is frontmost
set visible of frontmostProcess to false
repeat while (frontmostProcess is frontmost)
delay 0.2
end repeat
set secondFrontmost to name of first process where it is frontmost
set frontmost of frontmostProcess to true
end tell
set ActifProcess to secondFrontmost as text
tell application ActifProcess to activate -- set active the last actived application
delay 1
-- recursive loop to list all elements of active window
tell application "System Events" to tell process ActifProcess to set myWindow to front window
set T to ""
getUI(myWindow, 1)
set the clipboard to T
display dialog "Result is in clipboard. paste in Excel or text document"
on getUI(UIObjet, myLevel) -- get recursively the list of UI elements
set AppleScript's text item delimiters to {"//"}
tell application "System Events" to set UIliste to UI elements of UIObjet
repeat with oneUI in UIliste
tell application "System Events"
set UItext to ("Level=" & myLevel & tab & "Name=" & (name of oneUI) & tab & (description of oneUI) & tab & "Class=" & (class of oneUI) as string) & tab & "Title=" & (title of oneUI) as string
set UItext to (UItext & tab & "Value=" & (value of oneUI) as string) & tab
try
set UItext to (UItext & "Position=" & (position of oneUI) as text)
end try
set UItext to UItext & return
try
set NbSub to count of UI elements of oneUI
on error
set NbSub to 0
end try
set T to T & return & UItext
end tell
if NbSub > 0 then
getUI(oneUI, myLevel + 1) -- there are other sub UI elements, get them
end if
end repeat
end getUI
Copy this script in Script Editor. Make active the window/application you want to get UI elements. Make this script active and run.
The result is sometime not easy to interpret because developper of the application/window you're looking for may not have use UI element clear names or titles which describe what they are. Then you will have to look their relative position in the window.

The "import all" button is "button 3 of group 2 of splitter group 1 of window 1" for image capture version 6.6. Also, I prefer to use button number, instead of button name to make sure the script works with any language.
tell application "Image Capture"
activate
tell application "System Events"
tell process "Image Capture"
click button 3 of group 2 of group 1 of splitter group 1 of window 1
end tell
end tell
end tell
Please note that any next changes done by Apple on Image Capture will impact your script.

Related

what means "tab group 1" in AppleScript, why not "tab group 2"?

I'm trying to understand code belong, and can't find anything explain what means "tab group 1"..
And I don't know how to debug to find the value with "tab group 1"
Btw, I test "tab group 0",its ok, but "tab group 2" its error..
set devices to {}
tell application "System Preferences"
reveal pane "声音"
end tell
tell application "System Events"
tell application process "System Preferences"
repeat until exists window "声音"
end repeat
tell tab group 1 of window "声音"
get properties
click radio button "输出"
tell table 1 of scroll area 1
set selected_row to (first UI element whose selected is true)
set currentOutput to value of text field 1 of selected_row as text
repeat with r in rows
try
set deviceName to value of text field 1 of r as text
set end of devices to deviceName
end try
end repeat
end tell
end tell
end tell
end tell
if application "System Preferences" is running then
tell application "System Preferences" to quit
end if
set text item delimiters to "‡"
set devicesStr to devices as text
set comm to "bash ./main.sh" & " \"" & devicesStr & "\"" & " \"" & currentOutput & "\"" & " output"
log comm
#do shell script comm
AppleScript GUI scripting involves working its way down through the view hierarchy of the application's windows. In this case, 'Tab Group [X]' means that there are at least [X] tab groups within the container at that level of the hierarchy, and you need to determine which one contains the lower-level element you're trying to access. Unfortunately, the elements of the view hierarchy aren't always immediately visible (there may be 'hidden' containers and such), and the hierarchy may change significantly from one app update to the next. That can be headache inducing.
You can debug this manually (with a little patience) by working your way down the hierarchy yourself until you find the elements you need, using a series of every UI element of.. commands. I.e., begin with:
tell application "System Events"
tell application process "System Preferences"
tell window "声音"
get every UI element
end tell
end tell
end tell
Then choose a likely UI element from the list it produces and add a new tell block. However, it's easier to use the Accessibility Inspector app, which gives you a look into the details of any applications view hierarchy. Accessibility Inspector is included with Xcode downloads (which is free, and worth having around); I don't know if there's a place to download it separately.
See Apple's Guide of Testing Accessibility.

Apple script find and replace text

So, right now my code clicks on a textarea in safari then it adds a word using keystroke, however I was wondering if there is a way to find and replace a word in that area and also is there a way to replace a highlighted word without keystroke?
Current code:
Sorry its very very poorly formatted since I am pretty bad at applescript
to clickClassName(theClassName, elementnum)
tell application "Safari"
do JavaScript "document.getElementsByClassName('" & theClassName & "')[" & elementnum & "].click();" in document 1
end tell
end clickClassName
to clickID(theId)
tell application "Safari"
do JavaScript "document.getElementById('" & theId & "').click();" in document 1
end tell
end clickID -- All the code up to this point did is allow me to click on IDs and classes in safari.
tell application "Safari" to activate
tell application "System Events"
tell application "Safari"
tell window 1
set current tab to (make new tab with properties {URL:"https://en.wikipedia.org/w/index.php? title=User:USERNAMEHERE/sandbox&action=edit"})
end tell
end tell
end tell
delay 2 -- All the code up to this point from the last comment did is open a new tab in safari.
repeat 5 times
set randomlist to {"1","2","3","4"} -- List of numbers that will be randomly selected and put in the texarea
set randomword to some item of randomlist
clickClassName("tool-button wikiEditor-toolbar-spritedButton", 0) -- This clicks the bold button on the sandbox page
tell application "System Events"
tell process "Safari"
keystroke randomword -- Uses keystroke to put replace the text inside the bold formatting area
end tell
end tell
delay 1
clickID("wpSave") -- Clicks save
delay 2
tell application "Safari" to set the URL of the front document to "https://en.wikipedia.org/w/index.php? title=User:USERNAMEHERE/sandbox&action=edit" -- Goes back to sandbox and repeats the process
delay 2
end repeat
If you want to test this then put in your wikipedia username in the link and login into your account. Pretty much its a wikipedia bot that will just put stuff in my wikipedia sandbox

Applescript changes?

I have a problem with an applescript on an iMac I am upgrading at work, this is below, it stops at:
error number -1719 from text field 1 of window 1 of process "QuarkXPress"
The full script I am trying to run:
try
tell application "FileMaker Pro Advanced"
--tell database "artdb_Client"
set theRecord to current record of database "artdb_Client"
tell theRecord
set theCustomer to item 4
set theCustomerName to item 5
set theFileName to item 2
end tell
--end tell
end tell
tell application "QuarkXPress"
activate
display dialog ¬
theCustomer & " - " & theCustomerName default answer theCustomer buttons {"Cancel", "OK"} default button 2 ¬
with title "Enter the Customer Code for Job"
(*
display dialog ¬
theCustomer & " - " & theCustomerName & ¬
"?" with title "Export PDF for Customer"
*)
end tell
set theFolder to "Data HD:Proofs:" & theCustomer
set theFolderOfPosix to "/volumes/Data HD/Proofs/" & theCustomer
set this_item to theFolder & ":" & theFileName & ".pdf"
set theProofsDir to "Data HD:Proofs:" as alias
tell application "System Events"
tell process "QuarkXPress"
tell menu bar 1
tell menu bar item "File"
tell menu "File"
tell menu item "Export"
tell menu "Export"
click menu item "Layout as PDF..."
delay 1
end tell
end tell
end tell
end tell
end tell
end tell
repeat 5 times
tell process "QuarkXPress"
if window "Export as PDF" exists then
keystroke "G" using command down
set value of text field 1 of window 1 to theFolderOfPosix & "/"
keystroke return
click button "Go" of window 1
exit repeat
else
delay 1
end if
end tell
delay 1
end repeat
end tell
end try
I finally got it to work, where the script was stopping (without putting the called data in):
set value of text field 1 of window 1 to theFolderOfPosix & "/"
I changed it to:
set value of text field 1 of sheet 1 of window 2 to theFolderOfPosix & "/"
Thanks to UI Browser for showing me the "sheet" field I was missing, it doesn't look right but it does the job so that's all that matters I guess.
You need to provision YOUR.app for UI Scripting (Accessibility support) in the Security pane of the System Preferences. There is a good writeup on how that works here: AppleScript System Notifications Support (advanced). That is, it describes the steps necessary to make ui scripting work.
HTH

How to click a button that's only visible when hovered over?

I'm tinkering with an applescript that would clear notifications from Notification Center. At this point cannot figure out a way to click the clear button.
You see, the button only appears if cursor is hovering on the row of the app, or rows below that belongs to this app. I do:
tell application "System Events" to tell process "SystemUIServer" ¬
to click menu bar item "Notification Center" of menu bar 2
tell application "System Events" to tell process "Notification Center" ¬
to value of attribute "AXChildren" of UI element 1 of row 2 of table 1 of ¬
scroll area 1 of window "NotificationTableWindow"
Result:
{static text "iTunes" of UI element "iTunes" of row 2 of table 1 of ¬
scroll area 1 of window "NotificationTableWindow" of application process ¬
"NotificationCenter" of application "System Events"}
But if I strategically place the cursor beforehand, I get:
{static text "iTunes" of (ditto), button 1 of (ditto)}
button 1 is what I'm searching for. So far I tried three ways that didn't work out, listed from dumb to less dumb:
1) Keyboard Navigation
I tried to navigate down the list with key code 125. This doesn't make the button visible. While there, I tried a myriad of Delete combos. Nothing seems to delete notification entries.
2) click at (x,y) from System Events
On a 1280x800 screen, the button has an AXFrame of:
{x=1256.00 y=77.00 w=16.00 h=16.00}
which gives it a center of {1264, 85}, so:
tell application "System Events" to tell process "Notification Center"
click at {1264, 85}
value of attribute "AXChildren" of UI element 1 of row 2 of table 1 of ¬
scroll area 1 of window "NotificationTableWindow"
end tell
This, unsurprisingly, doesn't work.
3) select from System Events
tell application "System Events" to tell process "Notification Center" to ¬
tell window "NotificationTableWindow" to tell scroll area 1 to tell table 1 ¬
to tell row 2
select UI element 1
value of attribute "AXChildren" of UI element 1
end tell
Still, I only get static text 1 that is the app name. button 1 that is the clear button is nowhere to be found.
Does anyone know how to click this button - a button that only appears when hovered over?
I can't answer your question (the GUI Scripting, I'm not on Yosemite), but :
To clear all notifications (works on Maverick, I don't know on Yosemite), this script delete notifications from the database in "~/Library/Application Support/NotificationCenter" folder :
set notifCenterFolder to (path to application support from user domain as text) & "NotificationCenter:"
tell application "System Events" to set tDB to POSIX path of (first file of folder notifCenterFolder whose name extension is "db")
do shell script "/usr/bin/sqlite3 " & (quoted form of tDB) & " 'DELETE FROM notifications' && osascript -e 'quit application \"NotificationCenter\" ' && killall usernoted"
--
To clear notifications for a specific application (iTunes as example):
set iTunesPath to "/Applications/iTunes.app"
set notifCenterFolder to (path to application support from user domain as text) & "NotificationCenter:"
tell application "System Events" to set tDB to POSIX path of (first file of folder notifCenterFolder whose name extension is "db")
do shell script "/usr/bin/sqlite3 " & (quoted form of tDB) & " 'delete FROM notifications where app_id = (select app_id FROM app_source where last_known_path = \"" & iTunesPath & "\")' && osascript -e 'quit application \"NotificationCenter\" ' && killall usernoted"
--
On Yosemite, this file is in another folder --> /var/folder/... see this answer
Use this AppleScript on Yosemite :
do shell script "cd `getconf DARWIN_USER_DIR`com.apple.notificationcenter/db/ && /usr/bin/sqlite3 db 'DELETE FROM notifications' && osascript -e 'quit application \"NotificationCenter\" ' && killall usernoted"

Applescript delay issue

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

Resources