Applescript select all checkboxes in scroll area - applescript

I've automated printing a weekly calendar with 'applescript' and 'Calendar'. There is a scroll area with collection of checkboxes. How do you iterate over every checkbox in a scroll area and uncheck it?
https://gist.github.com/spuder/c92dd0637ce85b6960b81e1415d7c52e
This works but is fragile since the rows are hard coded.
-- Click the “<fill in title>” checkbox.
delay 0.5
set timeoutSeconds to 2.0
set uiScript to "click checkbox 1 of row 2 of outline 1 of scroll area 1 of window \"Print\" of application process \"Calendar\""
my doWithTimeout(uiScript, timeoutSeconds)
-- Click the “<fill in title>” checkbox.
delay 1
set timeoutSeconds to 2.0
set uiScript to "click checkbox 1 of row 3 of outline 1 of scroll area 1 of window \"Print\" of application process \"Calendar\""
my doWithTimeout(uiScript, timeoutSeconds)
-- Click the “<fill in title>” checkbox.
delay 1
set timeoutSeconds to 2.0
set uiScript to "click checkbox 1 of row 4 of outline 1 of scroll area 1 of window \"Print\" of application process \"Calendar\""
my doWithTimeout(uiScript, timeoutSeconds)
This seems like it should work but it does not uncheck any of the boxes
delay 1
set timeoutSeconds to 2.0
set uiScript to "click checkbox 1 of every row of outline 1 of scroll area 1 of window \"Print\" of application process \"Calendar\""
my doWithTimeout(uiScript, timeoutSeconds)

This works for me using the latest version of macOS Mojave
tell application "Calendar"
activate
reopen
end tell
tell application "System Events" to tell application process "Calendar"
if not (exists of window "Print") then keystroke "p" using command down
repeat while not (exists of window "Print")
delay 0.1
end repeat
set everyCheckboxRef to a reference to every checkbox of rows of outline 1 ¬
of scroll area 1 of window 1
repeat with i from 1 to count of everyCheckboxRef
set thisCheckbox to item i of everyCheckboxRef
if value of thisCheckbox is 1 then perform action "AXPress" of thisCheckbox
end repeat
end tell

The following example AppleScript code is one way to achieve the goal of unchecking all checkboxes in the Calendars section of the Print dialog box in the Calendar application:
-- # Check to see if Calendar is open and act accordingly.
if running of application "Calendar" then
-- # Calendar is already open however, make sure the main window is showing not minimized.
tell application "Calendar"
if not (visible of window "Calendar") then set visible of window "Calendar" to true
activate -- # Bring the main window forward.
end tell
else
-- # Calendar is not open, so open it.
tell application "Calendar"
activate
-- # Wait for main window before proceeding.
repeat until exists window "Calendar"
delay 0.1
end repeat
end tell
end if
-- # Open the Print dialog box.
tell application "System Events" to keystroke "p" using command down
-- # Make sure the Print dialog box is showing before proceeding.
tell application "Calendar"
repeat until exists window "Print"
delay 0.1
end repeat
end tell
-- # Uncheck all checkboxes in the Calendars scroll area of the Print dialog box.
tell application "System Events"
tell outline 1 of scroll area 1 of window "Print" of application process "Calendar"
repeat with i from 1 to (count rows)
tell row i
if (count UI element) > 0 then
click checkbox 1
end if
end tell
end repeat
end tell
end tell
Note: The example AppleScript code is just that and does not contain any additional 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. Additionally, UI Scripting may require the use of the delay command as appropriate, needed or wanted.

Related

Notes export all as PDF

Ability to export all Notes in macOS Notes.app as PDFs.
execution error: Notes got an error: AppleEvent handler failed. (-10000)
Multiple scripts, latest below.
tell application "Notes"
activate
repeat with theFolder in every folder
repeat with theNote in every note of theFolder
tell application "System Events"
tell process "Notes"
set dockPrefs to dock preferences
set appearancePrefs to appearance preferences
delay 1
display dialog "Foo"
tell menu bar 1 of process "Notes"
click menu bar item "File"
click menu item "Export as PDF..." of menu "File" of menu bar of process "Notes"
end tell
click button "Save" of sheet 1 of window "Notes" of process "Notes"
delay 1
key code 125
end tell
end tell
end repeat
end repeat
end tell
execution error: Notes got an error: AppleEvent handler failed. (-10000)
There are a few problems in there:
You are already targeting the Notes process, so including that in
the click statements is adding another process target - use one or the other, but if you are
doing a lot of menu clicks you might look at using a general
purpose handler;
The export menu item uses an ellipse (a single character), not three
periods;
By placing a display dialog statement in the System Events tell statement, you are moving the focus away from the application.
Also note that the text field is selected and the save button is the default in the sheet, so you can use keystrokes instead of trying to click UI elements. A cleaned up example (tested in Mojave) would look something like:
tell application "Notes"
launch -- seems to work better than 'activate'
repeat with aFolder in folders
repeat with aNote in notes of aFolder
set noteName to (name of aNote)
try -- keep the name a reasonable length
set noteName to text 1 thru 20 of noteName
end try
tell (current date) to set timeStamp to text 2 thru -1 of (get (1000000 + (its hours) * 10000 + (its minutes) * 100 + (its seconds)) as text) -- hhmmss
tell application "System Events"
#display dialog noteName -- testing?
tell process "Notes"
set frontmost to true -- retarget the Notes app
delay 0.5
click menu item "Export as PDF…" of menu "File" of menu bar item "File" of menu bar 1
repeat until exists sheet 1 of window 1 -- wait for the sheet
delay 0.02
end repeat
end tell
keystroke noteName & "_" & timeStamp -- update the name, trying to avoid duplicates
delay 0.5
keystroke return -- dismiss the sheet
delay 0.5
key code 125
end tell
end repeat
end repeat
end tell

UI automation, appleScript Keyboard shortcuts for non-menued items Preview?

I am trying to create a keyboard shortcut action for Preview, namely Draw and Sketch. However, they are are the NON-menued items, which means can't get it done in System Preference I see someone done it from inspiration, it is possible, but when I try to follow alone, here is my code so far and please help me complete this. here are the action.
Here is the error I am facing now
after some researches, does the UI/accessibility inspector help?
activate application "Preview"
delay 0.4
set the menuItem to "Draw"
tell application "System Events"
try
tell application process "Preview" to click radio button menuItem of radio group 1 of splitter group 1 of window 1
on error
try
tell application process "Preview" to click radio button menuItem of radio group 1 of window 1
on error errorM
display dialog errorM
end try
end try
end tell
ideally, trying to make it to work when all bars are hidden,
but if not possible. can we make it to work vwith mark up bar is shown. as below.
This work for me using the latest version of macOS Mojave
tell application "Preview" to activate
repeat while application "Preview" is not running
delay 0.2
end repeat
tell application "System Events"
try
click menu item "Show Markup Toolbar" of menu 1 of menu bar item "View" of menu bar 1 of application process "Preview"
end try
delay 0.5
try
click menu item "Show Toolbar" of menu 1 of menu bar item "View" of menu bar 1 of application process "Preview"
end try
delay 0.5
repeat while not (exists of toolbar 1 of window 1 of application process "Preview")
delay 0.2
end repeat
set description2 to a reference to every checkbox of toolbar 1 of window 1 of application process "Preview"
set theCheckboxes to description of description2
if item 1 of theCheckboxes is "Draw" then
set checkBoxDraw to 1
else
set checkBoxDraw to 2
end if
if item 1 of theCheckboxes is "Sketch" then
set checkBoxSketch to 1
else
set checkBoxSketch to 2
end if
delay 1
-- Below, insert either checkBoxSketch for "Sketch" or checkBoxDraw for "Draw"
click checkbox checkBoxDraw of toolbar 1 of window 1 of application process "Preview"
end tell
-- without these next following lines, the toolbar "Draw" or "Sketch" do not appear to be selected
tell application "Preview" to tell window 1
set visible to false
set visible to true
end tell
delay 3

Working AppleScript fails when put into Workflow

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

How to download a new voice for say's function with AppleScript?

I'm searching a method to install different voices (2 to be exact) on the system preferences.
The voices are "Alex" for an english voice and "Thomas" for a french voice.
I've tried directly by console but didn't succeed, that's why I've turned to the AppleScript language, but I never used this language.
The code I've for the moment is
set osver to system version of (system info)
if osver is equal to "10.6.8" then
display dialog ("Downloading voices is only available in OS X Lion and higher")
else
tell application "System Preferences"
activate
reveal (pane id "com.apple.preference.speech")
end tell
try
tell application "System Events"
click radio button 2 of tab group 1 of window 1 of process "System Preferences"
repeat until (exists pop up button of tab group 1 of window 1 of process "System Preferences")
delay 2
end repeat
delay 2
click pop up button 1 of tab group 1 of window 1 of process "System Preferences"
delay 2
click menu item -1 of menu 1 of pop up button of tab group 1 of window 1 of process "System Preferences"
delay 2
end tell
on error
display dialog ("An error happend")
end try
end if
This program is opening the voice window but the display dialog appears every time whatever the index I put.
If you have another idea to download the voices, or if you can help me to understand what is not working, I will be grateful.
This worked for me in 10.9:
tell application "System Preferences"
reveal anchor "TTS" of pane id "com.apple.preference.speech"
activate
end tell
tell application "System Events" to tell window 1 of process "System Preferences"
tell pop up button 1 of tab group 1
click
click menu item "Customize..." of menu 1
end tell
delay 1
repeat with r in UI element 1 of rows of table 1 of scroll area 1 of sheet 1
if exists static text 1 of r then
if {"Alex", "Thomas"} contains value of static text 1 of r then
if value of checkbox 1 of r is 0 then click checkbox 1 of r
end if
end if
end repeat
click button "OK" of sheet 1
end tell
It took multiple seconds to run the script though.

How can I make this ApplesScript execute instantly without a 13 second delay?

I was trying to make a shortcut on OSX Mountain Lion to "Share a file" with the LogMeIn Application as one does not exist. I opened Automator and created an Application under "watch me do". I clicked record and
navigated to the menu bar and clicked on "LogMeIn">"Share a file..." and clicked "Share a file" in that dialog box and stopped recording. I then copied the commands in Automator and pasted them in AppleScript editor, hoping to change a few things to make it execute faster. I chose 10x for the speed in Automator and it still takes about 13 seconds from the time I do the shortcut. If anyone knows how to change this AppleScript to make it instant, please feel free to change it.
Any help would be appreciated.
Thanks
-- Click the “<fill in title>” menu.
set timeoutSeconds to 2.0
set uiScript to "click menu bar item 1 of menu bar 1 of application process \"LogMeIn Menubar\""
my doWithTimeout(uiScript, timeoutSeconds)
-- Share a file...
set timeoutSeconds to 2.0
set uiScript to "click menu item \"Share a file...\" of menu 1 of menu bar item 1 of menu bar 1 of application process \"LogMeIn Menubar\""
my doWithTimeout(uiScript, timeoutSeconds)
-- Click the “Share a file...” button.
set timeoutSeconds to 2.0
set uiScript to "click UI Element \"Share a file...\" of group 1 of window \"LogMeIn\" of application process \"LogMeIn\""
my doWithTimeout(uiScript, timeoutSeconds)
on doWithTimeout(uiScript, timeoutSeconds)
set endDate to (current date) + timeoutSeconds
repeat
try
run script "tell application \"System Events\"
" & uiScript & "
end tell"
exit repeat
on error errorMessage
if ((current date) > endDate) then
error "Can not " & uiScript
end if
end try
end repeat
end doWithTimeout
That's really cool. I didn't know you could copy/paste those "watch me do" commands in AppleScript Editor. I think I'll find a use for that.
Anyway you may not be able to make your code "instant" because ui scripting just does that. However if I was writing your code as a regular applescript, here's what it would look like. Try it, maybe it will help. I can't test it because I do not have that application. Good luck.
NOTE: I made "Share a file…" as "Share a file" and an ellipsis character (option-semicolon). If you have problems try changing the ellipsis to 3 periods.
set timeoutSeconds to 2.0
tell application "LogMeIn Menubar" to activate
tell application "System Events"
tell process "LogMeIn Menubar"
click menu bar item 1 of menu bar 1
-- delay until it opens
set startTime to current date
repeat until exists menu 1 of menu bar item 1 of menu bar 1
delay 0.2
if (current date) - startTime is greater than timeoutSeconds then
error "Waited too long for menu 1 of menu bar item 1 of menu bar 1"
end if
end repeat
click menu item "Share a file…" of menu 1 of menu bar item 1 of menu bar 1
-- delay until it opens
set startTime to current date
repeat until exists group 1 of window "LogMeIn"
delay 0.2
if (current date) - startTime is greater than timeoutSeconds then
error "Waited too long for group 1 of window \"LogMeIn\""
end if
end repeat
click UI element "Share a file…" of group 1 of window "LogMeIn"
end tell
end tell

Resources