Search code examples
outlookapplescript

Set the default Microsoft Outlook for Mac signature with Applescript


I've written a script that grabs information from Active Directory and creates a new Signature in Microsoft Outlook for Mac.

I use the following code to create the signature(I will leave the other code out, as it isn't really relevant):

tell application "Microsoft Outlook"

    make new signature with properties {name:strName, content:contentHTML, plain text content:"", include in random:false}

end tell

Where strName is the name of the signature I get from elsewhere and contentHTML is the actual signature in HTML that I build elsewhere.

Adding this signature to Microsoft Outlook is working perfectly, but I can't see how to set the signature that I created to the default signature for the current account. I have done quite a lot of research that hasn't helped at all, and I've poked around the dictionary as well.


Solution

  • This can be done with AppleScript. There is nothing in the Outlook 2011 dictionary to specifically do this, so instead this can be done by scripting the UI elements, which admittedly is rather clunky.

    Signatures are set on a per-account basis, so you need to provide the name of the account to this script as well as the name of the signature you want to set for that account.

    setDefaultSignature to "strName" for "Gmail"
    
    on setDefaultSignature to mySignature for accountName
      tell application "Microsoft Outlook" to activate
      tell application "System Events"
        -- turn on UI automation - may throw a permissions dialog
        if UI elements enabled is false then set UI elements enabled to true
    
        click menu item "Preferences..." of menu 1 of menu bar item "Outlook" of menu bar 1 of application process "Outlook"
        click item 1 of (buttons of window "Outlook Preferences" of application process "Outlook" whose description is "Signatures")
        click button "Default Signatures..." of window "Signatures" of application process "Outlook"
    
        repeat with thisRow in rows of table 1 of scroll area 1 of sheet 1 of window "Signatures" of application process "Outlook"
          if value of text field of thisRow as string is accountName then
            click pop up button 1 of thisRow
            click menu item mySignature of menu 1 of pop up button 1 of thisRow
            click button "OK" of sheet 1 of window "Signatures" of application process "Outlook"
            click item 1 of (buttons of window "Signatures" of application process "Outlook" whose description is "close button")
            exit repeat
          end if
        end repeat
      end tell
    end setDefaultSignature