0

Does Applescript provide a mechanism for me to search for a message by subject; or do I have to write a loop and examine every message "by hand".

The code below generates this error: Mail got an error: Can’t get subject of mailbox.

tell application "Reminders"
    set snoozeList to "Snooze"
    set notCompleted to reminders in list snoozeList whose completed is false
    set numNotCompleted to (count of notCompleted)
    repeat with index from 1 to numNotCompleted
        set reminderName to name of item index of notCompleted
        tell application "Mail"
            set theMailbox to mailbox "Snooze" of account "GVSU"
            set foundIt to (message in theMailbox whose subject is reminderName)
            display dialog read status of message foundIt
        end tell
    end repeat
end tell

The error message suggests that whose is being applied to the mailbox and not the messages; but, I can't figure out how to rephrase that line of code.

(For context, I would like to write a script that will iterate over all Reminders and move the linked email from the "Snoozed" folder back to the inbox. First, however, I have to figure out how to get a reference to the linked email.)

9
  • 1
    Try: (every message in theMailbox whose subject is reminderName) Commented Dec 28, 2018 at 1:02
  • 1
    In that case, first message in theMailbox whose..., though this will throw an error if it can't find a message satisfying the criterion, whereas every message will return an empty list.
    – CJK
    Commented Dec 28, 2018 at 3:33
  • 1
    @d-b Yes, but it will be very slow to search a reasonably-sized mailbox for a message id. To retrieve a specific email, it would be much faster to have stored the id property of the message with or without the message id property. message id is limited to use in a whose filter clause, e.g. set M to a reference to the inbox's first message whose message id = "...". To check: if M exists then... (slow!). id gives direct access to a message (so no searching), i.e. set M to a reference to «class mssg» id 74760 in the inbox, then checking: if M exists then... is instant!
    – CJK
    Commented Nov 1, 2022 at 17:30
  • 1
    @d-b [ NB. The "small" caveat is Apple's idiotic use of the term message id as a property belonging to a message class object, which then means you can't reference message class objects by their id using regular nomenclature, which should be get message id 74760 in the inbox...spot the problem ? So you have to use raw syntax to reference the message class object, which is «class mssg», and then do it in a way so that Script Editor's parser doesn't expand it back into the term message. ]
    – CJK
    Commented Nov 1, 2022 at 17:42
  • 1
    @d-b Sorry for the late reply. Open Mail, and select a single message. Open Script Editor and execute the following: tell app "Mail" to tell the first item in (get selection) to return its properties Examine the output in detail and that will make things clearer. But to answer your question, 74760 is an integer assigned by the Mail app to a specific message (the integer itself will vary from message-to-message). It is held in the id property. The message id property, however, holds a UUID RFCC822 message ID. Raw syntax was exemplified in the same sentence I used the term.
    – CJK
    Commented Nov 10, 2022 at 7:37

1 Answer 1

1

I've been able to search mail by subject using the AppleScript below:

tell application "Mail"
    open (every message of inbox whose subject contains "KeyPhrase")
end tell

I have a similar question here.

2
  • 1
    Could you edit this to be clearer which portion is answering the OP's original question? By including a tangential question of your own you may run the risk of it getting flagged as 'not an answer'. It would be better raised as a separate question with reference back to this question / answer. Commented Jul 5 at 22:59
  • 1
    Thank you Andy, I will edit it accordingly. I've used forums where people want similar question in the same post so that it doesn't clutter things up too much. I just did the 2-minute site tour and I see that this is a bit different. Thanks for letting me know 🙂.
    – Izaac Post
    Commented Jul 6 at 20:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .