Error when changing email account settings

I’m almost done with an applescript to change the incoming mail server settings of a user’s email account in Mail on 10.3.x.

At first, it appears to work fine. However, if I open up Mail’s preferences, or try to quit, a disconnected save sheet appears, asking if I should save the modifications to the account. If I confirm the changes, and return to my account settings in Mail’s preferences, the incoming mail server hasn’t changed. I even included a line at the end to test if the server was changing. According to my test, when the script ends, the server name has changed properly, so this seems to be a problem with Mail rather than my script. However, I don’t want to overlook the possibility of an error on my part. Can anyone help me out, or recommend another way to accomplish this change?

It’s specific to Cal Poly accounts, so if you want to recreate the error, you’ll have to make up a fake email address with the incoming mail server as either or

This is my first applescript, by the way, and I’m holding off on breaking the script down into functions (or whatever the terminology is for applescript) until I have it working properly.

Any other helpful comments or bits of advice are more than welcome.

Thanks in advance!

Here’s the script:

tell application “Mail”

set everyMailAccount to every account

set openmailAccounts to {}
set openmailAccountNames to {}

-- Obtain a list of all Openmail accounts in Mail
repeat with rep from 1 to count of everyMailAccount
	set thisAccount to item rep of everyMailAccount
	if (server name of thisAccount = "" or server name of thisAccount = "") then
		--Assert: This is an Openmail account
		set end of openmailAccounts to thisAccount
		set end of openmailAccountNames to name of thisAccount
		--Assert: This is NOT an Openmail account
	end if
end repeat

set openmailCount to count of openmailAccounts

if (openmailCount is 0) then
	--Assert: No Openmail accounts, so notify user and end the script
	display dialog "You have no Openmail accounts to migrate" buttons {"OK"} default button 1
	--Assert: Openmail accounts are present.  Continue running
	--Get from user name of account to migrate
	set nameOfAccountToMigrate to choose from list openmailAccountNames with prompt "The following accounts are still using Openmail.  If you are scheduled for migration to Oracle, select the account to migrate and press OK." without multiple selections allowed
	set nameOfAccountToMigrate to nameOfAccountToMigrate as string
	set accountToMigrate to ""
	set oldAccountServer to ""
	if (nameOfAccountToMigrate is not equal to false) then
		--Assert: User has picked an account to migrate	
		--Find the account by the name chosen
		repeat with rep from 1 to openmailCount
			set thisAccount to item rep of openmailAccounts
			if (nameOfAccountToMigrate is name of thisAccount) then
				--Assert: This is the account chosen
				set accountToMigrate to thisAccount
				--Save old account server
				copy server name of thisAccount to oldAccountServer
				--Assert: This is not the account chosen
			end if
		end repeat
		--Proceed with migration
		tell accountToMigrate to set server name to ""
		display dialog "Congratulations, migration to Oracle is complete!  Do you want to keep these changes?" buttons {"Revert back to Openmail", "Accept Migration"} default button 2
		if the button returned of the result is "Accept Migration" then
			display dialog "That's it!  Your incoming mail server is now: """ buttons {"OK"} default button 1
			tell accountToMigrate to set server name to oldAccountServer
			display dialog "Migration canceled!  Your Openmail settings have been restored." buttons {"OK"} default button 1
		end if
		--Used to test what the current server name is
		set currentServer to server name of accountToMigrate
		--Assert: The user has chosen to cancel 
	end if
end if

end tell