Changes to Outlook messages not saved


I am forced to use Outlook for Mac connected to corporate Exchange at work and I have pieced together a small AppleScript to alter subject lines of incoming email messages from our monitoring system. By default they are of an ungodly length and not human readable. I am attempting to fix that.

For some strange reason everything works great when I send test messages myself. When the real messages from the monitoring system come in, everything is reverted back! Message arrives into my mailbox with the subject line exacly how I want it, but within 3-5 seconds it reverts back to the original subject! If I manually run the rule on the messages, everything is saved correctly too, so I suspect the issue may be because a message is not completely downloaded when I change the subject and when a message finally finishes downloading, it overwrites my changes… This is the only theory that I have about this.

I do not know anything about AppleScript and have no idea where to begin troubleshooting something like this.

Would anyone please take a look and tell me if what I am attempting to do is doable?

on replaceText(find, replace, subject)
    set prevTIDs to text item delimiters of AppleScript
    set text item delimiters of AppleScript to find
    set subject to text items of subject

    set text item delimiters of AppleScript to replace
    set subject to "" & subject
    set text item delimiters of AppleScript to prevTIDs

    return subject
end replaceText

tell application "Microsoft Outlook"
    set theMessages to the current messages
end tell

repeat with theMsg in theMessages
    tell application "Microsoft Outlook"
        set mysubject to get the subject of theMsg
        if mysubject contains "[Subscription:Spectrum CTO] SPECTRUM - " then
            set mysubject to my replaceText("[Subscription:Spectrum CTO] SPECTRUM - ", "", mysubject)
            set mysubject to my replaceText("Alarm Title: ", "", mysubject)
            set mysubject to my replaceText("Severity: ", "", mysubject)
            set mysubject to my replaceText("Model Name:", "-", mysubject)
            set subject of theMsg to the mysubject as string
        end if
    end tell
end repeat

Ha! I think I fixed it by inserting a 3 second delay before it changes the subject.

Yep. That was it. delay(3) fixed it.