I want to select a given message, which I know the message id of, in it’s mailbox, in Mail.app.
I’ve tried what is supposed to be a correct method, but it fails with an error message that it cannot get the nominated mailbox. The mailbox, and the message, exist. Basic amended script extract below.
Any advice please?
Regards
Santa
tell application "Mail"
activate
set m to message 20 of mailbox "* items to manually process"
set theID to message id of m
open m
if (count of message viewers) is 0 then make new message viewer
say (exists mailbox "* items to manually process") --< 'true'
tell the front message viewer
set selected mailboxes to {mailbox "* items to manually process"}
set m to (first message whose message id is theID) of mailbox "* items to manually process"
set selected messages to {m}
end tell
end tell
Within a message-viewer-tell-block you have to properly reference a mailbox (see below).
tell application "Mail"
set mailApp to a reference to it
set someMailbox to mailbox 1 of account 1
set myMailbox to mailbox "A_TEST_MOVE"
tell (some message viewer whose index is 2)
set mbxList to selected mailboxes
# Fails
set selected mailboxes to {mailbox "A_TEST_MOVE"}
# Works
set selected mailboxes to {someMailbox}
# Works
set selected mailboxes to {mailbox "A_TEST_MOVE" of mailApp}
# Works
set selected mailboxes to {myMailbox}
end tell
end tell
Your script works fine, thank you, but I also need to select a particular message out of a mailbox which might contain 400+ messages.
I’ve tried, and failed, to add to your suggestions.
Any further advice please?
Regards
Santa
tell application "Mail"
activate
set mailApp to a reference to it
if (count of message viewers) is 0 then make new message viewer
set theMessage to message 2 of mailbox "* items to manually process"
set theID to message id of theMessage
set theMailBox to "* items to manually process"
set myMailbox to mailbox theMailBox
set m to first message of myMailbox whose message id is theID
say name of mailbox of m as rich text --< Works!
tell (some message viewer whose index is 1)
# Works
set selected mailboxes to {myMailbox}
#
# Fails
# tell myMailbox to set selected messages to {m}
#
# Doesn't select
set selected messages to {m}
#
say (count of selected messages) --< zero!
end tell
end tell
I’m a bit puzzled. Here the script works. The events log is :
tell application "Mail"
activate
count every message viewer of current application
make new message viewer
get message 2 of mailbox "Seedx/En cours"
get message id of message id 55136 of mailbox "Seedx/En cours"
get mailbox "Seedx/En cours"
get message 1 of mailbox "Seedx/En cours" whose message id = "559596989912f_83811153cc961a3@xseedtools.apple.com.mail"
get name of mailbox of message id 55136 of mailbox "Seedx/En cours"
say "En cours"
end tell
tell current application
say "En cours"
end tell
tell application "Mail"
set selected mailboxes of some message viewer whose index = 1 to {mailbox "Seedx/En cours"}
set selected messages of some message viewer whose index = 1 to {message id 55136 of mailbox "Seedx/En cours"}
count every selected messages of some message viewer whose index = 1
say 1
end tell
tell current application
say 1
end tell
To get rid of the double call to the say command, I edited the code this way :
set theMailBox to "* items to manually process"
set theMailBox to "Seedx/En cours"
tell application "Mail"
activate
set mailApp to a reference to it
if (count of message viewers) is 0 then make new message viewer
set theMessage to message 2 of mailbox theMailBox
set theID to message id of theMessage
--set theMailBox to theMailBox
set myMailbox to mailbox theMailBox
set m to first message of myMailbox whose message id is theID
name of mailbox of m
tell me to say result --< Works!
tell (some message viewer whose index is 1)
# Works
set selected mailboxes to {myMailbox}
#
# Fails
# tell myMailbox to set selected messages to {m}
#
# Doesn't select
set selected messages to {m}
#
(count of selected messages)
tell me to say result --< zero!
end tell
end tell
Now, the events log is clean :
tell application "Mail"
activate
count every message viewer of current application
make new message viewer
get message 2 of mailbox "Seedx/En cours"
get message id of message id 55136 of mailbox "Seedx/En cours"
get mailbox "Seedx/En cours"
get message 1 of mailbox "Seedx/En cours" whose message id = "559596989912f_83811153cc961a3@xseedtools.apple.com.mail"
get name of mailbox of message id 55136 of mailbox "Seedx/En cours"
end tell
tell current application
say "En cours"
end tell
tell application "Mail"
set selected mailboxes of some message viewer whose index = 1 to {mailbox "Seedx/En cours"}
set selected messages of some message viewer whose index = 1 to {message id 55136 of mailbox "Seedx/En cours"}
count every selected messages of some message viewer whose index = 1
end tell
tell current application
say 1
end tell
Yvan KOENIG (VALLAURIS, France) dimanche 12 juillet 2015 10:33:37
Unfortunately your posting won’t even compile on my iMac running 10.10.4 Yosemite, let alone run.
Here’s my try at amending your code, because I need to use variables. The mailbox could be any of a whole years worth, every day, with 400+ in each days mailbox.
Regards
Santa
tell application "Mail"
activate
if (count of message viewers) is 0 then make new message viewer
set theMailbox to "* items to manually process" as rich text # Could be any of 365 mailboxes! So need variable.
set theMessage to message 2 of mailbox theMailbox
set theMessageID to message id of theMessage
set theMailbox to "* items to manually process"
set myMailbox to mailbox theMailbox
set theShortID to id of first message of mailbox theMailbox whose message id is theMessageID
display dialog theShortID -- < 71166
#
# Won't compile!
# say name of mailbox of message id 71166 of mailbox theMailbox
end tell
tell application "Mail"
set selected mailboxes of some message viewer whose index = 1 to {myMailbox}
#
# Doesn't compile! theShortID errors! Also errors if I replace it with 71166
set selected messages of some message viewer whose index = 1 to {message id theShortID of mailbox themailbox} #
end tell
Ooooops!! My mistake. I didn’t read your posting correctly, and tried to run your log as a script that I thought you’d come up with. Sorry.
Your amended script works, thank you, but I thought that Mail would actually select the matching message visually, as though it had been clicked on.
So, my next question is…
Is it possible to actually scroll the mail item list window to the matching email, and visually select it? A lot to ask for, I know, but I’m trying to present the matching email to the user of my App.
Yvan, your amended script worked the first time I ran it after re-booting, but now has stopped. I’ve removed the first ‘say’ statement, and tried adding delays, and amended the message viewer to the front viewer, but the log still shows the actual email as being supposed to be selected, but the log shows no selection.
LOG...
tell application "Mail"
activate
count every message viewer of current application
get message 2 of mailbox "* items to manually process"
get message id of message id 71166 of mailbox "* items to manually process"
get mailbox "* items to manually process"
get message 1 of mailbox "* items to manually process" whose message id = "898A6A8B-7745-48F0-A9E6-0BFF06170CA3@gmail.com"
set selected mailboxes of message viewer 1 to {mailbox "* items to manually process"}
set selected messages of message viewer 1 to {message id 71166 of mailbox "* items to manually process"}
count every selected messages of message viewer 1
end tell
tell current application
say 0
end tell
tell application "Mail"
activate
set mailApp to a reference to it
if (count of message viewers) is 0 then make new message viewer
set theMessage to message 2 of mailbox theMailBox
set theID to message id of theMessage
--set theMailBox to theMailBox
set myMailbox to mailbox theMailBox
set m to first message of myMailbox whose message id is theID
# name of mailbox of m
# tell me to say result --< Works!
tell front message viewer
# Works
set selected mailboxes to {myMailbox}
#
#
# Doesn't select
set selected messages to {m}
#
(count of selected messages)
tell me to say result --< zero
end tell
end tell
If anyone reading this happens to be running the same, I’d appreciate you running the script below, amended to use one of your own mailboxes, and post the result. I get a count of ‘1’ after a Computer re-boot, but ‘Zero’ on subsequent runs of the script.
If you get a Zero result, I’ll lodge a bug report.
Regards
Santa
set my theMailbox to "* items to manually process"
tell application "Mail"
activate
set mailApp to a reference to it
if (count of message viewers) is 0 then make new message viewer
--set theMailBox to theMailBox
set myMailbox to mailbox theMailbox
set theMessage to message 2 of myMailbox
set theID to message id of theMessage
set m to first message of myMailbox whose message id is theID
tell front message viewer
# Works
set selected mailboxes to {myMailbox}
#
#
# Doesn't select
set selected messages to {m}
#
(count of selected messages)
tell me to say result --< zero
end tell
end tell
Model: Late 2014 retina i7, Yosemite
AppleScript: 2.4
Browser: Safari 600.2.5
Operating System: Mac OS X (10.10)
I added a loop in the code to see how it behaves when it’s urged to select different messages.
set theMailBox to "* items to manually process"
set theMailBox to "Seedx/En cours"
tell application "Mail"
activate
set mailApp to a reference to it
if (count of message viewers) is 0 then make new message viewer
repeat with i from 2 to 8 by 2
set theMessage to message i of mailbox theMailBox
set theID to message id of theMessage
set myMailbox to mailbox theMailBox
set m to (first message of myMailbox whose message id is theID)
name of mailbox of m
tell me to say result --< Works!
tell (some message viewer whose index is 1)
# Works
set selected mailboxes to {myMailbox}
class of (get selected mailboxes)
log result --> (*list*)
# Select flawlessly
set selected messages to {m}
-- class of selected messages # FAILS
selected messages
class of result # Works
set selectedMessages to selected messages
class of selectedMessages # Works
-- item 1 of selected messages -- FAILS
selected messages
item 1 of result # Works
item 1 of (get selected messages) # Works
set theMessage to item 1 of selectedMessages
log result
--> (*message id 55136 of mailbox Seedx/En cours*)
--> (*message id 53742 of mailbox Seedx/En cours*)
--> (*message id 51953 of mailbox Seedx/En cours*)
--> (*message id 51879 of mailbox Seedx/En cours*)
subject of theMessage
log result
--> (*AppleSeed: OS X Yosemite 10.10.4 Update Released*)
--> (*AppleSeed: New build of 10.11 Available*)
--> (*AppleSeed: New Build of OS X Yosemite 10.10.4 available*)
--> (*AppleSeed: OS X El Capitan Seed Invitation*)
#
count of selected messages
tell me to say result --> 1
end tell
end repeat
end tell
It did the job flawlessly.
The selected message what correctly highlighted in the viewer.
I was puzzled because I was getting an error when I tried to grab properties of selected messages.
You may see in the script which syntax works.
Yvan KOENIG (VALLAURIS, France) lundi 13 juillet 2015 11:41:46
There’s definitely something wrong with my setup, I think.
Regards
Santa
set theMailBox to "* items to manually process"
tell application "Mail"
activate
set mailApp to a reference to it
if (count of message viewers) is 0 then make new message viewer
repeat with i from 1 to 3
set theMessage to message i of mailbox theMailBox
set theID to message id of theMessage
set myMailbox to mailbox theMailBox
set m to (first message of myMailbox whose message id is theID)
name of mailbox of m
tell me to say result --< Works!
tell (some message viewer whose index is 1)
# Works
set selected mailboxes to {myMailbox}
class of (get selected mailboxes)
log result --> (*list*)
set selected messages to {m}
-- class of selected messages # FAILS
selected messages
class of result # Works
set selectedMessages to selected messages
class of selectedMessages # Works, returns (*list*)
selected messages
item 1 of result # Errors with 'missing value'
item 1 of (get selected messages)
set theMessage to item 1 of selectedMessages
log result
subject of theMessage
log result
#
count of selected messages
tell me to say result --> 0
end tell
end repeat
end tell
Log…
tell application "Mail"
activate
count every message viewer of current application
--> 1
get message 1 of mailbox "* items to manually process"
--> message id 71169 of mailbox "* items to manually process"
get message id of message id 71169 of mailbox "* items to manually process"
--> "20150712062338.92ABBEF0AC78@prmac.com"
get mailbox "* items to manually process"
--> mailbox "* items to manually process"
get message 1 of mailbox "* items to manually process" whose message id = "20150712062338.92ABBEF0AC78@prmac.com"
--> message id 71169 of mailbox "* items to manually process"
get name of mailbox of message id 71169 of mailbox "* items to manually process"
--> "* items to manually process"
end tell
tell current application
say "* items to manually process"
end tell
tell application "Mail"
set selected mailboxes of some message viewer whose index = 1 to {mailbox "* items to manually process"}
get selected mailboxes of some message viewer whose index = 1
--> {mailbox "* items to manually process"}
(*list*)
set selected messages of some message viewer whose index = 1 to {message id 71169 of mailbox "* items to manually process"}
get selected messages of some message viewer whose index = 1
--> missing value
get selected messages of some message viewer whose index = 1
--> missing value
get selected messages of some message viewer whose index = 1
--> missing value
Result:
error "Can't get item 1 of missing value." number -1728 from item 1 of missing value
If I select some messages in a different mailbox, and run my script (attached), it say the count of the selected messages.
It SHOULD have de-selected the messages, so that lead me to thinking… could the fact that my mail box name starts with '* " (an asterix and space) be the problem.
Would you mind temporarily renaming your test maIlbox with a pre-ceding asterix and space, and re-testing, please.
A big thank you if you do, and you get a ‘zero’ count.
I’ve lodged a bug report, but may have to amend it if my theory proves true.
I have to keep the asterix, as it was used many years ago to ensure 4 mailboxes were placed at the top of the mailbox list, before manual sorting of mailboxes was introduced, and legacy code uses the term.
Regards
Santa
property myMailBox : ""
property theID : ""
set theMailbox to "* items to manually process"
tell application "Mail"
launch
if (count of message viewers) is 0 then make new message viewer
# set up variables, that are normally set in App
#
set my myMailBox to mailbox (theMailbox)
set theMessage to message 1 of my myMailBox
set my theID to message id of theMessage
#
# end of set up variables
set m to first message of my myMailBox whose message id is my theID
tell front message viewer
# Works
set selected mailboxes to {my myMailBox}
#
# Doesn't select
# set selected messages to {}
set selected messages to {m}
#
(count of selected messages)
tell me to say result --< zero
end tell
end tell
To get a more readable log I made an other attempt after adding some new log instructions.
This time it worked flawlessly.
Here is the events log restricted to the first message (always with no viewer open)
tell application "Mail"
activate
count every message viewer of current application
make new message viewer
get message 2 of mailbox "* Claude"
get message id of message id 48561 of mailbox "* Claude"
get mailbox "* Claude"
get message 1 of mailbox "* Claude" whose message id = "BA15009D-D6CD-476B-9590-65C82113ABE9@sfr.fr"
get name of mailbox of message id 48561 of mailbox "* Claude"
end tell
tell current application
say "* Claude"
end tell
tell application "Mail"
set selected mailboxes of some message viewer whose index = 1 to {mailbox "* Claude"}
get selected mailboxes of some message viewer whose index = 1
(*list*)
set selected messages of some message viewer whose index = 1 to {message id 48561 of mailbox "* Claude"}
get selected messages of some message viewer whose index = 1
(*list*)
get selected messages of some message viewer whose index = 1
(*list*)
get selected messages of some message viewer whose index = 1
(*message id 48561 of mailbox * Claude*)
get selected messages of some message viewer whose index = 1
(*message id 48561 of mailbox * Claude*)
(*message id 48561 of mailbox * Claude*)
get subject of message id 48561 of mailbox "* Claude"
(*Re: utilisation de excel vs numbers pour les listes*)
count every selected messages of some message viewer whose index = 1
end tell
tell current application
say 1
end tell
To be sure that we ran the same code here is the one which generated the log passed above.
set theMailBox to "* items to manually process"
set theMailBox to "* Claude" -- "Seedx/En cours"
tell application "Mail"
activate
set mailApp to a reference to it
if (count of message viewers) is 0 then make new message viewer
repeat with i from 2 to 8 by 2
set theMessage to message i of mailbox theMailBox
set theID to message id of theMessage
set myMailbox to mailbox theMailBox
set m to (first message of myMailbox whose message id is theID)
name of mailbox of m
tell me to say result --< Works!
tell (some message viewer whose index is 1)
# Works
set selected mailboxes to {myMailbox}
class of (get selected mailboxes)
log result --> (*list*)
# Select flawlessly
set selected messages to {m}
-- class of selected messages # FAILS
selected messages
class of result # Works
log result
set selectedMessages to selected messages
class of selectedMessages # Works
log result
-- item 1 of selected messages -- FAILS
selected messages
item 1 of result # Works
log result
item 1 of (get selected messages) # Works
log result
set theMessage to item 1 of selectedMessages
log result
subject of theMessage
log result
#
count of selected messages
tell me to say result --> 1
end tell
end repeat
end tell
I guess that these exchanges may be boring for some readers. You may post directly to my mailbox. Will be back here if we get some interesting infos.