Hi All,
In different areas of my website I either remove email addresses completely (for privacy reasons) or cloak the “@” symbol (in an attempt to frustrate robots trying to harvest addresses for spammers). I’ve traditionally done this, server-siide, using PHP but am now shifting a lot of this functionality to my desktop Mac and AppleScript.
After some excellent assistance here (especially kai, his contented crocodile, and Nigel Garvey) I’ve sorted out some linebreak troubles during the transfer of text from Mail.app and FileMaker, and I’ve refined my script to entirely replace the email addresses. Now I’ve turned my efforts to the problem of replacing the @ in email addresses without, hopefully, messing-up other uses of @ within a body of text.
The script, so far, is included below and it does the job pretty well, but I have a few questions and would appreciate your thoughts.
1. Is there a better way to winnow-out the email addresses? I’ve simply set the script to consider any text item that contains at least one “@” and at least one “.” and is seven or more characters in length.
2. My first “set text item delimiters to …” works fine if I use “space & return” but not simply “return” (as I’d expected and started out). If I use “return” the script misses some of the addresses. I don’t understand why this is the case … but using “space & return” seems to fix it.
3. On my website I use a small graphic “@” (at.gif) to replace the text “@”. This helps maintain the ‘look’ of the address but offers a degree of spammer harvest protection. I’ve tried setting ReplacementString to “<img src="images/at.gif" width="15" align="absmiddle">” but it doesn’t work properly. If I don’t use the backslash escape characters it doesn’t work at all but if I do then the final ModifiedText also shows the backslashes. How do I need to initially set ReplacementString so the final text that replaces @ is "?
4. Are there tidier or smarter ways of doing this simple task?
Cheers
Dougal
set StartText to "
From: noobie@paradise.net.nz
Subject: Reply to topic: 'Changing the @ within email addresses'
Date: 13 January 2006 1:58:51 AM
To: some_dude@macscripter.net
Return-Path: <www@paradise.net.nz >
Delivered-To: noobie@paradise.net.nz
X-Envelope-To: noobie@paradise.net.nz
Received: (qmail 5997 invoked from network); 12 Jan 2006 12:58:55 -0000
Message-Id: 20000111125851.5909B378D9F@paradise.net.nz
Hi Everyone,
I want to try and cloak the @ symbol within the email addresses contained within a body of text, but I don't want to mess-up adjacent punctuation or angle-brackets:
bloggs@hotmail.com;
noobie@paradise.net.nz;
bloggs@hotmail.com:, bloggs@hotmail.com;, and bloggs@hotmail.com.
<bloggs@hotmail.com>
@, @@, @@@, and @@@@
The c@ s@ on the m@ and looked @ the f@ b@.
Cheers
Dougal"
set ReplacementString to "[at]"
set ModifiedText to StartText
-- make sure the text uses ASCII_13 returns. Mail.app returns ASCII_10s.
-- Thanks to kai and his contented crocodile
set text item delimiters to space & return
tell ModifiedText's paragraphs to set ModifiedText to beginning & ({space} & rest)
set text item delimiters to space
set AddressTest to ModifiedText contains "@"
if AddressTest then
set text item delimiters to space
set TextParts to text items of ModifiedText
set WordCount to count TextParts
considering case
-- Thanks Nigel ... I'm afraid I still don't understand why it's quicker to consider case but I'm sure i will one day
repeat with n from 1 to WordCount
-- find the words that might be addresses
-- and test to see if they're likely to be an email address
set PossibleAddress to item n of TextParts
if PossibleAddress contains "@" and PossibleAddress contains "." and length of PossibleAddress > 6 then
-- it looks and smells a little like an email address so get to work on it
-- first change the @ within PossibleAddress
set text item delimiters to "@"
set InterimPossibleAddress to text items of PossibleAddress
set text item delimiters to ReplacementString
set ModifiedPossibleAddress to InterimPossibleAddress as text
set text item delimiters to space
-- then put the modified email address back into the main text
set text item delimiters to PossibleAddress
set InterimText to text items of ModifiedText
set text item delimiters to ModifiedPossibleAddress
set ModifiedText to InterimText as text
set text item delimiters to space
end if
end repeat
end considering
end if
-- have a look at the results
return ModifiedText