Looking for AppleScript help




I'm a Newbie to Mac-Forums and to AppleScript. I can't seem to find any forums dedicated AppleScript so I will post this here as a start.

While I have used a Mac at home for many years my recent programing has been in M$ VBA on my employers PC. Looking at AppleScript I can see some potential but the script editor is not as powerful the VBA editor when it comes to learning the language. I learn best by studying sample scripts but I have been unable to find a script that does anything like what I want to do.

I am trying to export a large number of e-mails from OS X Mail and save them as simple text files. I am struggling to both understand the object structure and how to reference it. Therefore the information in the Dictionary doesn't help me much.

The generic algorithm steps would go something like:
For every message in the inbox
if the message subject is like "ABC Corp:*" then
Save message as plain text "subject.txt" in "xyz" directory
move message to trash
end if
next message

Can anyone offer some support?


Mar 9, 2004
Reaction score
Miami FL
Your Mac's Specs
G4 1Ghz OS X 10.4.7
Here is an example from the Applescript applications:
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
tell application "Mail"
set theText to "This AppleScript is intended to be used as an AppleScript rule action, but is also an example of how to write scripts that act on a selection of messages or mailboxes." & return & return & "To view this script, hold down the option key and select it again from the Scripts menu."
repeat with eachMessage in theMessages
set theSubject to subject of eachMessage
-- If this is not being executed as a rule action,
-- getting the name of theRule variable will fail.
set theRuleName to name of theRule
set theText to "The rule named '" & theRuleName & "' matched this message:"
set theText to theText & return & return & "Subject: " & theSubject
display dialog theText
set theText to ""
end try
end repeat
if theText is not equal to "" then
display dialog theText buttons {"OK"} default button 1
end if
end tell
end perform mail action with messages
end using terms from
You may well be able to use this one with some modifications


witeshark said:

repeat with eachMessage in theMessages
set theSubject to subject of eachMessage

Thanks for the reply! :) I copied into the script editor and had to remove the first and last two lines for the syntax check to work :confused: however that brought me back to th problem i have been having looping through each message. "theMessages" is undefined. Yesterday I tried seting theMessages something like:

tell application "Mail"
set everyMessage to every message
repeat with eachMessage in everyMessage
set theSubject to subject of eachMessage
(*code identify and so save messages*)
end repeat
end tell

and I would get, NSCannontCreateScriptCommandError on "every Message"

How do I get a set of messages, specifically the ones in the Inbox, so I can loop through them?

Your help is appreciated.



Shop Amazon

Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.