I've been able to achieve 95% success using the following script on my Mac OS X 10.10.1 However, I can't get the email that I've opened to "Close". Any suggestions???
Here's the Applescript:
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
tell application "Mail"
-- walk through all matching messages
repeat with thisMessage in theMessages
-- open the message
set openedMail to open thisMessage
-- perform your UI scripting
tell application "System Events"
tell process "Mail"
-- Select the Print menu item
click (first menu item of menu "File" of menu bar 1 whose name begins with "Print")
tell window 1
-- Wait until the print sheet appears
repeat 30 times
if sheet 1 exists then exit repeat
delay 0.5
end repeat
tell sheet 1
-- Click the PDF button
click menu button "PDF"
-- Select the PDF to SBS Dropbox menu item
delay 0.5
click (first menu item of menu 1 of menu button "PDF" whose name begins with "PDF to SBS Dropbox")
delay 4
end tell
end tell
end tell
end tell
-- close the message
close openedMail
end repeat
end tell
end perform mail action with messages
end using terms from
If your code doesn't work then why not try the same technique you used earlier with system events...
click (first menu item of menu "File" of menu bar 1 whose name begins with "Close")
Or you can just tell mail to do it with...
close window 1
Related
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
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.
I want to automate clicking a specific pop down menu's item.
For Example, I want to change the Value of "Message receive Sound" to something else. How can I do this with AppleScript? And how can I do this with other pop down menus in AppleScript?
(To open the iMessage Settings menu, shown in the image, type CMD COMMA, once you open iMessage)
Note: I have successfully done this Automator, I just want to do it in applescript.
It's called GUI scripting. You have to identify the reference to the UI element(s).
GUI scripting strongly depends on the system version. If an update changes the UI structure the script will brake.
This selects the sound "Popcorn" in the sound popup menu. It's for El Capitan. In systems < 10.11 the UI elements may be different and the process name might be "iChat"
tell application "System Events"
tell process "Messages"
set frontmost to true
if not (exists (1st window whose value of attribute "AXIdentifier" is "MessagesPreferencesWindow")) then
keystroke "," using command down
repeat until exists (1st window whose value of attribute "AXIdentifier" is "MessagesPreferencesWindow")
delay 0.1
end repeat
end if
tell (1st window whose value of attribute "AXIdentifier" is "MessagesPreferencesWindow")
tell pop up button 4 of group 1
click
delay 0.2
click menu item "Popcorn" of menu 1
end tell
end tell
end tell
end tell
I'm trying to execute the following Applescript within a Rule I set up in Mac Mail. I'm running OS 10.10.1 Yosemite on my iMac. My Rule looks for a particular email I receive every day, then instructs Mail to execute my Applescript. The Rule works perfectly when I manually highlight the email and click on "Apply Rules"; however, when I start up my computer in the morning and receive my first mail of the day. The Applescript program gets caught in never ending loop, which is evidenced by the whirling icon that appears in the menu bar. BTW: My automator routine works perfectly. My thinking is that the Applescript gets confused trying to execute while Mail is downloading all my mail???? Any suggestions?? Oh, I'm a novice...Thx
tell application "System Events"
tell process "Mail"
-- Select the Print menu item
click (first menu item of menu "File" of menu bar 1 whose name begins with "Print")
tell window 1
-- Wait until the print sheet appears
repeat until sheet 1 exists
end repeat
tell sheet 1
-- Click the PDF button
click menu button "PDF"
-- Select the PDF to SBS Dropbox menu item
click (first menu item of menu 1 of menu button "PDF" whose name begins with "PDF to SBS Dropbox")
end tell
end tell
end tell
end tell
First you need to use the Applescript handler called by mail rules:
on perform mail action with messages theMessages for rule theRule
Inside this handler you have access to the mails that matched your mail rule conditions and do whatever you like. Here is the skeleton you can use to add your code:
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
tell application "Mail"
repeat with thisMessage in theMessages
-- place your code here
end repeat
end tell
end perform mail action with messages
end using terms from
In your special case I think we can try to put your code inside the repeat-loop and let mail open the mail before execution and close it after printing:
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
tell application "Mail"
-- walk through all matching messages
repeat with thisMessage in theMessages
-- open the message
set openedMail to open thisMessage
-- perform your UI scripting
tell application "System Events"
tell process "Mail"
-- Select the Print menu item
click (first menu item of menu "File" of menu bar 1 whose name begins with "Print")
tell window 1
-- Wait until the print sheet appears
repeat 30 times
if sheet 1 exists then exit repeat
delay 0.5
end repeat
tell sheet 1
-- Click the PDF button
click menu button "PDF"
-- Select the PDF to SBS Dropbox menu item
click (first menu item of menu 1 of menu button "PDF" whose name begins with "PDF to SBS Dropbox")
end tell
end tell
end tell
end tell
-- close the message
close openedMail
end repeat
end tell
end perform mail action with messages
end using terms from
It's not tested yet! And I prevent the script from running in an infinite loop when trying to access the print sheet now.
Give it a try! Enjoy, Michael / Hamburg
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