Hello buddies,
Here is my challenging issue:
When my customers place their orders online, sometimes they type spaces or invalid characters in their email field address.
So every next day I have a list of email addresses with some of them invalid.
i.e. {“name1@email.com”, “name 2@email.com”, “name3@émail.com”, “name4@email.cöm”}
where only the first one is valid.
Is there any way of eliminating all those invalid entries with a script and getting a “clean” list to contact directly those customers only?
Here is one solution. It is set to show the goodEmailList at the end of the routine. Replace this with badEmailList and the badEmailList will be displayed.
property legalCaps : "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
property legalLower : "abcdefghijklmnopqrstuvwxyz"
property legalNums : "0123456789"
property legalChars : "!#$%&'*+-/=?^_`{|}~@."
property doubleDot : ".."
set allLegalChars to legalCaps & legalLower & legalNums & legalChars
set emailList to {"name1@email.com", "name 2@email.com", "name3@émail.com", "name4@email.cöm", "name..5@email.com"}
set goodEmailList to {}
set badEmailList to {}
repeat with anEmail in emailList
set OK to true
if anEmail contains doubleDot then
set OK to false
copy (anEmail as text) to the end of the badEmailList
else
repeat with i from 1 to the count anEmail
set theChar to character i of anEmail
if allLegalChars does not contain theChar then
set OK to false
copy (anEmail as text) to the end of the badEmailList
exit repeat
end if
end repeat
if OK then copy (anEmail as text) to the end of the goodEmailList
end if
end repeat
goodEmailList
Model: Mac Pro (Mid 2010)
AppleScript: 2.7
Browser: Firefox 79.0
Operating System: macOS 10.14
Amazing! Great work my friend! You help me save so much time!
One last question, how can we exclude also some other invalids like “name6@emailcom” or those which do not follow in general the pattern “aaa@bbb.ccc” ?
Unfortunately there are so many daily orders with mistyped email addresses!
You can check if some text is valid email address using AsObjC. It checks pattern automatically:
use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
set emailsList to {"name1@email.com", "name 2@email.com", "name3@émail.com", "name4@email.cöm", "name..5@email.com", "name6@emailcom"}
set validEmailsList to {}
repeat with email in emailsList
set foundedText to (my findEmailAddressesIn:email)
if (contents of email) = foundedText then
set end of validEmailsList to contents of email
end if
end repeat
return validEmailsList
on findEmailAddressesIn:theString
-- Locate all the "links" in the text.
set theNSDataDetector to current application's NSDataDetector's dataDetectorWithTypes:(current application's NSTextCheckingTypeLink) |error|:(missing value)
set theString to current application's NSString's stringWithString:theString
set theURLsNSArray to theNSDataDetector's matchesInString:theString options:0 range:{location:0, |length|:theString's |length|()}
-- Extract email links
set emailPredicate to current application's NSPredicate's predicateWithFormat:"self.URL.scheme == 'mailto'"
set emailURLs to theURLsNSArray's filteredArrayUsingPredicate:emailPredicate
-- Get just the addresses
set emailsArray to emailURLs's valueForKeyPath:"URL.resourceSpecifier"
--eliminate duplicates
set emailsArray to (current application's NSSet's setWithArray:emailsArray)'s allObjects()
-- Join the remainder as a single, return-delimited text
set emailAddresses to emailsArray's componentsJoinedByString:(return)
-- Return as AppleScript text
return emailAddresses as text
end findEmailAddressesIn:
NOTE: as I see, the 5th email (with double dot) is valid email address as well as the 1st email
KniazidisR,
Very, very nice work!
The specification of valid characters for an email that I found clearly indicated that the double dot (…) was not allowed unless it was within a quoted address. This example is not quoted hence why it was not allowed in my code.
Again, I really think that your solution makes things a lot easier except that it isn’t exactly perfect.
Best,
HaoleSurferDude
@KniazidisR have I told you how much I love you?