Mail sorting script quit working after upgrade

I recently upgraded from 10.9 to 10.11 and my mail sorting script stopped working


tell application "Mail"
	set theMsgs to selection
	repeat with i in theMsgs
		set junk mail status of i to true
		set background color of i to orange
		set flag index of i to 6
		move i to mailbox "Junk"
	end repeat
end tell

The script now give the following error, but previously worked flawlessly. I don’t have access to the library for the 10.9 version of mail anymore, so I’m having trouble running down what changed. It seems like it’s trying to process the actions on the mailbox instead of the selected messages, but I’m not sure why.

→ error number -1728 from mailbox “INBOX” of account “adam”
Result:
error “Mail got an error: Can’t set mailbox "INBOX" of account "adam" to true.” number -10006 from mailbox “INBOX” of account “adam”

The machine is now running 10.11.6 and was previously running 10.9.5

At this time I can’t test under 10.11.6 but the script behaves flawlessly under 10.12.5.
I will soon have to work under 10.11.6 so when I will do that I will test your script under this system.
Maybe there was a bug in this version.

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) dimanche 4 juin 2017 10:52:00

I was able to boot 10.11.6 sooner than expected.
Your script behaved flawlessly under this system.

Yvan KOENIG running El Capitan 10.11.6 in French (VALLAURIS, France) dimanche 4 juin 2017 11:10:32

Thanks for that. Turns out the server information for that mail account got screwed up when I upgraded. I deleted the account from mail and added it back, and now the script works again. But knowing that it worked for you was actually a big help, so thanks.

After I got it working again, I decided to add Stefan’s SKProgressBar to it. Feel free to copy and adapt.
Incidentally, this runs a LOT faster now than it did on Mavericks. I don’t know if that’s due to OS optimization, or if it’s just that my install of Mavericks was so old and messed up that nothing was running at proper speed anymore, but now it flies through the list of messages.


set iconPath to "960GB SSD:Applications:Mail.app:Contents:Resources:SidebarTrash.icns"
tell application "Mail"
	set theMsgs to selection
	set msgCount to count of theMsgs
	set msgPercent to 100 / msgCount
	my progressBar(msgCount, iconPath)
	repeat with i in theMsgs
		set headertext to subject of i
		my progressStart(msgCount, headertext)
		my progressHeader(headertext)
		my progressIncrement(msgPercent)
		set read status of i to true
		set junk mail status of i to true
		set background color of i to orange
		set flag index of i to 6
		move i to mailbox "Junk"
	end repeat
end tell


on progressBar(fileCount, iconPath)
	tell application "SKProgressBar"
		activate
		set quit delay to 1
		-- main window properties
		set floating to true --> default is true
		set position to {400, 300} --> default is {1000, 750}, origin point is bottom left
		set width to 600.0 --> default is 500.0
		set title to "Junking messages" --> default is "SKProgressBar"
		tell main bar
			set image path to iconPath
			set minimum value to 0.0 --> default is 0.0
			set maximum value to 100.0 -->  default is 100.0
			set current value to 0.0 --> default is 0.0
			set indeterminate to false --> default is true
		end tell
		
		set show window to true --> default is false
	end tell
end progressBar

on progressIncrement(msgPercent)
	tell application "SKProgressBar"
		tell main bar
			increment by msgPercent
		end tell
	end tell
end progressIncrement

on progressStart(msgCount, headertext)
	tell application "SKProgressBar"
		my progressHeader(headertext)
		tell main bar
			start animation
		end tell
	end tell
end progressStart

on progressStop()
	tell application "SKProgressBar"
		tell main bar
			stop animation
		end tell
		quit
	end tell
end progressStop

on progressHeader(headertext)
	tell application "SKProgressBar"
		tell main bar
			set header to headertext
		end tell
	end tell
end progressHeader