Thanks to Axel and whoever posted the 'sorted ascending' part (was that Chris?) this script now works well. I'll use it myself a lot.
Axel, i had to modify your routine to get it to work, but you once again set me on the right track. The reason i didn't use the 'Expand All Conversations' Menu was that it proved to be very unreliable.
property x : {}
property keepCheck : ""
property StoreMailBox : ""
on run
my setUpLoop()
end run
on setUpLoop()
tell application "Mail"
activate
set x to {}
set keepCheck to ""
set StoreMailBox to ""
set theflag to true
tell (some message viewer whose index is 1)
try
set AllMailBoxes to (get selected mailboxes)
repeat with eachMailBox in AllMailBoxes
my checkLoop(eachMailBox)
end repeat
on error errmsg number errnum
if errnum = -1730 then
display dialog "Please select a mailbox"
set theflag to false
end if
end try
end tell
if theflag then my MainLoop(1)
end tell
end setUpLoop
on checkLoop(eachMailBox)
tell application "Mail"
tell (some message viewer whose index is 1)
try
set theSortedflag to (sorted ascending = true)
set theflag to true
on error errmsg number errnum
display dialog errmsg & errnum
set theflag to false
end try
if theflag then
if theSortedflag then
set temp to (messages of eachMailBox)
repeat with yy in reverse of temp
copy (id of yy) as rich text to tempcheck
if tempcheck is not in keepCheck then
set end of x to yy
set keepCheck to tempcheck & ","
end if
end repeat
else
set temp to (messages of eachMailBox)
repeat with yy in temp
copy (id of yy) as rich text to tempcheck
if tempcheck is not in keepCheck then
set end of x to yy
set keepCheck to tempcheck & ","
end if
end repeat
end if
end if
repeat with yy in mailboxes of eachMailBox
my checkLoop(yy)
end repeat
end tell
end tell
end checkLoop
on MainLoop(xx)
tell application "Mail"
activate
set theflag to true
repeat
if x = {} then
set theCount to (count of selected mailboxes of message viewers)
if theCount = 1 then
display dialog "The mailbox is empty."
else
display dialog "The mailboxes are empty."
end if
set theflag to false
exit repeat
else
set xxx to (item xx of x)
set selected mailboxes of message viewers to mailbox of xxx
delay 0.2
set selected messages of message viewers to xxx
if StoreMailBox = "" or StoreMailBox ≠ mailbox of xxx then
tell application "System Events"
tell application process "Mail"
tell table 1 of scroll area 1 of group 1 of splitter group 2 of splitter group 1 of window 1
set tempRows to rows where (value of (first UI element of UI element 1 whose role is "AXDisclosureTriangle") is 0)
repeat with tempLocator in reverse of tempRows
try
tell ((first UI element of UI element 1 whose role is "AXDisclosureTriangle") of tempLocator)
click
end tell
on error errmsg number errnum
display dialog errmsg & errnum
end try
end repeat
end tell
end tell
end tell
set StoreMailBox to mailbox of xxx
end if
tell application "Finder"
activate
display dialog "Backwards or Forwards" buttons {"Backwards", "Select this one", "Forwards"} giving up after 240
end tell
set b to button returned of the result
if b = "Select this one" then exit repeat
if b = "Backwards" then
set xx to xx - 1
if xx < 1 then set xx to count of x
end if
if b = "Forwards" then
set xx to xx + 1
if xx > (count of x) then set xx to 1
end if
end if
end repeat
end tell
if theflag then my WaitLoop(xx)
end MainLoop
on WaitLoop(xx)
tell application "Finder"
activate
display dialog "Resume, Print, or Cancel" buttons {"Resume", "Print", "Cancel"} giving up after 240
set b to button returned of the result
if b = "Resume" then my MainLoop(xx)
if b = "Print" then
tell application "Mail" to activate
tell application "System Events" to tell process "Mail" to keystroke "p" using command down
my WaitLoop(xx)
end if
end tell
end WaitLoop