Using AppleScript to Move Mail Messages to Trash - applescript

I've written a script that is invoked by a Mail rule. I want the script to move messages to the trash under certain conditions. It didn't work so I've commented out the test and it still didn't work. I got the code from an online tutorial. The tutorial moved the message to "Filed". The rule itself worked when I replaced the call to the script with a move to the trash. Here's what I'm testing:
using terms from application "Mail"
on perform mail action with messages these_messages for rule this_rule
tell application "Mail"
set the message_count to the count of these_messages
repeat with i from 1 to the message_count
set this_message to item i of these_messages
-- try
-- set this_content to (every character of content of this_message) as Unicode text
-- if this_content is in {"", "?"} then error
-- if "bowles" is not in this_content and "patton" is not in this_content then
set mailbox of this_message to mailbox "Trash"
-- end if
-- on error error_message
-- set this_content to "NO CONTENT"
-- end try
end repeat
end tell
end perform mail action with messages
end using terms from
The statement at issue is: set mailbox of this_message to mailbox "Trash"
The rest of the active code was from AppleScript Editor template.
Thanks,
Curt
P.S. Are there any books out there that deal with using AppleScript with Mail?

Instead of trying to "move" a message to the Trash, you should just say
delete this_message.
Mail.app will then proceed according to your account settings (Mail preferences > Accounts > (your account) > Mailbox Behaviors > Trash) and move the message to its account's Trash mailbox or else delete it immediately, depending on your settings.

I should have done a more extensive Web search before asking here. Hopefully, this answer will be useful to others. I added the first line below and modified the second one from the code above.
set theAccount to account of mailbox of this_message
set mailbox of this_message to mailbox "Trash" of theAccount

Related

Create mail with attachment using applescript and outlook

I've wrote a litte Applescript to make a new message with an attached file via outlook. But it didn't work and I don't know why. There is no error or something else. The message is created in the Draft-section of outlook, but the attachment is missing. Can somebody help me out?
Here's the Script:
set mailBody to "<span>TEST</span>"
set mailAdress to text returned of (display dialog "Mail Adress" default answer "" buttons {"OK"} default button 1) as string
set whichFile to file
tell application "Finder" to set whichFile to selection
repeat with aFile in whichFile
tell application "Microsoft Outlook"
set filename to name of aFile
set theNewMessage to make new outgoing message with properties {subject:"TEST " & filename, content:mailBody}
make new recipient at theNewMessage with properties {email address:{address:mailAdress}}
set theAttachmentFile to aFile as POSIX file
make new attachment at the end of theNewMessage with properties {file:theAttachmentFile}
open theNewMessage
end tell
end repeat
Greetings and thanks in advance
Speedster
Here is what I have been using as an alternative. To use the below, you need the desired message to be queued up already in your outbox. Or you can just borrow a few lines of the below and work it into your existing code.
set theAttachmentFile to "Macintosh HD:Users:Speedster:Documents:Attachment.docx" as alias
---replace this with the hard address of your selected file
tell application "Microsoft Outlook"
repeat with theMessage in the messages of the outbox
make new attachment at the end of theMessage with properties {file:theAttachmentFile}
end repeat
end tell

Apple Mail and Applescript

I have has this script working fine on Outlook 2011 for Mac all on a local machine.
I have now found out my "rules" will now work on Apple mail after all these years.
The last part of the rule is to run this script which then sends an HTML email back to the sender.
If I change the tell application "Microsoft Outlook" to tell the application "Mail", I keep getting Syntax Errors and it highlights the "mail folder" telling me it is wrong.
I am stumped.
I am using Exchange with the Mail app. Not sure if that may be the issue?
tell application "Microsoft Outlook"
set replyToMessage to message 1 of mail folder "•HOT FOLDER•"
if (replyToMessage is "") then
log ("NOTHING SELECTED!")
return
end if
set replyMessageSubj to subject of replyToMessage
set replyMessage to reply to replyToMessage without opening window
set contentHTML to "Vault:Users:vault:Documents:VAULT:Digital_Subscription_Reply:Digital_issue22.html" as alias
set contentHTML to read contentHTML
set the content of replyMessage to contentHTML
set the subject of replyMessage to "Vault Digital Issue - Issue 22"
send replyMessage
end tell
Every scriptable application has its own scripting dictionary with its own unique terminology. We can't just change tell application "Microsoft Outlook" to tell application "Mail" and expect it to work, because Mail and Outlook use different keywords for their equivalent objects. I don't have outlook installed on my machine, so I can't compile or run the original script, but I've modified it as best I can to use Mail terminology. No guarantee this will work, but it will compile and run so you can (at least) test it.
tell application "Mail"
set replyToMessage to message 1 of mailbox "•HOT FOLDER•"
if (replyToMessage is "") then
log ("NOTHING SELECTED!")
return
end if
set replyMessageSubj to subject of replyToMessage
set replyMessage to reply replyToMessage without opening window
set contentHTML to "Vault:Users:vault:Documents:VAULT:Digital_Subscription_Reply:Digital_issue22.html" as alias
set contentHTML to read contentHTML
set the content of replyMessage to contentHTML
set the subject of replyMessage to "Vault Digital Issue - Issue 22"
send replyMessage
end tell
When you're in Script Editor, select "Open Dictionary..." from the File menu and scroll down to open the Mail scripting dictionary. That will give you all the relevant terminology for scripting Mail.app.

Applescript for Outlook v15 - Recipients won't populate

I have copied some scripts on the site and the result is that Outlook creates a new forwarded message but the recipient is blank. Have tried multiple forms but always the blank recipient. It's a script I will only run for messages I manually select, so I can't use a simple forwarding rule in the app.
Any ideas?
tell application "Microsoft Outlook"
repeat with eachSelectedMessage in selectedMessages
set theForwardedMessage to forward eachSelectedMessage with opening window
make new recipient at theForwardedMessage with properties {email address:{address:"recipient#somewhere.com", name:"Lumpkin Skinbark"}}
send theForwardedMessage
end repeat
end tell
New information: It turns out the recipient is being populated correctly and put in my Outbox, but not being sent. In addition, a new window pops up with the same forwarded message and a blank recipient. That new window was the one I saw that led me to believe the recipient was not populating.
My workaround for now is to periodically hit the send/receive button in Outlook to send the messages sitting in the Outbox (I don't know why they don't just get sent automatically like everything else). Then I added:
close window 1
to close the open window -- don't know why it's created and don't want it, but might as well just close it and move on.
So I do have it working, but it's not elegant.
I finally figured out how to make this work: DON'T Open the forwarded email window until AFTER you've set the email properties, especially the Recipients.
Here's my test script:
set newSubjectStr to "TEST -- DON'T OPEN at first."
set recipientEmail to "somebody#InvalidDomainName.com"
tell application "Microsoft Outlook"
set selMsg to current messages
set oMsg to item 1 of selMsg
set subjectStr to subject of oMsg
set oFwdMsg to ¬
forward oMsg without opening window ## This is KEY. Don't Open Window
make new recipient at oFwdMsg ¬
with properties {email address:{address:recipientEmail}}
set subject of oFwdMsg to newSubjectStr
--- NOW OPEN FWD EMAIL WINDOW ---
open oFwdMsg
end tell
--- SEND NOW IF NO MORE CHANGES ---
-- Works fine if I manually send, or use keyboard shortcut --
-- Send and Close Window ---
delay 0.2
tell application "Microsoft Outlook" to activate
tell application "System Events"
key code 36 using {command down} -- CMD-RETURN
end tell
--- BELOW SEND BY SCRIPT LEAVES MSG IN OUTBOX ---
--send oFwdMsg
--close window 1
--sync folder "Outbox"
NOTE to Moderators: Is there a way to get syntax highlight for AppleScript?

how to get the subject of my outlook email to mac clipboard through an applescript?

I am new to applescript. I am trying to get the subject of the selected outlook (2011) email with the following applescript.
tell application "Microsoft Outlook"
set theMessage to the current messages
get the subject of theMessage
end tell
But I am getting the following error message.
Can’t get subject of {incoming message id 392990 of application "Microsoft Outlook"}
Can someone please help me?
I am completely new to AppleScript ... and am wanting a script to extract a list of both the the 'subject line' and 'date received' from a specific batch of emails in Outlook 2011.
Is this possible.
The script above works great for extracting the subject line from one highlighted email.
Many thanks
Simon
The reason this does not work is that set theMessage to the current messages returns a list. Notice the { }
tell application "Microsoft Outlook"
set theMessage to first item of (get current messages)
set theSubject to the subject of theMessage
end tell
set the clipboard to theSubject

Getting Mac Hazel to e-mail .lnk, doc, and .xls files to my evernote account

i'm trying to automate and simplify my life. I'm new to hazel and apple scripts..
I have a lot of documents and i pull a lot of things of the web. i simply drag the link to my desktop where it makes a .lnk file.
i want to create a hazel + apple script that will look for these .lnk files and send an email with the link file attached to my evernote email address.
Any suggestions would be appreciated.
In Hazel Create a new rule.
If all ..
1, Extension - is - ink
Do the following:
2, Run AppleScript - embedded script.
Click the info icon to edit the script. Paste this script into it. With the correct email addresses, subject, content set.
set theAttachment1 to (POSIX path of theFile)
set subject_ to "subject"
set the_content to "the_content"
tell application "Mail"
set newMessage to make new outgoing message with properties {subject:subject_, content:the_content & return & return}
tell newMessage
set visible to false
set sender to "my1#mac.com"
make new to recipient at end of to recipients with properties {address:"their#mac.com"}
make new attachment with properties {file name:theAttachment1} at after the last paragraph
(* change save to send to send*)
send --<<<<---------------- change save to send to send or send to save to save to drafts
(* change save to send to send*)
end tell
end tell
3, Add a second action to move the file to a folder. If not you may keep getting the same files sent over and over again.

Resources