help with Spamcop Reporting script for Mail

Concise Version of Question:
How do I modify the applescript shown below to append 2 ‘line breaks’ and ‘a bit of text’ to the end of each saved “.src” text file?

Detailed Version of Question:
(I didn’t want to include this, it will just overcomplicate things, but I know SOMEONE will ask for it, so here it is.)
I use the free Spamcop.net spam reporting service as one of the ways I report/fight spam. Recently, I have been receiving lots of poorly coded spam, that appears incorrectly (body of the email is empty), but is still definitely spam (all the usual spam material is in the source/headers). This poor spam coding prevents me from reporting the spam to most services, usually with an error that says something like “this email is not spam because there is no body to the message”. Troubleshooting with help from Spamcop, we’ve determined that the spam source needs to be appended to add 2 ‘line breaks’ and ‘a bit of text’ to the end of each, before reporting, which should remedy the error with most services. The typical reporting method is to simply select the spam email in Mail and “Forward as Attachment” to the various services/recipients. There is no way to append information with this process. However, with help, I was able to locate a script written by another user that does somewhat the same thing, but may allow me to modify it to remedy my problem. So how do I modify the applescript shown to append 2 ‘line breaks’ and ‘a bit of text’ to the end of each saved “.src” text file?

Applescript

(* SendToSpamCop V1.0
  written by S.J.L. v/d Velden
  The code below is open source and you may freely  edit and redistribute it.
  Though I would appreciate it if you gave credits to  the original author (me).
  --------------------------------------------------------

   You have to customize a variable to make this scri_pt
   work with your SpamCop-account.
   
  Below you see the line that begins with "set
  SCaccount..."
  Replace the text YOURACCOUNT with the email address
  you've got from SpamCop.net to forward your spam to.
   *)
--

set SCaccount to "YOURACCOUNT"


(* 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 the selection
    set counter to 1
    set theMessage to (make new outgoing message with properties {visible:true, subject:"report spam", content:" "})
    repeat with thisMessage in theMessages
        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
        
        set counter to counter + 1
    end repeat
    tell theMessage
        make new to recipient at end of to recipients with properties {address:SCaccount}
    end tell
end tell

Original Spamcop Forum Thread with my Reporting Problem:
(I wouldn’t open this, it will just overcomplicate things, but I know SOMEONE will ask for it, so here it is.)
http://forum.spamcop.net/forums/index.php?s=&showtopic=10162&view=findpost&p=69742

Original Spamcop Forum Thread with Applescript:
(I wouldn’t open this, it will just overcomplicate things, but I know SOMEONE will ask for it, so here it is.)
http://forum.spamcop.net/forums/index.php?s=eaa95afbcc161204888beaf0ba62af53&showtopic=7886&view=findpost&p=53835

Hi,

replace


write thisSource to f

with


write thisSource & return & return & "a bit of text" to f

StefanK: THANK YOU! It works like a charm.

I added your change and some others. The final script is as follows:

(* SendToSpamCop V2.0
  written by S.J.L. v/d Velden
  The code below is open source and you may freely edit and redistribute it.  Though I would appreciate it if you gave credits to the original author (me).
 Modified by Sbimos and Legioss to allow reporting of "no-body" spam, reporting to multiple services, reporting via bcc, and use of Address Book groups.
 Assistance from:
 StefanK at Macscriptor.net
 RConner at Spamcop.net
 Wazoo at Spamcop.net
  --------------------------------------------------------

   You have to customize the recipient variable to make this scri_pt
   work with your SpamCop-account and, or, other reporting services.
   
  Below you see the line that begins with "set SCaccount..."
  Replace the text YOURACCOUNT with the intended recipient address or addresses.  
  Use the personalized reporting address you received from SpamCop.net to forward your spam to, or the reporting address of other services you may use.
  If you are using multiple reporting services, seperate the email addresses with a comma and a space.
  If you have an Address Book group, simply replace with the group name.
   *)
--

set SCaccount to "YOURACCOUNT"


(* 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 bcc addressed to the user's SpamCop account, and, or, other reporting services.  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.  Two line breaks and note "(This line added to ensure proper spam reporting.)"appended to the end of each file.
tell application "Mail"
	set theMessages to the selection
	set counter to 1
	set theMessage to (make new outgoing message with properties {visible:true, subject:"report spam", content:" "})
	repeat with thisMessage in theMessages
		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 & return & return & "(This line added to ensure proper spam reporting.)" 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
		
		set counter to counter + 1
	end repeat
	tell theMessage
		make new to recipient at end of bcc recipients with properties {address:SCaccount}
	end tell
end tell

I modified this script a while back to make the temporary folder invisible on the desktop. However, I think at a later time, the script stopped functioning. I figured out that the problem is caused by the PREVIOUS message attachment(s) getting stuck in the invisible folder. Deleting by hand [each time] allows the script to function normally. I’m not sure why this problem popped up.

Looking for a little help modifying the script to remove all files with extension “.src” from invisible desktop folder “.Spamcop”, BEFORE generating the new “.src” file in the same folder.

(* SendToSpamCop V2.1
  written by S.J.L. v/d Velden
  The code below is open source and you may freely edit and redistribute it.  Though I would appreciate it if you gave credits to the original author (me).
 Modified by Sbimos and Legioss to allow reporting of "no-body" spam, reporting to multiple services, reporting via bcc, and use of Address Book groups.
 Assistance from:
 StefanK at Macscriptor.net
 RConner at Spamcop.net
 Wazoo at Spamcop.net

 Changelog:
 2.1 -xxxx.xx.xx- Made attachment folder invisible on desktop.
  --------------------------------------------------------

   You have to customize the recipient variable to make this scri_pt
   work with your SpamCop-account and, or, other reporting services.
   
  Below you see the line that begins with "set SCaccount..."
  Replace the text YOURACCOUNT with the intended recipient address or addresses.  
  Use the personalized reporting address you received from SpamCop.net to forward your spam to, or the reporting address of other services you may use.
  If you are using multiple reporting services, seperate the email addresses with a comma and a space.
  If you have an Address Book group, simply replace with the group name.
   *)
--

set SCaccount to "YOURACCOUNT"


(* 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 bcc addressed to the user's SpamCop account, and, or, other reporting services.  Read the source from the selected messages in mail and save it as SpamCop readable file in the newly created invisible SpamCop folder. Then attach each file to the new message.  Two line breaks and note "(This line added to ensure proper spam reporting.)" appended to the end of each file.
tell application "Mail"
	set theMessages to the selection
	set counter to 1
	set theMessage to (make new outgoing message with properties {visible:true, subject:"report spam", content:" "})
	repeat with thisMessage in theMessages
		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 & return & return & "(This line added to ensure proper spam reporting.)" 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
		
		set counter to counter + 1
	end repeat
	tell theMessage
		make new to recipient at end of bcc recipients with properties {address:SCaccount}
	end tell
end tell

Thanks in advance.

Browser: Safari 534.52.7
Operating System: Mac OS X (10.6)

“Please”?

Got some help, seems to have solved the problem. Updated script below.

@Tom_X: Just noticed you added the “try” within the “if”. I didn’t add it yet as the script is functioning with your other change, and didn’t want to mess up the other portions you hadn’t seen yet. But what was the point of the “try”?

(* SendToSpamCop v2.2
written by S.J.L. v/d Velden
The code below is open source and you may freely edit and redistribute it. Though I would appreciate it if you gave credits to the original author (me).
Modified by Sbimos and Legioss to allow reporting of "no-body" spam, reporting to multiple services, reporting via bcc, and use of Address Book groups.
Assistance from:
Legioss at Macscriptor.net
StefanK at Macscriptor.net
Tom_X at Macscriptor.net
Sbimos at Spamcop.net
RConner at Spamcop.net
Wazoo at Spamcop.net

Changelog:
2.2 -2012.01.16- Clears attachment folder of previous attachments.
2.1 -xxxx.xx.xx- Made attachment folder invisible on desktop.
--------------------------------------------------------

You have to customize the recipient variable to make this scri_pt
work with your SpamCop-account and, or, other reporting services.

Below you see the line that begins with "set SCaccount..."
Replace the text YOURACCOUNT with the intended recipient address or addresses. 
Use the personalized reporting address you received from SpamCop.net to forward your spam to, or the reporting address of other services you may use.
If you are using multiple reporting services, seperate the email addresses with a comma and a space.
If you have an Address Book group, simply replace with the group name.
*)
--

set SCaccount to "YOURACCOUNT"


(* 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.  If one already exists, remove previous attachments within.
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

do shell script "rm -fr ~/Desktop/.Spamcop/*"

-- Create a new message in mail bcc addressed to the user's SpamCop account, and, or, other reporting services. Read the source from the selected messages in mail and save it as SpamCop readable file in the newly created invisible SpamCop folder. Then attach each file to the new message. Two line breaks and note "(This line added to ensure proper spam reporting.)" appended to the end of each file.
tell application "Mail"
   set theMessages to the selection
   set counter to 1
   set theMessage to (make new outgoing message with properties {visible:true, subject:"report spam", content:" "})
   repeat with thisMessage in theMessages
       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 & return & return & "(This line added to ensure proper spam reporting.)" 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
       
       set counter to counter + 1
   end repeat
   tell theMessage
       make new to recipient at end of bcc recipients with properties {address:SCaccount}
   end tell
end tell

Shoot. I don’t think this works after all. I use Invisibility Toggler (http://www.macupdate.com/app/mac/24632/invisibility-toggler) to make the invisible folder visible for the sake of testing the script (but the folder name and path are UNCHANGED). However, the shell script seems to stop the applescript in it’s tracks when the invisible folder is NOT visible. =(

Updated successful script:

(* SendToSpamCop v2.2
written by S.J.L. v/d Velden
The code below is open source and you may freely edit and redistribute it. Though I would appreciate it if you gave credits to the original author (me).
Modified by Sbimos and Legioss to allow reporting of "no-body" spam, reporting to multiple services, reporting via bcc, use of Address Book groups, hiding the temporary cache folder, and to fix cache error with a hidden folder.

Assistance from:
Legioss at Macscriptor.net
StefanK at Macscriptor.net
Tom_X at Macscriptor.net
Sbimos at Spamcop.net
RConner at Spamcop.net
Wazoo at Spamcop.net

Changelog:
2.2 -2012.01.16- Deletes previous attachments folder.
2.1 -xxxx.xx.xx- Made attachment folder invisible on desktop.
  --------------------------------------------------------

   You have to customize the recipient variable to make this scri_pt
   work with your SpamCop-account and, or, other reporting services.
   
  Below you see the line that begins with "set SCaccount..."
  Replace the text "SpamMac" with the intended recipient address or group.  
  Use the personalized reporting address you received from SpamCop.net to forward your spam to, or the reporting address of other services you may use.
  If you are using multiple reporting services, seperate the email addresses with a comma and a space.
  If you have an Address Book group, simply replace with the group name.
   *)
--

set SCaccount to "SpamMac"


(* Below is the rest of the sourcecode of the scri_pt.
   Please be very carefull editing this code *)

-- Delete the entire previous invisible ".Spamcop" folder from your desktop.  Create a new invisible ".SpamCop" folder on the desktop for use.
do shell script "rm -fr ~/Desktop/.Spamcop/"

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 BCC addressed to the user's SpamCop account, and/or, other reporting services.  Read the source from the selected messages in mail and save it as SpamCop readable file in the newly created invisible ".SpamCop" folder on your desktop. Then attach each file to the new message.  Two line breaks and note "(This line added to ensure proper spam reporting.)"appended to the end of each file.
tell application "Mail"
	set theMessages to the selection
	set counter to 1
	set theMessage to (make new outgoing message with properties {visible:true, subject:"report abuse", content:" "})
	repeat with thisMessage in theMessages
		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 & return & return & "(This line added to ensure proper spam reporting.)" 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
		
		set counter to counter + 1
	end repeat
	tell theMessage
		make new to recipient at end of bcc recipients with properties {address:SCaccount}
	end tell
end tell

Ok, I need help updating the above script again. I think maybe one of the OSX updates/upgrades may have broken it (running 10.8.3 now).

Current issue:
When run, the script only generates a blank email, no attachments, no recipients, no body, only shows correct subject line.

Current workaround:
Running the script a SECOND time with the same targets generates a 2nd new email, with all correct fields, attachments, etc.

Desired result:
I want to modify the script so it runs correctly the first time, with no need for a second run.

Clues to problem?:
I’ve noticed that the first time I run the ‘broken’ script, it DOES delete the temp “.Spamcop” folder, but never generates a new one. On the second run (the repeat), it correctly generates the folder and files within.

Model: MBP 17"
Browser: Safari 536.29.13
Operating System: Mac OS X (10.8)

Strange. I Updated to 10.8.4 yesterday, and the issue vanished. It had been broken for 6-12 months prior.

I FINALLY had time to get upgraded to El Capitan, and I’m still having the issue. Any help?

Now on:
Model: MBP 17" (same)
Browser: Safari 9.0.2
Operating System: Mac OS X (10.11.2)

Browser: Safari 601.3.9
Operating System: Mac OS X (10.10)