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?
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.
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.
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
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
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.