Saturday, July 21, 2018

#1 2014-11-12 09:55:08 am

professore
Member
Registered: 2011-10-22
Posts: 29

Scripting Contacts, cannot create new group

I am attempting to write a simple script to create a group within Contacts that contains all the "Recently Added" contacts. 

When I run the script, everything looks as though it is working fine.  However, within Contacts, no group called "Recently Added" appears.  I am using Yosemite.

Suggestions appreciated.

Applescript:


property pRecentTime : 30
property pRecentGroupName : "Recently Added"

tell application "Contacts"
   
   try
       set theGroup to group pRecentGroupName
       delete theGroup
   end try
   set allGroups to the name of every group as list
   
   make new group at the end of groups with properties {name:pRecentGroupName}
   set selected of group pRecentGroupName to true
   set allGroups to the name of every group as list
   
   repeat with thePerson in people
       if thePerson's creation date is greater than (current date) - (pRecentTime * days) then add thePerson to group pRecentGroupName
   end repeat
   
end tell
end


Eric
Dum vivimus, vivimus


Filed under: contacts, groups

Offline

 

#2 2014-11-12 10:12:13 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11499
Website

Re: Scripting Contacts, cannot create new group

Hi,

you have to save the address book.
This might be bit faster, the variable allGroups isn't used at all

Applescript:


property pRecentTime : 30
property pRecentGroupName : "Recently Added"

tell application "Contacts"
   
   if exists group pRecentGroupName then
       delete group pRecentGroupName
   end if
   make new group at the end of groups with properties {name:pRecentGroupName}
   set selected of group pRecentGroupName to true
   
   set recentAdded to people whose creation date comes after (current date) - (pRecentTime * days)
   repeat with aPerson in recentAdded
       add aPerson to group pRecentGroupName
   end repeat
   save
end tell


regards

Stefan

Offline

 

#3 2014-11-13 07:17:49 am

professore
Member
Registered: 2011-10-22
Posts: 29

Re: Scripting Contacts, cannot create new group

Stephan:  Thank you so much!  That was indeed the problem.  Should have posted 5 hours earlier.  So annoying that this type of thing is relatively undocumented. 

Thanks again,
Eric


Eric
Dum vivimus, vivimus

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)