How can I determine whether or not a contact with a given e-mail address exists in the Address Book? - email

I'm attempting to create a script that "walks" through a mailbox, checks the Address Book to see if the e-mail's sender is already there, and adds the contact to an Address Book group if found. If the e-mail's sender isn't found, a new contact would be created before it was added to the group.
So far, I've got this for the group adding part:
on addPersonToGroup(person)
tell application "Address Book"
add person to group "wedding guests"
end tell
save addressbook
end addPersonToGroup
and this for looping through selected messages:
tell application "Mail"
set selectedMessages to selection
if (count of selectedMessages) is equal to 0 then
display alert "No Messages Selected" message "Select the messages you want to add to 'wedding guests' address book group before running this script."
else
set weddingGuests to {}
repeat with eachMessage in selectedMessages
set emailAddress to extract address from sender of eachMessage
--if emailAddress is found in Address Book then
-- get the person from the Address Book
--else
-- create new person with emailAddress
--end if
--addPersonToGroup person
end repeat
end if
end tell
The commented-out part inside the "repeat with eachMessage ..." block is what I haven't figured out yet.
What ways are available for searching the Address Book for an e-mail address using AppleScript? Are there alternative scripting languages on the Mac that would be more suitable for such a task?

Below is the script that ultimately got me the result I wanted. Thanks to #fireshadow52 for the assist:
tell application "Mail"
set selectedMessages to selection
if (count of selectedMessages) is equal to 0 then
display alert "No Messages Selected" message "Select the messages you want to add to 'wedding guests' address book group before running this script."
else
set weddingGuests to {}
repeat with eachMessage in selectedMessages
set emailSender to (extract name from sender of eachMessage) as string
set emailAddress to (extract address from sender of eachMessage) as string
my addSender(emailSender, emailAddress)
end repeat
end if
end tell
on splitText(delimiter, someText)
set prevTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to delimiter
set output to text items of someText
set AppleScript's text item delimiters to prevTIDs
return output
end splitText
on addSender(theSender, theEmail)
tell application "Address Book"
set tmp to my splitText(" ", theSender)
set numOfItems to count tmp
set senderFirst to item 1 of tmp
if (numOfItems) is greater than 1 then
set senderLast to item 2 of tmp
else
set senderLast to ""
end if
try
set the check to get first person whose first name is senderFirst and last name is senderLast
--No error, sender exists
my addPersonToGroup(check)
on error --sender is not in Address Book yet; add sender and email to contacts and add the new contact to group
set newPerson to (make new person with properties {first name:senderFirst, last name:senderLast})
--Add Email
make new email at the end of emails of newPerson with properties {label:"Email:", value:theEmail}
--add the new person to the group
my addPersonToGroup(newPerson)
end try
end tell
end addSender
on addPersonToGroup(theSender)
tell application "Address Book"
add theSender to group "wedding guests"
save
end tell
end addPersonToGroup

Believe it or not you kind of wrote the answer in the question!
--In your "looping through selected messages" script, add this line...
set emailSender to (get sender of eachMessage) as string
--...before this one...
set emailAddress to (extract address from sender of eachMessage) as string
--This will help you later
--You should add this subroutine at the bottom of your script to check whether the contact exists or not; invoke it by doing 'my addSender(emailSender, emailAddress)'
on addSender(theSender, theEmail)
tell application "Address Book"
set the check to (get first person whose first name is theSender) as list
if check is {} --sender is not in Address Book yet; add sender and email to contacts and add the new contact to group
set newPerson to (make new person with properties {first name:theSender, last name:null}) --Sorry if you want the last name too
--Add Email
make new email at the end of emails of newPerson with properties {label:"Email:", value:theEmail}
--add the new person to the group
my addPersonToGroup(newPerson)
else --sender is in Address Book, add sender to group
my addPersonToGroup(check)
end if
end tell
end addSender
As always, if you need help, just ask. :)
P.S. If you get an error in the subroutine, it is most likely the if block's fault. To fix the error (and to keep the script running), just change the if block to a try block, as shown here:
try
set check to (get first person whose first name is theSender) as list
--No error, sender exists
my addPersonToGroup(check)
on error --Error, sender is not in Address Book yet; add sender and email to contacts and add the new contact to group
set newPerson to (make new person with properties {first name:theSender, last name:null}) --Sorry if you want the last name too
--Add Email
make new email at the end of emails of newPerson with properties {label:"Email:", value:theEmail}
--add the new person to the group
my addPersonToGroup(newPerson)
end try
P.P.S. person is a reserved word for Address Book. Change person to some other variable and it will work (referring to the addPersonToGroup subroutine).

Related

AppleScript to add multiple address from selected message to given rule

Want to Select Multiple messages in Mail.app, then
1.move them to a folder "AdvertRule"
2.Add Condition (sender's email address) to existing rule "TestRule 23"
Results from Code
works perfectly
Partial, wud add condition to the rule, x times if x number of messages selected, and all emails addresses in same condition as in ( a#aa.com b#bb.com c#cc.com)
Wondering if can be helped to get 1 email address for each condition in the Rule
even better if has error correction (wud not form a new Condition if it already exists)
Tx in advance
(*
Based on
https://apple.stackexchange.com/questions/149008/in-mail-app-get-list-of-recipients-for-just-one-of-the-many-email-accounts
https://discussions.apple.com/thread/7319379
Move selected Messages to another Folder
*)
tell application "Mail"
set theSenderList to {}
set target_account to "Exchange"
set target_mailbox to "AdvertRule"
set theMessages to the selected messages of message viewer 0
repeat with aMessage in theMessages
set end of theSenderList to (extract address from sender of aMessage)
set oldDelimits to AppleScript's text item delimiters
set AppleScript's text item delimiters to " "
set theSender to (theSenderList as string)
set AppleScript's text item delimiters to oldDelimits
end repeat
set r to rule "TestRule 23"
set s to selection
if s is not "" then
repeat with currentMessage in s
move currentMessage to (first mailbox whose name is target_mailbox) of account target_account
tell r
make new rule condition at end of rule conditions with properties {expression:theSender, qualifier:does contain value, rule type:from header}
end tell
end repeat
end if
end tell
Hey after reading thru the applescript for beginners
After getting Sender List, then add rule with every sender in the sender list, then with every message in selection, move the message
That shud work

Applescript to delete contacts from list

I accidentally added about 600 contacts to my Mac addressbook from a friend's iPhone I synced to my Mac a couple weeks ago. Unfortunately I dont have a backup of my contacts and now they are all synced with my iPhone and iCloud.
I was able to export all the unwanted contacts as ".vcf" files from my friends phone so I have about 600 files like "John Doe.vcf" and also a plain text list of just the contact names.
Ideally I would like a script that matches the data inside the VCF file to make sure I'm deleteing the right contact.
Idea #2 would be to delete the contacts that matches the name in list, skipping the searches that return two or more results (that way I dont lose my own contacts with same name as my friend's)
I started by attempint to delete a single contact using the following applescript but with no luck. Please help!
set daName to "John Doe"
tell application "Contacts"
repeat with onePerson in people
if (value of name of onePerson) is daName then
delete onePerson
end if
end repeat
end tell
--UPDATE--
Here is the final code I used to perform the task. I added a loop at the top that cycles through the list of names copied in the clipboard. I also added the name of the duplicated contact in the dialog box so I could take note of it.
Tips:
Make a backup of all your contacts before running the script.
If you want to see the number of contacts "magically" decreasing
while running the script, scroll down to the of your contacts in
your address book and you'll see the totla amount of contacts
there.
FINAL CODE
set the clipboard to (the clipboard as text)
set the_strings to the clipboard
repeat with this_string in paragraphs of the_strings
set daName to this_string
DeleteContact(daName)
end repeat
on DeleteContact(theName)
tell application "Contacts"
set myList to every person whose name is theName
set Nb to count of myList
if Nb = 0 then return -- no record found
if Nb > 1 then
display dialog theName & " (too many found: do nothing)"
else
set myperson to item 1 of myList -- only 1 found to be deleted
delete myperson
save
end if
end tell
end DeleteContact
The script bellow contains sub-routine which check name and delete. I added some comments to make it easier to adapt for your need :you can build a loop looking for your card and calling the DeleteContact sub-routine.
set daName to "John Doe"
DeleteContact(daName)
on DeleteContact(theName)
tell application "Contacts"
set myList to every person whose name is theName
set Nb to count of myList
if Nb = 0 then return -- no record found
if Nb > 1 then
display dialog "too many found: do nothing"
else
set myperson to item 1 of myList -- only 1 found to be deleted
delete myperson
save
end if
end tell
end DeleteContact
In case same name exist in multiple contact, I display a dialog, but up to you to decide what to do here.
Gio
This is a script I had and it may address your problem although you never said what the problem was.
tell application "Contacts"
local results,dlist,dloop
set results to {}
set dlist to people whose (first name contains "Oxnard" or (last name is equal to "Abercrombe"))
repeat with dloop in dlist
delete dloop
end repeat
set end of results to "Records deleted :" & count of dlist
save
return results
end tell
Note that the Contacts dictionary does define a name property however it will be based on preference settings.

VB Script to change the from field of emails in Outbox

I have emails in my Outbox in Outlook and I'd like to run a vb script to change the from field of these emails to some other email
I managed to do the following but it doesn't work as I'd like to and therefore I am missing the main piece. I'd appreciate if someone could help.
intFolderOutbox = 4
msoFileDialogOpen = 1
' Load requied objects
Set WshShell = WScript.CreateObject("WScript.Shell") ' Windows Shell
Set ObjOlApp = CreateObject("Outlook.Application") ' Outlook
Set ns = ObjOlApp.GetNamespace("MAPI") ' Outlook
Set box = ns.GetDefaultFolder(intFolderOutbox) ' Outlook
For Each Item In box.Items
*** HERE IS WHAT I NEED TO REPLACE THE FROM FIELD ****
Item.sender = "email2#gmail.com"
Item.Update
Item.Save
Next
Something like the following works adding a recipient but I couldn't find the equivalent to the from field.
Item.Recipients.Add "email2#gmail.com"
Here is something that could help but it doesn't work in my case
Set oAddrEntry = CreateObject("MAPI.AddressEntry")
oAddrEntry.Name = SYSTEM_ADDRESS
oAddrEntry.resolve
Set oNewMsg.sender = oAddrEntry
oNewMsg.Update
oNewMsg.Send
Thanks
Firstly, once a message is submitted (and moved to Outbox) it cannot be touched - it belongs to the spooler.
Secondly, you cannot send on behalf of an arbitrary user. In case of Exchange, set the MailItem.SentOnBehalfOfName property to the name of the Exchange mailbox on whose behalf the current user can send. In case of POP3/SMTP accounts, set the MailItem.SendUsingAccount property to one of the accounts from the Namespace.Accounts collection.

Get the Date of an Email via AppleScript

I am currently trying to customize an AppleScript that is reading the unread Mails in the Inbox.
That basically works fine except the fact, that I can#T manage to get the date of the mails.
After about 2 hours of googling around I found out that the Variable I need should be receivedate or deliverydate, but when trying to use one of these I get an error like this:
"...receivedate of id... cannot be converted to Type reference..."
Someone's got an Idea, how I can convert this?
And this is my current code:
tell application "System Events"
set processList to (name of every process)
end tell
if processList contains "Mail" then
tell application "Mail"
if (unread count of inbox) > 0 then
set messageList to (messages of inbox) whose read status is false
set output to "Mails:" & return & return & ""
repeat with itemNum from 1 to (unread count of inbox)
set itemDate to (receivedate of item itemNum of messageList)
set output to output & itemDate & " - " & (extract name from sender of item itemNum of messageList) & return & subject of item itemNum of messageList & return & return
end repeat
end if
end tell
else
set output to "ÄpplMäil isch aus..."
end if
The term you are looking for is date received
tell application "Mail" to if running then
if (unread count of inbox) > 0 then
set output to "Mails:" & return & return & ""
repeat with thisMsg in (get messages of inbox whose read status is false)
tell thisMsg to set output to output & (date received) & " - " & (extract name from sender) & return & subject & return & return
end repeat
end if
else
set output to "ÄpplMäil isch aus..."
end if
A quicker way to get the help you need is the Applescript dictionarie of Mail.
All commands, classes and properties of Mail are in this dictionary.
One way of opening this dictionary is to use the Open Dictionary item in the File menu of AppleScript Editor. When you use this item, you will get a listing of the available applications that have dictionaries. Choose Mail and click the Open button or use the Browse button to navigate to an unlisted application.
Another way to open a dictionary, is to make use of AppleScript Editor's Library window. It's located in the Window menu below the Result History and Event Log History menu items. The Library window shows a list of default applications whose dictionaries you can open with a double click.

Access 2010 Trying to Enter a unique record through Form

I'm trying to input an error check for my form. I have the user entering the name and I would like a prompt to inform them if they are attempting to use a Name already in the records.
Example: the Person table has 3 records with FNames being: Jeff, Kyle, Darren.
If on the add person form in the Fname Box Kyle is entered, the after update event will notify the user that this name has been claimed and null the field. Where as if Greg was enter no notifications will occur.
I just don't know how to compare a text field value to values in a filtered query list, and Google searches have other loosely related links in the way.
Thank you for help!
If all fnames must be unique, add a unique index to the table. This will prevent duplicates being entered. The form error property will allow you to provide a custom error.
You can also check if the name exists in the Before Update event of the control.
In this example, the control and field are both called AText. Generally, you should rename controls so they are not the same as fields.
Private Sub AText_BeforeUpdate(Cancel As Integer)
Dim IsOk As Boolean
''One of the very few places where the .Text property is used
sLookUp = Me.AText.Text
IsOk = IsNull(DLookup("Atext", "Table1", "Atext='" & sLookUp & "'"))
If Not IsOk Then
MsgBox "Found!"
Cancel = True
End If
End Sub