Applescript to extract subject line

1k Views Asked by At

Total n00b here, definitely not a programmer. Would love some assistance with an applescript. I'm basically trying to extract a subject line from emails in a particular folder underneath my inbox. I need it to yank out the subject line, look for some numbers (ex. 123456) and pull out the last 4 digits. Then put that into a text file. Below is what I have so far, but it's not working. I'm not getting any output at all. Any guidance would be greatly appreciated!

tell application "Microsoft Outlook"
    set theAccount to exchange account “my account"
    set topFolder to folder "Inbox"
    set subFolder to folder “Stuff"
    set theMessages to messages of subFolder
    set folderPath to ((path to home folder from user domain as string) & “emails")
    repeat with aMessage in theMessages
        my SetSubject(subject of aMessage)
    end repeat
end tell



on SetSubject(theSubject)
    tell application "Microsoft Outlook"
        try
            save theSubject in ((path to home folder from user domain as string) & “emails" & “numbers.txt" as string)
        end try
    end tell
end SetSubject
end
1

There are 1 best solutions below

1
vadian On BEST ANSWER

I don't know what your criteria are to filter the subjects.

This is an example to write the last 4 characters of all subjects of the messages in the specified mailbox into a file numbers.txt in folder emails in the home folder, one subject per line.

If an error occurs – for example the number of characters in the subject is less than 4 – the file is closed reliably and the script aborts.

set numbersFile to (path to home folder as text) & "emails:numbers.txt"

tell application "Microsoft Outlook"
    set theSubjects to subject of messages of folder "Stuff" of folder "Inbox" of exchange account "my account"
end tell

try
    set fileDescriptor to open for access file numbersFile with write permission
    repeat with aSubject in theSubjects
        write (text -4 thru -1 of aSubject & return) to fileDescriptor starting at eof
    end repeat
    close access fileDescriptor
on error
    try
        close access file numbersFile
    end try
end try