Identifying Microsoft 365 accounts

My app Mail Archiver can import accounts from Mail and Outlook. Here are the scripts:

tell application id "com.apple.mail"
   set theAccounts to get accounts
   set AccountInfo to {}
   repeat with theAccount in theAccounts
      set theAccount to {name of theAccount & "|||" & user name of theAccount & "|||" & server name of theAccount & "|||" & port of theAccount}
      set end of AccountInfo to theAccount
   end repeat
   return AccountInfo
end tell

and

tell application "Microsoft Outlook"
   set theAccounts to get imap accounts
   set ExchangeAccounts to get exchange accounts
   set theAccounts to theAccounts & ExchangeAccounts
   set PopAccounts to get pop accounts
   set theAccounts to theAccounts & PopAccounts
   set AccountInfo to {}
   repeat with theAccount in theAccounts
      set theName to name of theAccount
      set UserName to user name of theAccount
      try
         set SMTPServer to smtp server of theAccount
         set thePort to smtp port of theAccount
      on error
         set SMTPServer to "Microsoft365"
         set thePort to "993"
      end try
      try
         set end of AccountInfo to {theName & "|||" & UserName & "|||" & SMTPServer & "|||" & thePort}
      on error number errorNumber
         --ignore
      end try
   end repeat
   return AccountInfo
end tell

Is there a better way to identify Microsoft365 accounts than with ā€œmissing valueā€ for the SMPT server?

1 Like

Not a m365 user but can you get the properties of such an account? You might find something distinctive that way.

And out of curiosity, if you are archiving messages, why collect SMTP info? Itā€™s typically related to the task of sending email.

For Mail Iā€™m getting the server name. Mail has different types of accounts. For the Microsoft 365 account Iā€™m getting ā€œunknownā€ as account type.

You are quite correct. Using smtp server in the Outlook script is a bug.

Thatā€™s a tough one. Are there any other types that generate ā€˜unknownā€™? If not, you could probably go with that. Does it have any other unique information?

For messages that arrive in that account, do the mail headers give any useful information?

So far I only the Exchange/Microsoft 365 account have the unknown type.

Script for Mail:

tell application id "com.apple.mail"
   set theAccounts to get accounts
   set AccountInfo to {}
   repeat with theAccount in theAccounts
      if account type of theAccount is unknown then
         set theAccount to {name of theAccount & "|||" & user name of theAccount & "|||Microsoft365|||993"}
      else
         set theAccount to {name of theAccount & "|||" & user name of theAccount & "|||" & server name of theAccount & "|||" & port of theAccount}
      end if
      set end of AccountInfo to theAccount
   end repeat
   return AccountInfo
end tell

Script for Outlook:

tell application "Microsoft Outlook"
   set ExchangeAccounts to get exchange accounts
   set AccountInfo to {}
   repeat with theAccount in ExchangeAccounts
      set theName to name of theAccount
      set UserName to user name of theAccount
      set theServer to "Microsoft365"
      set thePort to "993"
      try
         set end of AccountInfo to {theName & "|||" & UserName & "|||" & theServer & "|||" & thePort}
      on error number errorNumber
         --ignore
      end try
   end repeat
   set theAccounts to get imap accounts
   repeat with theAccount in theAccounts
      set theName to name of theAccount
      set UserName to user name of theAccount
      try
         set theServer to server of theAccount
         set thePort to port of theAccount
      on error
         --ignore
      end try
      try
         set end of AccountInfo to {theName & "|||" & UserName & "|||" & theServer & "|||" & thePort}
      on error number errorNumber
         --ignore
      end try
   end repeat
   return AccountInfo
end tell

The scripts are just there to make adding Imap accounts easier. Getting the header of the messages comes waayyy later in the workflow.

I believe that Microsoft Outlook 2016 doesnā€™t have support for AppleScript at this time, despite having the libraries available.

Old Outlook has AppleScript support. New Outlook has half baked AppleScript support. Accounts, for instance, donā€™t exist in New Outlook. Microsoft doesnā€™t show any interest in improving AppleScript for new Outlook.

Hi all,
Iā€™m stuck also w/ applescript for the New Outlook. My big issue is to ā€œget selected messagesā€. I want also to create a code to send file by new message in outlook.

in here, https://www.microsoft.com/en-us/microsoft-365/roadmap?featureid=88537, it seems the applescript is coming. Do you know is is already available?

tell application "Microsoft Outlook"
	try
		set theSelection to the selected objects
		if theSelection is {} then error "One or more messages must be selected."
		
		tell application id "DNtp"
			if not (exists current database) then error "No database is in use."
			set theGroup to preferred import destination
		end tell
		
		repeat with theMessage in theSelection
			try
				set theSubject to subject of theMessage
				set theSender to sender of theMessage
				set theSender to (address of theSender) as string
				set theSource to source of theMessage
				set theDateReceived to time received of theMessage
				set theDateSent to time sent of theMessage
				if theSubject is equal to "" then set theSubject to pNoSubjectString
				
				set theCategories to {}
				set theList to (category of theMessage)
				repeat with theCategory in theList
					set theCategories to theCategories & (name of theCategory)
				end repeat
				set isFlagged to true
				if todo flag of theMessage is (not flagged) then set isFlagged to false
				set isUnread to is read of theMessage
				
				tell application id "DNtp"
					set theRecord to create record with {name:theSubject & ".eml", type:unknown, creation date:theDateSent, modification date:theDateReceived, URL:theSender, source:(theSource as string)} in theGroup
					if theCategories is not {} then
						set theTags to tags of theRecord
						set theTags to theTags & theCategories
						set tags of theRecord to theTags
					end if
					if isFlagged then set state of theRecord to true
					if isUnread then set unread of theRecord to true
					perform smart rule trigger import event record theRecord
				end tell
			end try
		end repeat
	on error error_message number error_number
		if the error_number is not -128 then display alert "Outlook" message error_message as warning
	end try
end tell

Thanks

Iā€™m able to get accounts from Outlook

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

set myAccounts to {}
tell application "Microsoft Outlook"
	set myAccounts to myAccounts & exchange accounts --you can use just this line to get Exchange accounts
	set myAccounts to myAccounts & imap accounts
	set myAccounts to myAccounts & pop accounts
end tell

BWill, Can you be more specific by what you mean ā€˜Accounts, for instance, donā€™t exist in New Outlookā€™

bsilvamm, what do you mean 'My big issue is to ā€œget selected messagesā€ ā€™

For new Outlook most of the scripting dictionary looks like itā€™s available. But almost nothing works.

@ bsilvamm: which version are you on? Iā€™m on some sort of beta track. I simplified your script and the selection works fine.

tell application "Microsoft Outlook"
   try
      set theSelection to the selected objects
      if theSelection is {} then error "One or more messages must be selected."
      repeat with theMessage in theSelection
         try
            set theSubject to subject of theMessage
         end try
      end repeat
   on error error_message number error_number
      if the error_number is not -128 then display alert "Outlook" message error_message as warning
   end try
end tell

For me returns the same error:

image

Iā€™m on macOS Ventura 13.2.1 / New Outlook active and version is 16.71

Thanks

Iā€™m on Outlook 16.71 (latest)

Here is a script I wrote to test creation and sending of a reply to an email

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

set myAccounts to {}
tell application "Microsoft Outlook"
	set theSelection to selected objects
	if (count theSelection) = 0 then
		beep
		display alert "No Emails were selected!" giving up after 3
		return
	end if
	set myAddress to (sender of item 1 of theSelection) -- get From address to reply to
	set myAccount to account of item 1 of theSelection
	set myMessage to make new outgoing message with properties {account:myAccount, content:"Testing!", subject:"Test Email"}
	make to recipient of myMessage with properties {email address:myAddress}
	send myMessage
end tell

Iā€™m stupid. Sorry, I was doing 2 things at the same time. Iā€™m using Outlook 16.72 and now I canā€™t even get the simplest ā€œtell Microsoft Outlookā€ to work.

You are using the New Outlook? Iā€™ve the same version of Outlook and returns error : No Emails were selected!

Did you have any emails selected?

You need to have an email selected, as my script will send a reply to the person who sent you the email.

No email selected - no reply email sent

** EDIT ** - Hoping for a reply.

Youā€™re ahead of time. I believe 16.71 is the latest.

So nothing I can do to solve this?

No, not until Microsoft finally gets their act together.

@robertfern : no, getting the selection doesnā€™t work for new Outlook. Today I got a new update but the selection doesnā€™t do anything:

Getting any type of account has the same result - nothing.

OK,

It seems you are running Outlook in ā€˜New Outlookā€™ mode.

Whereas Iā€™m running the old interface.

Old interface works. New interface doesnā€™t

Hi @bwill
How can I track this subject to know when New Outlook interface have applescript support?

Microsoft has a roadmap for AppleScript which has an RSS feed:

https://www.microsoft.com/en-in/microsoft-365/roadmap?filters=&searchterms=apple%2Cscript

However, I donā€™t think that Microsoft understands RSS.

Other than that you can check the usual suspects: Michael Tsaiā€™s blog, TidBits or my own blog at Moth Software.

Iā€™m really knocking on wood here that this time AppleScript is really added.