Jump to content
moritzz

AppleScript Application to Automatically Report spam with Apple Mail

Recommended Posts

Hi folks

Based on the @svv07's AppleScript code, I wrote and AppleScript application (Applet) that helps you to report spam to SpamCop through Apple Mail. Note these instructions when using it:

  1. Because I check my Mail regularly on an iPad or iPhone where AppleScript is not available, I switched the AppleScript from fetching e-mail messages from Mail's spam folder. Therefore you can safely mark your messages as spam in Apple Mail for iOS and process it, as soon as you returned to your Mac.
  2. For the AppleScript to not report already reported spam, it won't process any messages in your spam folder, that matches one of these criteria:
    • The message's subject starts with `[spam]`.
    • The message is flagged. (See below.)
  3. I introduced some cleverness to set the Reporting Address even when the AppleScript is run as an AppleScript App (Applet). It will ask for your SpamCop Reporting Address on its first launch automatically and offer an option to save your Reporting Address for later runs.
    • You can reset the Reporting Address by holding down the option key while starting up the Applet.
  4. My AppleScript is capable to automatically send the reporting e-mail after it fetched and processed all spam.
  5. For the AppleScript to not report spam twice, it flags all processed messages in your spam folder.

So, folks, attached is the code and the said Applet; either download and run the Applet directly or open the source code file with `scri_pt Editor` on your Mac and save it as an Application to your Desktop or wherever you like. 

Have fun – and keep reporting spam,
yours sincerely,
Moritz

Report to SpamCop.zip

Edited by moritzz
Copy Editing

Share this post


Link to post
Share on other sites

P.S. You might need to charge the delay at line 98 depending on the speed of your Mac and Mail app.

Edited by moritzz

Share this post


Link to post
Share on other sites

Hi folks

@Lking kindly informed me, that attachments or links to downloads aren't allowed in this forum. So here comes the source code; feel free to ask on how to compile it into an app:

(*
	Report to SpamCop AppleScript
	by Moritz Zimmer
	
	based on SendToSpamCop V1.0
   written by S.J.L. v/d Velden
	
	NOTA BENE: Hold down the Option key to reset your 
	SpamCop e-mail address saved earlier.
   *)

property SpamCopAddress : ""

on run
	
	if number of characters in SpamCopAddress is less than 1 or option of (getModifierKeys()) is true then
		set dialogResult to (display dialog "Enter the SpamCop e-mail address associated with your account." default answer "" buttons {"Cancel", "Save", "Save and Continue"} default button 3 cancel button 1 with title "SpamCop AppleScript" with icon caution)
		if button returned of dialogResult is not "Cancel" then
			if button returned of dialogResult is "Save and Continue" then
				set SpamCopAddress to (text returned of dialogResult)
				set SCaccount to SpamCopAddress
			else
				set SCaccount to (text returned of dialogResult)
			end if
		else
			return
		end if
	else
		set SCaccount to SpamCopAddress
	end if
	
	(* Below is the rest of the sourcecode of the scri_pt.
   Please be very carefull editing this code *)
	
	-- Create a SpamCop folder on the desktop
	set theOutputFolderPath to path to desktop folder
	set theNewFolderName to "SpamCop"
	
	tell application "Finder"
		if (exists folder (theOutputFolderPath & theNewFolderName as string)) = false then
			make new folder at desktop with properties {name:theNewFolderName}
		end if
	end tell
	
	-- Create a new message in mail addressed to the users SpamCop account. Read the source from the selected messages in mail and save it as SpamCop readable file in the newly created SpamCop folder. Then attach each file to the new message.
	tell application "Mail"
		
		set theMessages to (messages of junk mailbox whose flagged status is false)
		set counter to 0
		
		if number of items in theMessages is greater than 0 then
			
			set theMessage to (make new outgoing message with properties {visible:true, subject:"spam Report through AppleScript", content:" "})
			repeat with thisMessage in theMessages
				
				if subject of thisMessage does not start with "[spam]" then
					set thisDate to date received of thisMessage
					if thisDate > ((current date) - (40 * hours)) then
						set counter to counter + 1
						set sourceFile to ((theOutputFolderPath & theNewFolderName as string) & ":ml" & counter & ".src")
						set thisSource to the source of thisMessage as string
						set f to open for access sourceFile with write permission
						set eof of f to 0
						write thisSource to f
						close access f
						
						tell "Finder"
							set theAttachment to sourceFile as alias
						end tell
						
						tell the theMessage
							tell content
								make new attachment with properties {file name:theAttachment} at before the first character
							end tell
						end tell
						
					end if
					
					set flagged status of thisMessage to true
					set read status of thisMessage to true
				else
					set flagged status of thisMessage to true
					set read status of thisMessage to true
				end if
				
			end repeat
			
		end if
		
		if counter is greater than 0 then
			tell theMessage
				make new to recipient at end of to recipients with properties {address:SCaccount}
			end tell
		end if
		
	end tell
	
	delay 3
	
	if counter is greater than 0 then
		set myAnswer to display dialog ("Would you like to report " & counter & " messages to SpamCop?") giving up after 90
		if gave up of myAnswer is true or button returned of myAnswer is "OK" then
			tell application "Mail"
				tell theMessage
					send
				end tell
			end tell
		end if
	end if
	
end run

use framework "Cocoa"
on getModifierKeys()
	set modifierKeysDOWN to {command:false, option:false, control:false, shift:false, fn:false, capslock:false}
	set modifierBits to current application's NSEvent's |modifierFlags|()
	set modifierBits to modifierBits * 1
	if (modifierBits > 0) then
		if (modifierBits > 8388607) then
			-- fn key is pressed, subtract it away
			set modifierBits to modifierBits - 8388608
			set fn of modifierKeysDOWN to true
		end if
		if (modifierBits > 1048575) then
			-- command key is pressed, subtract it away
			set modifierBits to modifierBits - 1048576
			set command of modifierKeysDOWN to true
		end if
		if (modifierBits > 524287) then
			-- option key is pressed, subtract it away
			set modifierBits to modifierBits - 524288
			set option of modifierKeysDOWN to true
		end if
		if (modifierBits > 262143) then
			-- ctrl key is pressed, subtract it away
			set modifierBits to modifierBits - 262144
			set control of modifierKeysDOWN to true
		end if
		if (modifierBits > 131071) then
			-- shift key is pressed, subtract it away
			set modifierBits to modifierBits - 131072
			set shift of modifierKeysDOWN to true
		end if
		if (modifierBits > 65535) then
			-- capslock key is pressed, subtract it away
			set modifierBits to modifierBits - 65536
			set capslock of modifierKeysDOWN to true
		end if
	end if
	return modifierKeysDOWN
end getModifierKeys

Yours sincerely,
Moritz 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×