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
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.
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.
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.
We're almost there. When I test this workflow, Automator creates a new message in Mail with the Mail Internet File attached to the message and the email property fields empty. I need the file to be opened to populate the To:, Subject, and Body of the message--then send. Any suggestions?
Just fill in the fields in the Automator action. The only field that will accept a variable is the subject, so if you are wanting to dynamically set other Mail message fields, something other than the *New Mail Message* action will need to be used.
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?
Ah, you are talking about a *mailto URL* - an AppleScript can be used. Is this a standard encoded mailto URL that contains all of the fields, or are there additional attachment files?
Yes! =) That is exactly what it is what it is. There are no attachments to be included. In the script I still want there to be a 2 minute delay then dispose of the 'mailto' file when it is complete, then select another file until the all the files have been processed and sent.
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?
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).
The AppleScript keeps telling me that the file was skipped. 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?
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.
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?