AppleScript for Mail

Is it possible to make an applescript that will check for unread messages through the whole mailbox, then tell me how many there are, then say who each unread mail is from. Ask me to have it read the subject then open, mark as read, or leave for later?

just like to add in my script based on the above
i've been looking for something similar to this for a while and tried allsorts of snipets from many of the posts so heres my attempt
this actually uses the mailbox or folder that is selected in mail.app so you dont have to explicitly tell it which folder you want to use (had that part of the code from another post somewhere so credit to them not me
display alert "Please note!" message "This script may take a while if you have a lot of mailboxes selected. Do you want to continue?" buttons {"Cancel", "Yes"} cancel button "Cancel"
tell application "Mail"
          tell front message viewer
                    set theSelectedMailboxes to selected mailboxes
                    repeat with a from 1 to length of theSelectedMailboxes
                              set theCurrentMailbox to item a of theSelectedMailboxes
  processMailbox(theCurrentMailbox) of me
                    end repeat
          end tell
end tell
on processMailbox(theMailbox)
          tell application "Mail"
                    set unread_messages to (messages of theMailbox whose read status is false)
                    set unread_count to count unread_messages
  --display dialog "Total unread messages: " & unread_count
                    repeat with each_message in unread_messages
                              set the_subject to subject of each_message
                              set the_sender to sender of each_message
                              set the_action to choose from list {"Open", "Mark as read", "Keep for later", "Delete"} default items {} with prompt "Subject: " & the_subject & return & return & "Sender: " & the_sender & return & return & "What do you want to do with this message?" & return with title "Unread message" without multiple selections allowed
                              if the_action is false then return
                              if item 1 of the_action is "Open" then open each_message
                              if item 1 of the_action is "Mark as read" then set read status of each_message to true
                              if item 1 of the_action is "Delete" then
                                        set read status of each_message to true
                                        set junk mail status of each_message to true
                              end if
                    end repeat
          end tell
          tell application "AppleScript Editor"
  activate
                    display dialog "Do you want to delete the email?" buttons {"yes", "no"} default button 2
                    if result = {button returned:"yes"} then
                              tell application "Mail"
  # deletes every message that is Read and marked as Junk.
  # Any that are not read AND junk will remain.
                                        delete (every message of theMailbox whose junk mail status is true and read status is true)
                              end tell
                    else
                              if result = {button returned:"no"} then
                              end if
                    end if
          end tell
end processMailbox

Similar Messages

  • Applescript for Mail Question

    Is it possible to create an Applescript for Mail, that, when an email with a specific subject line arrives in my inbox, an attached photograph would get sent immediately in response to the person who sent the email.  The subject lines would vary (e.g., people_1, or, places_16).  Every subject line would have a corresponding photo located somewhere on my hard drive (there would be hundreds of possibilities - the catalogue would be concentrated in one place).  So, for example, when "places_21" hits my Mail in box, an email would be generated in response, automatically, with a photo (places_21) attached.  Thank you!  Marcos

    Actually, it's not that hard to do, it's just that there are a lot of use cases, and a lot of caveats to consider.
    For example, do you intend/expect to work from a predefined list of image file names? Or do you want/expect to react to any filename that's requested?
    You say that the files would be 'somewhere on my hard drive'... how does the script know where to look? How does it resolve filename conflicts? What should happen if a file can't be found?
    As fane suggests, you should also consider an audit log, so you can see who asked for what, and when, along with errors that occurred on the way.
    So it's entirely do-able, just a lot of questions to answer before you get started.

  • AppleScript for mail merge

    What is AppleScript for mail merge

    AppleScript is a programming language that is tied to certain Applications that support it. Pages v5.5.1 has a scripting dictionary of the terms it will support via AppleScript. Apple's answer to not including Mail Merge functionality in Pages v5.5.1 is for individual users to roll their own, download an example and adapt it, or use another application with built-in Mail merge features.
    See my previous post to you.

  • Applescript for mail error can't get message id

    I have an applescript (found on internet) for Mail 4.5 that deletes messages in "on my mac" mailbox "Deleted Inbox" that are older than 120 days. The script runs from an iCal daily event. I drag messages from Inbox to "Deleted Inbox" when I don't want the message in Inbox, but I want to save the message for 120 days before deleting it. The script sometimes runs successfully, and sometimes gives error "Mail got an error: Can't get message id xxx of mailbox "Deleted Inbox"". Then in Mail, I "Mailbox / Rebuild" that mailbox, and run the script manually without error. How can I avoid that error? The script is essentially:
    tell application "Mail"
              set OldStuff to every message of mailbox TheBox where date received of it < OldDate
              repeat with ThisMessage in OldStuff
        delete ThisMessage
              end repeat
    end tell
    Thanks for any help.
    Ken

    How can I avoid that error?
    I don't actually know how you can avoid that error. However, maybe you can use a workaround and have the script rebuild the mailboxes automatically when such an error occurs. Try the following code:
    tell application "Mail"
      try
        set OldStuff to every message of mailbox TheBox where date received of it < OldDate
      on error
        activate
        tell application "System Events" to click menu item "Rebuild" of menu 1 of menu bar item "Mailbox" of menu bar 1 of process "Mail"
        delay 1 -- adjust if necessary
        set OldStuff to every message of mailbox TheBox where date received of it < OldDate
      end try
      repeat with ThisMessage in OldStuff
        delete ThisMessage
      end repeat
    end tell
    Since the above script uses GUI Scripting, you must enable the Accessibility Frameworks by clicking the checkbox labeled "Enable access for assistive devices" in the Universal Access System Preference pane.
    Message was edited by: Pierre L.

  • Export mail message as PDF Applescript for mail rules

    I have looked everywhere for this and found nothing. I don't have 40 hours to learn applescript myself but from the little I do know this should be possible. Probably easy.
    I want to created an applescript to run as a mail rule. I want the script to export the message as a PDF and move that PDF to a specified folder. Hazel can take it from there.
    for instance:
    Fedex sends me a email message that my shipment has been delivered.
    mail rule kicks in and runs the script
    script creates a PDF of the message
    message is moved to "fedex delivered" folder

    I found a workaround, though there must be something neater.
    I added this before my script
    tell application "System Events"
              tell process "Mail"
                        tell menu bar 1
                                  tell menu bar item "View"
                                            tell menu "View"
                                                      tell menu item "Sort By"
                                                                tell menu "Sort By"
                                                                          click menu item "Subject"
                                                                end tell
                                                      end tell
                                            end tell
                                  end tell
                        end tell
              end tell
    end tell
    then the same except "Date" instead of "Subject".
    My script does what i want now.

  • Writing an AppleScript for Mail

    I have never written an AppleScript before, so I don't know if this is even possible. But, here goes...
    I manually process my junk mail by opening each message, clicking on "Long Headers," and forwarding to [email protected] Is it possible to write a script that would allow this process to be automated?
    Thanks.

    You can get the header text with this Applescript:
    Click here to launch Script Editor.
    tell application "Mail"
    the selection
    properties of item 1 of the result
    set thisHeader to all headers of the result
    end tell
    There is a sample Applescript included with Mail.app called "Create New Message.scpt", it should contain snippets to get you going.
    Or Google and there will be plenty of simpler ones. Truth be told, the Apple sample scripts are about the worst for learning from.

  • Applescript for mail.app won't send attachment

    I have an applescript to send a mail attachment but the mail sends without the attachment? Anyone know why?
    set theContent to (current date) as string
    set theSubject to "excel file"
    set theTarget to "someone @someplace.com"
    set theSender to "admin"
    set theName to "some name"
    set theAttachment to "mcp0_HD:Users:bill:Site:logs:deviceLogs.xls"
    tell application "Mail"
    set newMessage to make new outgoing message with properties {subject:theSubject, content:theContent & return & return}
    tell newMessage
    make new to recipient at end of to recipients with properties {visible:true, name:theName, address:theTarget}
    set sender to theSender
    tell content
    make new attachment with properties {file name:theAttachment} at after the last paragraph
    send newMessage
    end tell
    end tell
    end tell

    If you are still having problems, then there is something wrong with your file that you are trying to attach, or your declaration of this variable:
    set theAttachment to "mcp0_HD:Users:bill:Site:logs:deviceLogs.xls"
    Examine that very carefully. It is also a good idea to use the *path to* nomenclature (see the Dictionary for *+Standard Additions+* for more details) to access a file in cases like these. For example, here is the pertinent portion of a script that I have used since the Panther days to do precisely what you are trying to do:
    set pay_Path to ((path to documents folder as text) & "Cathouse:Payroll:Paysheets:Today.xls")
    tell application "Mail"
    set b to make new outgoing message with properties {sender:"[email protected]", subject:("Payroll")}
    set content of b to "Hello:
    Here is the payroll ending today: " & return & return
    tell b's content
    make new attachment with properties {file name:pay_Path as alias} at after the last paragraph
    end tell
    tell b to make new to recipient with properties {address:"[email protected]"}
    send b
    end tell
    I use this every other week to report my payroll to my accountant. It has never failed, and I am using Leopard (10.5.7).
    Good luck,

  • Applescript for mail rule

    I found this on Mactech. All I did was add the line "make new outgoing........", well, it does NOT work. What do you have to do to make Mail trigger an applescript. All I want to do is when I an email reminder, to just make a new email message with Happy Birthday in the Subject. I'll click the stationary and add a birthday background. Any help would be appreciated.
    using terms from application "Mail"
    on perform mail action with messages theSelectedMessages for rule theRule
    repeat with a from 1 to count theSelectedMessages
    make new outgoing message with properties {visible:true, subject:"Happy Birthday", content:"Happy Birthday "}
    end repeat
    end perform mail action with messages
    end using terms from

    sorry but your suggestion did NOT work. Here is what I wrote and it did NOT make a difference the script will NOT generate an outgoing message.
    using terms from application "Mail"
    on perform mail action with messages newMessages
    repeat with newMessage in newMessages
    tell application "Mail"
    set senderName to (extract name from sender of newMessage)
    say "You've just received an email from " & senderName
    say (subject of newMessage) as string
    if (subject of newMessage) contains "birthday" then
    tell application "Mail"
    activate
    make new outgoing message with properties {visible:true, subject:"Happy Birthday", content:"Happy Birthday "}
    end tell
    end if
    end tell
    end repeat
    What I was able to do was embed a line of code to call another script that does successfully create an out going message. Here is the successful script.
    using terms from application "Mail"
    on perform mail action with messages newMessages
    repeat with newMessage in newMessages
    tell application "Mail"
    set senderName to (extract name from sender of newMessage)
    say "You've just received an email from " & senderName
    say (subject of newMessage) as string
    end tell
    end repeat
    tell application "Finder"
    open "Macintosh HD:Users:Shawn:Documents:Applescripts:Mail scripts:make outgoing email happy birthday5.app"
    end tell
    end perform mail action with messages
    end using terms from
    here is the make birthday5 app
    tell application "Mail"
    activate
    make new outgoing message with properties {visible:true, subject:"Happy Birthday", content:"Happy Birthday "}
    end tell
    Now when I get my birthday reminder, according to the "Rule" set up in preferences, if the subject contains "birthday" the script will read the name and subject and generate an outgoing message with Happy Birthday in the Subject and Body.....all I have to do is add some birthday stationary, type in a sappy line and viola, done. See why I love Macs so much.

  • AppleScript for mail merge with Excel data?

    Is there any approach for an AppleScript that lets you use Excel data as source for some kind of mail merge operations? If not, would anyone here be interested if someone (I?) would take a closer look at some kind of a (AppleScript Studio) solution for this?

    I'm not sure I get the question.
    If it is "could you use AppleScript to create mail merge for Pages with Excel?", the answer is, Yes. Both Pages and Excel are scriptable.
    If the question is "has anyone done it yet?", I don't know, but it could be a fun exercise.
    If the question is "would there be a market, if someone (you?) wrote and released such a script?", I cannot tell, but personally I would probably write a hack that worked only for my own needs - if I had them. The overhead with usability, testing, different address formats, languages, different Excel sheet formats, and so on, makes me guess that the investment wouldn't pay off for a generic solution. Especially considering both Pages and MS Office already have their own mail merge functions.

  • Applescript for mail to speak 'sent to'

    Hey, I use 6 mail accounts in mac mail, 4 of which are work addresses. I get a lot of mail to my 2 personal addresses which is low priority but i would like to setup speech to say the 'sent to' address when mail arrives. my computer screen is on sleep a lot of the time so if a mail arrives to a high-priority work address I would wake my screen to read immediately. Does anyone know of a script that would speak the mailbox/sent to name? thanks guys, dren.

    --- Code starts here ---
    using terms from application "Mail"
    on perform mail action with messages tMessages
    tell application "Mail"
    repeat with tMessage in tMessages
    say "Sent to " & (name of account of mailbox of tMessage)
    end repeat
    end tell
    end perform mail action with messages
    end using terms from
    --- Code ends here ---
    In 'Mail' 'Preferences' -
    01. Create a new rule.
    02. Leave the first popup button set to 'any', and select 'Every Message' from second popup button.
    03. Under 'Perform the following actions:', select 'Run AppleScript' from the only popup button.
    04. Click on the respective popup button's 'Choose...' button and navigate to where the above code is saved (as an AppleScript script file).

  • Applescript for Mail Simple Problem

    I am a professor, and I am looking to write several simple scripts for Apple's Mail client.  These scripts all involve replying to students (vs. replying to colleagues, or others).
    The first script I am looking for would create a new blank message and set the signature of that message to a predefined sig called "Teaching".
    What I have so far is:
    tell application "Mail"
        activate
        set theMsg to make new outgoing message with properties {visible:true}
        set message signature of theMsg to "Teaching"
    end tell
    This script hangs at "activate", resulting in a timeout error (why???).  If I delete the activate command, the new message is created, and then Mail times out.
    Any suggestions appreciated.
    Warmly,
    Professor E

    Hi,
    You must specify the signature like this :
    set message signature of theMsg to signature "Teaching"

  • Applescript for Mail 2.x

    Hi,
    Does anyone have a script that will save the attachments (from a folder called Requisition in Mail) in a folder located on the desktop? Have been trying but to no avail!
    Thanks and appreciate any help in this matter.

    set source_Folder to "Requisition" -- Name of source folder.
    set destination_Folder to "From Requisition" -- Name of destination folder.
    try -- Optional code, to ensure existance of 'destination_Folder'
    tell application "Finder" to make new folder at (path to desktop folder from user domain) with properties {name:destination_Folder}
    end try
    -- Code to move contents from 'source_Folder' to 'destination_Folder'.
    tell application "Finder" to move items of folder (((path to library folder from user domain) as string) & "Mail:" & source_Folder) to folder (((path to desktop folder from user domain) as string) & destination_Folder)
    ... or, hard-coded as, ...
    try -- Optional code, to ensure existance of 'destination_Folder'
    tell application "Finder" to make new folder at (path to desktop folder from user domain) with properties {name:"From Requisition"}
    end try
    -- Code to move contents from 'source_Folder' to 'destination_Folder'.
    tell application "Finder" to move items of folder (((path to library folder from user domain) as string) & "Mail:Requisition") to folder (((path to desktop folder from user domain) as string) & "From Requisition")
    ... Or, if a sub-set of files of 'Requisition' is desired, ...
    set source_Folder to "Requisition" -- Name of source folder.
    set destination_Folder to "From Requisition" -- Name of destination folder.
    set sourceFolderPath to (((path to library folder from user domain) as string) & "Mail:" & source_Folder & ":")
    try -- Optional code, to ensure existance of 'destination_Folder'
    tell application "Finder" to make new folder at (path to desktop folder from user domain) with properties {name:destination_Folder}
    end try
    try
    set tList to list folder sourceFolderPath without invisibles -- Obtain list of 'source_Folder's contents.
    -- Present 'tList' in 'choose from list' window, and accept single or multiples selections, or the 'Cancel' action.
    set selected_Files to choose from list tList with prompt "select File(s):" with multiple selections allowed without empty selection allowed
    repeat with i in selected_Files -- Cycle through the selected files.
    -- Move selected files from 'source_Folder' to 'destination_Folder'.
    tell application "Finder" to move file ((sourceFolderPath & i) as alias) to folder (((path to desktop folder from user domain) as string) & destination_Folder)
    end repeat
    end try
      Mac OS X (10.4.4)  

  • Using AppleScript in Mail Rules

    I can't figure out how to edit an AppleScript for Mail to perform the following:
    1) After moving a filtered incoming message to a prescribed folder (can do this easily), open the message in a new window (or go to the folder and show message).
    2) Before sending a message, specify a folder to store the outgoing message. This could be done via bcc in Eudora, and can be done separately in Entourage, but I don't know enough to figure out how to do it in Mail.
    Thanks for any suggestions.

    gauthier,
    1) After moving a filtered incoming message to a prescribed folder (can do this easily), open the message in a new window (or go to the folder and show message).
    You might want to take a look at the Open Message and Open Mailbox scripts available from Mail Scripts - they should do exactly what you are looking for:
    http://homepage.mac.com/aamann/
    2) Before sending a message, specify a folder to store the outgoing message. [...]
    This is not possible in Mail since you cannot hook into the sending action to perform actions before sending (and rules per se don't apply to outgoing messages). However, you might check out the Filter Sent Messages script from the above referenced Mail Scripts as somewhat of a work-around.
    Andreas

  • Documentation on applescript for printing in mail

    Is there any simple way to tell mail to print a message using applescript
    Something like this (setup to run with a rule):
    using terms from application "Mail"
              on perform mail action with messages theSelectedMessages for rule theRule
                             repeat with theMessage from 1 to count theSelectedMessages
                     -- Process the current message
                     display dialog "test" buttons {"OK"} default button 1
                     print theMessage without dialog
                             end repeat
              end perform mail action with messages
    end using terms from
    The script has no isses trigering from rules set in mail but I can't for the life of me figure out the right context to get the "print" command to work or even find any documentation on it for mail.
    I have tried tons of diffrent varents of what to put after the "print" command but have yet to get it to do anything.
    I know that you can save all the contense of an email into a text file then use textedit to print and I have had no issue getting that working but a lot of the emaills I need printed now are formated with graphics with html so all you get is a garbage text file to print.
    Also messed with like:
    keypress "p" using command down
    To try and manuly get it to print from mail but still no luck.
    I have tried to get this working on 10.6, 10.7, and 10.8
    Ideally I want this to work on 10.7 not sure how much mail has changed between versions when it comes to applescript.
    Anyone have any insite for doing this or am I just crazy thinking this should be a simple applescript?

    Yes getting the script to auto save out as a PDF would also be an option but as with the print command I have yet to find a way to get it to save out as a full PDF ony ways to save out the text and then have it made into a PDF.
    http://macscripter.net/viewtopic.php?id=25865
    This is the script I have gotten working but it only really extracts text.
    I know it can print attachments properly but I'm looking to print the body of an email that is formated in html.
    Notes
    Please note, that pergamail currently doesn't support fancy HTML eMail very good. You won't see any pictures or formatted text in the printouts from pergamail. Maybe this can be overcome in the future by parsing the raw source of the eMail messages with a Python/Ruby/Perl script, then extracting and saving the HTML part, and finally printing it with the «lpr»-command. Maybe.
    Future
    Currently pergamail creates text files from incoming eMail messages, but it would be much better to directly create HTML or PDF documents to get more control over the print layout. previmail alreday uses an advanced HTML template and Colendarshows hot to create a PDF on the fly, so I might just need to invest another weekend to write an improved version of pergamail
    As far as im aware it was never really updated to handle html formating properly.
    I also played around with previmail script and it does extract the text better out of an html formated email but it still only gets the text.
    I am looking for a more 1:1 way of printing an email.

  • Speak Sender Name Only AppleScript in Mail No Longer Working In Mountain Lion

    Hi all, the title pretty much says it all.
    I have a very simple AppleScript that I use as a rule in Apple Mail everytime new email comes in.
    This AppleScript will speak only the sender's name. That's all I want it to say.
    I there are two emails, it will say, "Mail From John Smith" followed closely by "Mail From Johnny Appletree", etc.
    This works great in Lion as a Mail rule.
    I have properly placed this AppleScript in the correct path for Mail scripts.
    It's located in:
    (user name)/Library/Application Scripts/com.apple.mail
    I've popped in the sample script provided by Apple into this same path that speaks the total number of selected emails, the name and the subject, etc. That works fine, so I'm positive the path is correct. But that script says a bunch of items I don't need. I just need a simple script and I'm guessing somehow the conversion from Lion to Mountain Lion doesn't work for some reason with this script.
    Anyone know how to modify this supposedly simple script to make it work in Mountain Lion?
    Any help is most appreciated, thanks!
    Here is the script I had been using in Lion as a Mail rule without any issues before Mountain Lion:
    on perform_mail_action(info)
    -- see if iTunes is playing
              set itunes_playing to false
              tell application "System Events"
                        if (get name of every process) does not contain "iTunes" then
                                  set itunes_playing to false
                        else
                                  tell application "iTunes"
                                            if player state is playing then set itunes_playing to true
                                  end tell
                        end if
              end tell
    -- pause iTunes if needed
              if itunes_playing is true then tell application "iTunes" to pause
              tell application "Mail"
                        set the_messages to |SelectedMessages| of info
                        repeat with a_message in the_messages
                                  set the_sender to extract name from sender of a_message
                                  if (junk mail status of a_message) is not true then
                                            say "Mail from " & the_sender
                                  end if
                        end repeat
              end tell
    -- resume iTunes if it was playing before
              if itunes_playing is true then tell application "iTunes" to play
    end perform_mail_action

    Hi,
    This script should work, try it :
    using terms from application "Mail"
          on perform mail action with messages theselectedMessages
                tell application "iTunes" -- this doesn launch Tunes if iTunes is not running
                      set itunes_playing to running and player state is playing -- see if iTunes is playing
                      if itunes_playing then pause -- pause iTunes if needed
                end tell
                tell application "Mail"
                      repeat with a_message in theselectedMessages
                            set the_sender to extract name from sender of a_message
                            if (junk mail status of a_message) is not true then
                                  tell me to say "Mail from " & the_sender
                            end if
                      end repeat
                end tell
                -- resume iTunes if it was playing before
                if itunes_playing then tell application "iTunes" to play
          end perform mail action with messages
    end using terms from

Maybe you are looking for

  • HT5552 How can I use an iTunes gift card to buy tv shows on my iPod touch?

    Or do I need to buy the tv shows on a computer, with an iTunes gift card? Is it possible to buy the shows on my iPod touch? Thank you.

  • Is it safe to use my charger?

    Happy New Year, Can I use my MacBook Air's 45w charger in Kuwait with my old white Macbook which used to run with a 60w charger? Any advice would be greatly appreciated. Thanks everyone.

  • Trouble adding epub and pdf files to Kobo Glo

    Hi there, I've recently downloaded some epub and pdf ebooks that I want to add to my kobo glo.  I'm using a macbook pro computer with OS 10.7.  I've searched online forums that tell me that I can just drag and drop pdf and epub files on to the KOBOer

  • Line breaks within contents of floating fields

    Hello everyone, I have the following question regarding the usage of floating fields and line breaks: I have a text that incorporates a companies name using a floating field. If the name of the company is too large, a line break is forced automatical

  • BIW - SAP

    Hi ,   I need to pick a file placed in the Application Server(AL11) of BI box. Can anubody let me know on how to establish the connection. The SLD config is done, all that i need is the Parameters that i need to define in the File adapter of XI and w