Zantafio Zantafio - 1 month ago 10
AppleScript Question

AppleScript lists and the whose clause

AppleScript beginner here, and still struggling with basic syntax.

This works:

tell application "Mail"
set flagged to messages of inbox whose flagged status is true
log count of flagged
end tell


this does not work:

tell application "Mail"
set msgs to messages of inbox
set flagged to msgs whose flagged status is true
log count of flagged
end tell


Why? (I suspect it's a simple syntactic rule that is escaping me)

foo foo
Answer

When you write set msgs to messages of inbox, what you're really saying is set msgs to GET messages of inbox as AppleScript performs a get command automatically if you don't tell it otherwise, the result of which is an AppleScript list (of message references), e.g.:

{message id 123 of mailbox "Inbox" ..., message 124 of mailbox "Inbox" ...}

However, whose queries (references) only work on application objects, not on AppleScript lists, e.g.:

every item of {1, 2, 3, 4, 5} where it > 3
-- error "Can’t get {1, 2, 3, 4, 5} whose it > 3." number -1728

Try the following instead:

tell application "Mail"
    set msgs to a reference to messages of inbox
    set flagged to msgs whose flagged status is true
    log count of flagged
end tell
Comments