Applescript mail examples
Here are some examples of AppleScript code for interacting with Mail:
1. Send an email
tell application "Mail"
set newMessage to make new outgoing message with properties {subject:"Hello", content:"Hello, this is a test email."}
tell newMessage
make new to recipient at end with properties {name:"John Doe", address:"[email protected]"}
send
end tell
end tell
2. Get the number of unread emails
tell application "Mail"
set unreadCount to count of unread messages of mailbox "Inbox"
display dialog "You have " & unreadCount & " unread emails."
end tell
3. Move an email to a specific folder
tell application "Mail"
set theMessage to message id "1234567890" of mailbox "Inbox"
move theMessage to mailbox "Archive"
end tell
4. Search for emails with a specific subject
tell application "Mail"
set searchResults to search messages of mailbox "Inbox" with query "subject:Hello"
repeat with eachMessage in searchResults
display dialog eachMessage's subject
end repeat
end tell
5. Get the sender and subject of the latest email
tell application "Mail"
set latestMessage to last message of mailbox "Inbox"
display dialog "From: " & latestMessage's sender & ", Subject: " & latestMessage's subject
end tell
6. Delete an email
tell application "Mail"
set theMessage to message id "1234567890" of mailbox "Inbox"
delete theMessage
end tell
7. Get the attachments of an email
tell application "Mail"
set theMessage to message id "1234567890" of mailbox "Inbox"
set attachments to attachments of theMessage
repeat with eachAttachment in attachments
display dialog eachAttachment's name
end repeat
end tell
8. Forward an email
tell application "Mail"
set theMessage to message id "1234567890" of mailbox "Inbox"
set newMessage to make new outgoing message with properties {subject:"Fwd: " & theMessage's subject, content:"Fwd: " & theMessage's content}
tell newMessage
make new to recipient at end with properties {name:"John Doe", address:"[email protected]"}
attach theMessage's attachments
send
end tell
end tell
These are just a few examples of what you can do with AppleScript and Mail. You can use these scripts as a starting point and modify them to fit your specific needs.