Mail script doesn't work- why?

Hi,

I have written this script to turn off deleting emails from the server for POP accounts in Mail.


tell application "Mail"
	set n to number of accounts
	repeat with m from 1 to n
		set acc_name to name of account m
		tell account acc_name
			try
				set delete mail on server to false
			end try
		end tell
	end repeat
end tell

It doesn’t work, is there another way of doing this? Or am I doing something wrong?

Thanks for any help.

John Maisey

Hi John,

Can you provide more info? What do you expect to happen when you run the script? What happens when you run the script other than “it doesn’t work”?

– Rob

Hi Rob,

I have three POP accunts set up in Mail that I want to be able to toggle to not removing emails from the server when I collect my emails.

The script should change the preferences for the accounts from remoing from the server, to not (Mail - Prefs - Accounts - Advanced - remove copy from server…). This is what I understand “set delete mail on server to false” to do.

I know the script works in general because if I change that line to “set big message warning size to 10000” it works.

I think the propery “delete mail on server” listed in Mail’s dictionary is broken. How do you find this out?

Is this any clearer?

Thanks for your help.

John

I just tested your code on a single account and it “appears” to work. I say it appears to work because using the script to check the setting after the change indicates that it was successful, even though it doesn’t appear to change anything when viewing the account in Mail’s preference window. I don’t use Mail so I didn’t run any tests to see if the setting had any impact when a mail check is actually performed.

This modfied version of your script will show you what I mean. It will throw a dialog before and after the setting is changed on each account. The dialogs will indicate the current setting. According to my test, the setting is changed.

tell application "Mail"
	activate
	set n to number of accounts
	repeat with m from 1 to n
		set acc_name to name of account m
		tell account acc_name
			try
				display dialog "Before: " & (delete mail on server as text)
				set delete mail on server to false
				display dialog "After: " & (delete mail on server as text)
			end try
		end tell
	end repeat
end tell

– Rob

Thanks Rob,

Yes, it appears to work with your tester statements. In reality the value is not changed though - the emails are still deleted from the server.

I don’t understand why this is the case.

Maybe modifying the plist file for the Mail accounts via Terminal would be possible? How do you do this? I tried the code below, but I guess the for the mail accounts is stored deeper in the file. It didn’twork.

from Terminal

defaults write com.apple.mail "DeletionPolicy" 'Never'

As I said this does not work, it adds another key “DeletionPolicy = Never” at the top level of the plist file. I also don’t know how you can call a statement in the Terminal that includes double quotes.

Again, thanks for taking the time to help.

John

It sounds like a bug to me.

I’m not very experienced with modifying plists via AppleScript so hopefully someone else can offer guidance.

– Rob

Me too - annoying - it would make what I was trying to do so simple. At least I won’t keep going round and round making little variations to the script to see if it was that.

John