Re: Running Scripts from Mail Rules
Re: Running Scripts from Mail Rules
- Subject: Re: Running Scripts from Mail Rules
- From: Brian Christmas <email@hidden>
- Date: Sat, 28 Oct 2017 09:15:19 +1100
G’day from Oz, Steve.
Below is a Mail Rule Creation Script I’ve used, unaltered, for 10 years. Still
works in High Sierra.
HOWEVER, the script this rule calls does ALL the gathering of details, this
script just MOVES.
The delays are pretty much unnecessary, but are included to make sure the
script always works.
Hope it’s useful.
Regards
Santa
on mailRuleCreator:{}
try
repeat
tell application "Mail"
activate
try
do shell script ("sleep 0.2")
tell application "System
Events" to tell process "Mail"
try
click menu item
"Message Viewer" of menu 1 of menu bar item "Window" of menu bar 1
end try
end tell
if not (exists mailbox "* items
to shift") then make new mailbox with properties {name:"* items to shift"}
set theAddRuleMailbox to ""
try
set inBoxList to name
of every account whose enabled is true as list
on error errmsg
set inBoxList to {}
end try
end try
end tell
set addText to "" as text
if (count of inBoxList) = 0 then
tell application "System Events" to
display dialog "I cannot proceed because there isn't a set-up Mail Account to
use." & return & return & "Please set up an appropriate Mail Account." & return
& return buttons "OK" giving up after 3600
else
exit repeat
end if
end repeat
set (my eMailAddressesList) to {}
set repeatExit to false
repeat
if (count of inBoxList) = 1 then
try
set chosenList to inBoxList
end try
exit repeat
else
try
tell application "System Events"
activate
set chosenList to
choose from list inBoxList with prompt addText & "Please choose an Account to
which to add the Mail rule, " & return & return & "\"Move to '* items to
shift'\"." & return & return & "You can choose multiple mailboxes by holding
down the 'Command' key. Just tick 'Finished' after you've selected all the ones
you require." & return with title "Art Archiver add Mail rule" OK button name
"Finished" with multiple selections allowed
if result ≠ false then
set
theAddRuleMailbox to "true"
exit repeat
end if
set addText to "You
MUST choose!" & return & return
end tell
end try
end if
end repeat
on error errmsg number errNum
tell application "System Events" to display dialog
"Odds N Sods Setting Mail Rule error " & errmsg giving up after 20
end try
tell application "Mail"
activate
try
repeat
try
set (my eMailAddressesList) to
{}
if (count of inBoxList) = 1 then
set theMainAddresses to
account 1's email addresses as list
if (my
eMailAddressesList) = {} then set (my theMainEmailAddress) to item 1 of
theMainAddresses as rich text
repeat with temp in
theMainAddresses
set end of (my
eMailAddressesList) to temp as rich text
end repeat
else
repeat with y from 1 to
count of inBoxList
if (item y of
inBoxList as rich text) is in chosenList then
set
theMainAddresses to account y's email addresses as list
if (my
eMailAddressesList) = {} then set (my theMainEmailAddress) to item 1 of
theMainAddresses as rich text
repeat
with temp in theMainAddresses
set end of (my eMailAddressesList) to temp as rich text
end
repeat
end if
end repeat
end if
on error errmsg number errNum
tell application "System
Events" to display dialog "Odds N Sods Setting Mail Address error " & errmsg &
" number " & errNum giving up after 20
end try
try
delete (every rule where name
contains "items to shift")
end try
try
delete (every rule where name
contains "Autom")
end try
if not (exists rule "Move to '* items
to shift'") then
set x to 0
try
set x to x + 1
activate
tell application
"System Events" to tell process "Mail"
# tell
application "Mail" to activate
try
keystroke return
end try
click menu item
"Preferences…" of menu 1 of menu bar item "Mail" of menu bar 1
activate
do shell script
("sleep 0.1")
activate
click button 8
of toolbar 1 of window 1
do shell script
("sleep 0.1")
#
# Set
descriptive name of rule
#
activate
click button
"Add Rule" of group 1 of group 1 of window "Rules"
do shell script
("sleep 0.1")
# tell
application "Mail" to activate
set value of
text field 1 of sheet 1 of window "Rules" to "Move to '* items to shift'"
do shell script
("sleep 0.1")
#
# First, select
the 'all' menu item
#
activate window
"rules"
click pop up
button 1 of sheet 1 of window "Rules"
do shell script
("sleep 0.1")
activate window
"rules"
click menu item
"all" of menu 1 of pop up button 1 of sheet 1 of window "Rules"
#
# Set first
scroll area, 'Conditions'
#
set y to 1
repeat with
theAddRuleMailbox in chosenList
try
click pop up button y of scroll area 1 of sheet 1 of window 1
on
error errmsg
tell application "System Events" to display dialog "1 " & errmsg
end try
do
shell script ("sleep 0.2")
try
click menu item "Account" of menu 1 of pop up button y of scroll area 1 of
sheet 1 of window "Rules"
end try
do
shell script ("sleep 0.2")
try
click pop up button y of scroll area 1 of sheet 1 of window "Rules"
on
error errmsg
tell application "System Events" to display dialog " 2 " & errmsg
end try
do
shell script ("sleep 0.2")
activate window "rules"
try
click menu item (theAddRuleMailbox as text) of menu 1 of pop up button y of
scroll area 1 of sheet 1 of window "Rules"
on
error errmsg
tell application "System Events" to display dialog "3 " & errmsg
end try
do
shell script ("sleep 0.2")
try
click button y of scroll area 1 of sheet 1 of window 1
on
error errmsg
tell application "System Events" to display dialog "4 " & errmsg
end try
do
shell script ("sleep 0.2")
set y
to y + 2
#
end repeat
do shell script
("sleep 0.1")
#
click pop up
button y of scroll area 1 of sheet 1 of window "Rules"
do shell script
("sleep 0.1")
click menu item
"Every Message" of menu 1 of pop up button y of scroll area 1 of sheet 1 of
window "Rules"
#
# Set second
scroll area, 'Actions'
#
click pop up
button 1 of scroll area 2 of sheet 1 of window "Rules"
click menu item
"Move Message" of menu 1 of pop up button 1 of scroll area 2 of sheet 1 of
window "Rules"
do shell script
("sleep 0.1")
click pop up
button 2 of scroll area 2 of sheet 1 of window "Rules"
repeat until
exists menu 1 of pop up button 2 of scroll area 2 of sheet 1 of window "Rules"
do
shell script ("sleep 0.1")
end repeat
click menu item
"* items to shift" of menu 1 of pop up button 2 of scroll area 2 of sheet 1 of
window "Rules"
do shell script
("sleep 0.1")
#
click button
"add action" of scroll area 2 of sheet 1 of window "Rules"
do shell script
("sleep 0.1")
#
click pop up
button 3 of scroll area 2 of sheet 1 of window "Rules"
repeat until
exists menu 1 of pop up button 3 of scroll area 2 of sheet 1 of window "Rules"
do
shell script ("sleep 0.1")
end repeat
click menu item
"Run Applescript" of menu 1 of pop up button 3 of scroll area 2 of sheet 1 of
window "Rules"
do shell script
("sleep 0.1")
activate
click pop up
button 4 of scroll area 2 of sheet 1 of window "Rules"
repeat until
exists menu 1 of pop up button 4 of scroll area 2 of sheet 1 of window "Rules"
do
shell script ("sleep 0.1")
end repeat
do shell script
("sleep 0.1")
try
click
menu item "Art Archiver Caller" of menu 1 of pop up button 4 of scroll area 2
of sheet 1 of window "Rules"
on error
try
click menu item 1 of menu 1 of pop up button 4 of scroll area 2 of sheet 1 of
window "Rules"
end try
end try
do shell script
("sleep 0.1")
activate
click button
"OK" of sheet 1 of window "Rules"
do shell script
("sleep 0.1")
activate
try
click
button "Don’t Apply" of sheet 1 of window "Rules"
on error
keystroke return
end try
do shell script
("sleep 0.1")
try
activate
click
button 1 of window 1
end try
end tell
my
writeFile4("shouldWeInstallMailRule", "false")
on error errmsg number errNum
end try
if (exists rule "Move to '*
items to shift'") then exit repeat
end if
end repeat
on error errmsg number errNum
tell application "System Events" to tell
process "Mail"
try
do shell script ("sleep 0.1")
# activate
click button "OK" of sheet 1 of
window "Rules"
end try
try
# activate
click button 1 of window 1
end try
end tell
display dialog "mailRuleCreator error " & errmsg
end try
try
set enabled of rule "Move to '* items to
shift'" to true
end try
try
set enabled of every rule to true
end try
end tell
end mailRuleCreator:
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden