Different category behavior in GUI vs. AppleScript

applescriptms office

I'm seeing some weird categorization behavior on Outlook 2016 on El Capitan.

In the GUI's Category list, there's a category called, say, "Category1", with the color red. When I use the GUI, that category gets applied correctly.

But when I use AppleScript to apply the "Category1" label, a different color is applied, and there's no checkmark next to "Category1" in the GUI. It's like there are two categories with the same name, and AppleScript and the GUI are pointing to different ones.

Has anyone else seen this, or have a fix?

Thanks.

Updated: Here's a code snippet showing how I'm using AppleScript. Also, note that many of my categories are imported from a Windows PST file.

tell application "Microsoft Outlook"

-- get the currently selected message or messages
    set selectedMessages to current messages

    -- if there are no messages selected, warn the user and then quit
    if selectedMessages is {} then
        display dialog "Please select a message first and then run this script." with icon 1
        return
    end if

    repeat with theMessage in selectedMessages
        set categoryList to get categories of theMessage
        set cleanCategoryList to {}
        set wasCategoryRemoved to 0
        repeat with theCategory in categoryList
            if name of theCategory is "Category1" then
                set wasCategoryRemoved to 1
            else
                set end of cleanCategoryList to theCategory
            end if
        end repeat
        if wasCategoryRemoved is 0 then
            set end of cleanCategoryList to category "Category1"
        end if
        set categories of theMessage to cleanCategoryList
    end repeat

end tell

Best Answer

I had the exact same issue. I was able to resolve by applying the exact category id I wanted like:

set end of theList to category id 33

Rather than like:

set end of theList to category "Category1"

Here is how I got the category ids. I selected a message in Outlook that had only the category I wanted and then ran this script manually from Script Editor:

tell application "Microsoft Outlook"

    set msgSet to current messages
    if msgSet = {} then
        error "No messages selected. Select at least one message."
        error -128
    end if

    repeat with aMessage in msgSet
        set theList to categories of aMessage
        return theList
    end repeat

end tell

I then used the returned category id in the code below to set messages to this category in the future (I cobbled this together from many sources online over time, so unfortunately it's not easy for me to give credit to the right folks):

tell application "Microsoft Outlook"

    -- Workaround for Outlook 2016 Reminders window bug, part 1
    set windowClosed to false
    if "Reminder" is in (name of the front window) then
        set windowClosed to true
        close front window
    end if

    set msgSet to current messages
    if msgSet = {} then
        error "No messages selected. Select at least one message."
        error -128
    end if

    repeat with aMessage in msgSet          
        set theList to categories of aMessage
        set end of theList to category id 33 -- CHANGE THIS TO THE CATEGORY ID RETURNED IN THE PREVIOUS SCRIPT
        set categories of aMessage to theList
    end repeat

    -- Workaround for Outlook 2016 Reminders window bug, part 2
    if windowClosed is true then
        tell application "System Events" to keystroke "9" using command down
    end if

end tell

I hope that helps someone else looking for a solution to adding a category to selected Outlook emails with AppleScript.

A great improvement would be removing the first manual step of getting the desired category id, and instead allowing setting category via the textual name of the category, looping through all existing categories in Outlook until you find it, and then applying that id to the selected emails. I'd be grateful if anyone improved my version this way and shared it.