Flagged Mail to Notes Script Dupes Notes Endlessly

Please help me fix my script.

  • Script runs on my iMac, every 90 seconds (plisterine.app)
  • It moves all email to a single account ($hidden_domain_name is my obfuscation for posting here)
  • It then looks for flagged email colored orange/red
  • Changes the flag to a different flag color
  • Creates a note with that flagged email
  • Moves flagged email to Archive

This script works (mostly?) just fine if I flag the email in Mail.app, on the iMac that runs this script. BUT, if I flag email on my MacBook, iPhone, or iPad, it SOMETIMES gets caught in an endless loop and creates the same note with every run.

Can you help me stop this behavior?

-- START Move Mail to $hidden_domain_name
tell application "Mail"
	synchronize with account "$hidden_domain_name"
	delay 5
	repeat with theMessage in (every message of (mailbox "INBOX" of account "iCloud"))
		set mailbox of theMessage to mailbox "INBOX" of account "$hidden_domain_name"
	end repeat
	repeat with theMessage in (every message of (mailbox "Sent" of account "iCloud"))
		set mailbox of theMessage to mailbox "Sent" of account "$hidden_domain_name"
	end repeat
	repeat with theMessage in (every message of (mailbox "Sent" of account "Google"))
		set mailbox of theMessage to mailbox "Sent" of account "$hidden_domain_name"
	end repeat
	repeat with theMessage in (every message of (mailbox "INBOX" of account "Google"))
		set mailbox of theMessage to mailbox "INBOX" of account "$hidden_domain_name"
	end repeat
	synchronize with account "$hidden_domain_name"
	delay 3
end tell
-- END Move Mail to $hidden_domain_name

-- START Flagged Mail to Notes
set newline to ASCII character 10
set formatBody to ""

tell application "Mail"
	set theMessages to every message of mailbox "INBOX" of account "$hidden_domain_name" whose flag index is 0
	repeat with i from 1 to number of items in theMessages
		
		set thisMessage to item i of theMessages
		set Message_Subject to subject of thisMessage
		set Message_Sender to sender of thisMessage
		set Message_Body to content of thisMessage
		set Message_ID to message id of thisMessage
		set Message_Date to date received of thisMessage
		
		tell application "Notes"
			set Note_Body to "<pre style=\"font-family:Helvetica,sans-serif;\">" & "FROM: " & Message_Sender & newline & "DATE: " & Message_Date & newline & Message_Body & "</pre>"
			tell account "iCloud"
				make new note at folder "Notes" with properties {creation date:Message_Date, name:"✉️" & Message_Subject, body:Note_Body & " message://%3c" & Message_ID & "%3e"}
				delay 2
			end tell
		end tell
		
		set flag index of thisMessage to 3
		delay 2
		set mailbox of thisMessage to mailbox "Archive" of account "$hidden_domain_name"
	end repeat
end tell

tell application "Mail"
	set theMessages to every message of mailbox "Sent" of account "$hidden_domain_name" whose flag index is 0
	repeat with i from 1 to number of items in theMessages
		
		set thisMessage to item i of theMessages
		set Message_Subject to subject of thisMessage
		set Message_Sender to sender of thisMessage
		set Message_Body to content of thisMessage
		set Message_ID to message id of thisMessage
		set Message_Date to date received of thisMessage
		
		tell application "Notes"
			set Note_Body to "<pre style=\"font-family:Helvetica,sans-serif;\">" & "FROM: " & Message_Sender & newline & "DATE: " & Message_Date & newline & Message_Body & "</pre>"
			tell account "iCloud"
				make new note at folder "Notes" with properties {creation date:Message_Date, name:"✉️" & Message_Subject, body:Note_Body & " message://%3c" & Message_ID & "%3e"}
				delay 2
			end tell
		end tell
		
		set flag index of thisMessage to 3
		delay 2
		set mailbox of thisMessage to mailbox "Archive" of account "$hidden_domain_name"
	end repeat
end tell
-- END Flagged Mail to Notes

-- START Move Mail to $hidden_domain_name
tell application "Mail"
	synchronize with account "$hidden_domain_name"
	delay 5
	repeat with theMessage in (every message of (mailbox "INBOX" of account "iCloud"))
		set mailbox of theMessage to mailbox "INBOX" of account "$hidden_domain_name"
	end repeat
	repeat with theMessage in (every message of (mailbox "Sent" of account "iCloud"))
		set mailbox of theMessage to mailbox "Sent" of account "$hidden_domain_name"
	end repeat
	repeat with theMessage in (every message of (mailbox "Sent" of account "Google"))
		set mailbox of theMessage to mailbox "Sent" of account "$hidden_domain_name"
	end repeat
	repeat with theMessage in (every message of (mailbox "INBOX" of account "Google"))
		set mailbox of theMessage to mailbox "INBOX" of account "$hidden_domain_name"
	end repeat
	synchronize with account "$hidden_domain_name"
	delay 3
end tell
-- END Move Mail to $hidden_domain_name