Working AppleScript fails when put into Workflow - applescript

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

Related

Applescript select all checkboxes in scroll area

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.

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

Charles and AppleScript (missing values in Accessibility Inspector)

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!

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

Applescript automation flipping checkbox not always working

I have the following applescript that is acting strangely. If the file sharing checkbox is unchecked it will check it everytime. If the file sharing checkbox is already checked it will sometimes uncheck it. then when it comes to the if statement that is to determine if the current state of the file sharing checkbox(this is to take place after the checking action) it never reads it as checked.
here is the thought proccess:
open system prefs (works)
show sharing pane (works)
click the file sharing check box, row 3. (sort of works. checks if unchecked. does not uncheck)
get current state of file sharing box, generate appropriate message(does not work)
quit system prefs(works)
Display message about what action was taken(works)
Apple Script
tell application "System Preferences"
activate
reveal (pane id "com.apple.preferences.sharing")
end tell
tell application "System Events"
tell process "System Preferences"
try
click checkbox of row 3 of table 1 of scroll area of group 1 of window "Sharing"
delay 2
if checkbox of row 3 of table 1 of scroll area of group 1 of window "Sharing" is equal to 1 then
set response to "File Sharing turned on"
else
set response to "File Sharing turned off"
end if
tell application "System Preferences" to quit
activate (display dialog "Flipped")
on error
activate
display dialog "something went wrong in automation but you are in the right menu..."
return false
end try
end tell
end tell
Most of the time, the problem is that the window is not yet fully shown, so you should ask the UI element whether it is already available:
== I´ve edited the script below to reflect changes based on the comments.
tell application "System Preferences"
activate
reveal (pane id "com.apple.preferences.sharing")
end tell
tell application "System Events" to tell table 1 of scroll area of group 1 of window 1 of process "System Preferences"
tell (1st row whose value of static text 1 is "File Sharing")
set sharingStatus to value of checkbox 1 as boolean
if sharingStatus is true then
click checkbox 1
my notify("File Sharing is now turned off")
else
click checkbox 1
my notify("File Sharing is now turned on")
end if
end tell
end tell
on notify(notification)
display dialog notification
end notify

Resources