Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Apple Mail Automation

I want to process 'Mail Internet Location' files located in a folder named 'Email' on my Desktop via this progression:
1.) Randomly Select a Mail Internet Location File
2.) Open Selection
3.) Send Email Message
4.) Move Selection To Trash
5.) Pause The Workflow for 120 Seconds
6.) Loop Workflow
Can someone please help me get started. It would be nice if these actions were available in Automator but they're not.

MacBook Pro "17 et. al., Mac OS X (10.6.2), http://slideumentary.com

Posted on Feb 14, 2010 8:56 PM

Reply
15 replies

Feb 14, 2010 10:00 PM in response to Slideumentary

Pretty much all of those actions are already in Automator, except for a random selection, which can be done with a *Run AppleScript* action (I also have an action that does this). You don't need to open the files unless you are going to append the text, passed files are attached to the Mail message.
1) *Get Specified Finder Items* (drag your Email folder here)
2) *Get Folder Contents*
3) *Run AppleScript*: -- randomly pick an item<pre> on run {input, parameters}
return some item of the input
end run</pre>
4) *Set Value of Variable* { Variable: _Original Item_ } -- save the original item for later
5) *Send Email Message* -- send with an attachment
6) *Get Value of Variable* { Variable: _Original Item_ } (Ignore Input)
7) *Move Finder Items to Trash*
8) Pause for 2 minutes
9) Loop

You will probably need some kind of test for when the folder doesn't have any contents (I have an action for that, too), and adding a filter to just get files is probably a good idea also.

Snow Leopard's Automator is basically the same as Leopard's, so you can use the Leopard Automator forum for these topics.

Feb 14, 2010 11:19 PM in response to red_menace

red_menance,

Thanks for your assistance. Will this workflow send out all the emails in a batch all at once or individually with the 2 minute pause between each message? I would prefer the later. I still haven't tested it as I cannot find item #5 on your list to Send Email Message as it is not included in the default actions list in the Mail Library. Please advise.

Feb 15, 2010 3:33 PM in response to Slideumentary

Oops, was going off of your workflow example for the Mail part. The actions should be:
5a) *New Mail Message* -- create a message with the attachment
5b) *Send Outgoing Messages*

The messages will be sent one at a time - you could create several messages, but to send in a batch you would need to send them outside of the workflow, since the Loop action needs to be the last one.

Feb 15, 2010 10:10 PM in response to red_menace

red_menace,

That's exactly what I want it to do, automate the filling in of the data to the recipient. Once a file is randomly selected and opened, it populates the fields to the recipient as all of the data (To:, Subject:, and Body) is contained in the Mail Internet Location file. If this cannot be accomplished with traditional automator actions, what are my options?

Feb 16, 2010 1:14 PM in response to red_menace

This is what I have so far...

tell application "Finder"
set random_file to some file of folder "Macintosh HD:Users..."
open selection
move selection to trash
tell application "Mail" to activate
tell application "System Events"
click menu item "Send" of menu "Message" of menu bar item "Message" of menu bar 1 of application process "Mail" of application "System Events"
end tell
end tell

The script has a bug at line 7, I don't quite understand why. Can you help me add the pause and loop too?

Feb 16, 2010 5:11 PM in response to Slideumentary

The error is because you aren't creating a message, so that particular menu item is not available (it is grayed out). GIve the following script a try:

<pre style="
font-family: Monaco, 'Courier New', Courier, monospace;
font-size: 10px;
font-weight: normal;
margin: 0px;
padding: 5px;
border: 1px solid #000000;
width: 720px; height: 340px;
color: #000000;
background-color: #DAFFB6;
overflow: auto;"
title="this text can be pasted into the AppleScript Editor">
tell application "Finder" to set theFiles to files of folder "Macintosh HD:Users..." as alias list

repeat (count theFiles) times
set randomFile to some item of theFiles

set mailtoURL to (read randomFile as text) -- get the file contents
if mailtoURL does not start with "mailto:" then -- not a valid mailto URL, so skip it
log "the file " & quoted form of (randomFile as text) & " was skipped..." -- error message or whatever, if desired
else
tell application "Mail"
activate
mailto mailtoURL -- create the message
delay 5
tell application "System Events" to keystroke "d" using {command down, shift down} -- send it
end tell

tell application "Finder" to move randomFile to the trash
if (count theFiles) is equal to 1 then exit repeat -- skip time delay for the last item
end if

set editedList to {}
repeat with X from 1 to (count theFiles) -- remove the item from the list
if item X of theFiles is not equal to randomFile then set end of editedList to item X of theFiles
end repeat
set theFiles to editedList
delay 120
end repeat
</pre>

It will loop through all of the files of the folder (at the time the script is run), skipping improper mailto URLs (I am guessing that your files contain properly formatted text, e.g. mailto:joe@example.com?cc=bob@example.com&subject=testing&body=hello%20there). I also threw a delay in there after creating the message so that you can see it (just remove the delay after any tweaks).

Feb 16, 2010 8:31 PM in response to Slideumentary

The AppleScript keeps telling me that the file was skipped.

The script is looking for text that begins with mailto: (since that is what the RFC 2368/mailto URL is supposed to start with), and skips the file if it doesn't. Some additional text processing can be done if needed - what is the exact format and type of text in the files?

The script seems a little complicated, can't we just tell the finder to open a random file then activate mail to send the said message?

That is exactly what it is doing, although the Finder isn't needed to read a file. There are a couple of additional things included that may appear to be complicated, such as only using files that appear to contain valid mailto URLs and keeping track of the original list of files, but care needs to be taken in certain obvious cases so that the script doesn't halt with an error.

Feb 16, 2010 8:59 PM in response to red_menace

File Title: MAILTO
File Extension: .mailtoloc
Get Info 'Kind': Mail Internet Location

Structure/Syntax:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>URL</key>
<string>mailto:EMAIL@EMAIL.com?subject=SUBJECT%20LINE?body=BODY</string>
</dict>
</plist>

Feb 16, 2010 10:39 PM in response to Slideumentary

OK, now it is getting complicated. The script has been modified to extract the *mailto URL* from the plist file, and to fix the separators (regular plist files do not like an unencoded "&" character, so it is replaced with "?" in the file).

<pre style="
font-family: Monaco, 'Courier New', Courier, monospace;
font-size: 10px;
font-weight: normal;
margin: 0px;
padding: 5px;
border: 1px solid #000000;
width: 720px; height: 340px;
color: #000000;
background-color: #DAFFB6;
overflow: auto;"
title="this text can be pasted into the AppleScript Editor">
tell application "Finder" to set theFiles to files of folder "Macintosh HD:Users..." as alias list

repeat (count theFiles) times
set randomFile to some item of theFiles

try
tell application "System Events" -- get the poperty list item
set mailtoURL to value of property list item "URL" of property list file (randomFile as text)
end tell

set here to (offset of "?" in mailtoURL) -- find the header separator
if here is not 0 then -- fix any extra "?" separators
set textToFix to text (here + 1) thru -1 of mailtoURL
set {tempTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "?"} -- break on old character
set {textToFix, AppleScript's text item delimiters} to {text items of textToFix, "&"}
set {textToFix, AppleScript's text item delimiters} to {textToFix as text, tempTID} -- assemble with new character
set mailtoURL to (text 1 thru here of mailtoURL) & textToFix
end if

tell application "Mail"
activate
mailto mailtoURL -- create the message
delay 5
tell application "System Events" to keystroke "d" using {command down, shift down} -- send it
end tell

tell application "Finder" to move randomFile to the trash
if (count theFiles) is equal to 1 then exit repeat -- skip time delay for the last item
on error -- oops, skip the error
log "the file " & quoted form of (randomFile as text) & " was skipped..." -- error message or whatever, if desired
end try

set editedList to {}
repeat with X from 1 to (count theFiles) -- remove the item from the list
if item X of theFiles is not equal to randomFile then set end of editedList to item X of theFiles
end repeat
set theFiles to editedList
delay 120
end repeat
</pre>

By the way, what is the application that creates these files?

Apple Mail Automation

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.