Help is written in the few comment lines on top of the script.
#Address Groups
#Address Book
#0,003
#Created by Joy, 02.10.13
#=====================================
#USE:
#Step 1) first, select some people and then, run this script
#Step 2) choose a category (job title) or add a new one for the selected people
#
#BEHAVIOUR:
#When you start for the first time:
#by default, some empty groups (job title categories) will be created, if the script finds no groups to sort people
#A group container name can handle multiple labels (job titles) to collect smaller groups of people.
#and finally, a check collects people who isnt currently categorized in any of the existing groups
#
#DISCLAIMER:
# no Warranties. Use this software at your own Risk.
# This software is free. Credits: joy 2013
#=====================================
property del : "________________________"
property def_groups : {"Office", "Books", "Friends", "Shop-Workshop", "Jobs", "Client", "Museum", "Health", "Uni-Research", "Entertainment", "Housing", "Club", "Transportation", "Shipment"}
tell application "Address Book"
if unsaved is true then save
set ungrouped_tag to "# Ungrouped"
#create groups
set cu_groups to groups whose id ends with "ABGroup"
if cu_groups is {} then
repeat with a in def_groups
if not (exists group a) then make new group with properties {name:a}
end repeat
else
set def_groups to {}
repeat with b in cu_groups
if name of b does not start with ungrouped_tag then copy name of b to end of def_groups
end repeat
end if
set sel to the selection
if sel is {} then
activate
display dialog "NO PERSONS SELECTED!" & return & return & "Please select some people, who belong to a particular GROUP, like : Club" buttons "Cancel" default button 1 with icon 2
return
end if
#prepare group tag
set job_title to my group_tag(def_groups)
if job_title is false then return
#correct symbolic names
if job_title is "Uni" then
set job_title to "University"
else if job_title is "Jobs" then
set job_title to "Human Resources"
end if
repeat with a in sel
#set department of a to ""
if job title of a is missing value then set job title of a to job_title
end repeat
repeat with a in sel
set job_tt to job title of a
if job_tt is not in def_groups then
set more_groups to item 1 of (groups whose name contains job_tt)
add a to more_groups
else
add a to group job_tt
end if
end repeat
if unsaved is true then save
#update uncategorized people's list
set reg_people to {}
repeat with a in def_groups
set reg_people to reg_people & (name of people of group a)
end repeat
set all_p to name of people
set new_people to {}
repeat with a in all_p
if a is not in reg_people then set new_people to new_people & a
end repeat
#purge old group entries
try
delete group ungrouped_tag
delay 0.5
save
end try
if length of new_people is 0 then return
#make a new ungrouped list
delay 0.5
make new group with properties {name:ungrouped_tag}
set lg to length of new_people
repeat with a from 1 to lg
set the_pers to person (item a of new_people)
add the_pers to group ungrouped_tag
end repeat
save
end tell
on group_tag(all_groups)
set new_grouping to {}
repeat with a in all_groups
if "-" is in a then
set all_words to my find_rep(a)
repeat with b in all_words
copy b to end of new_grouping
end repeat
else
copy a to end of new_grouping
end if
end repeat
set all_groups to my tagSort(new_grouping)
activate
set the_group to choose from list all_groups & del & "New." with prompt "Choose Group..."
if the_group is false then return false
if (the_group as text) begins with "New" then
try
set set_group to the text returned of (display dialog "Choose a group for the selected people" default answer "")
on error
return false
end try
else
set set_group to the_group as text
end if
return set_group
end group_tag
on find_rep(T)
set tid to text item delimiters
set text item delimiters to "-"
set T to text items of T
set text item delimiters to tid
return T
end find_rep
to tagSort(L) -- a general purpose simple sort; not enough tags to get fancy.
considering case
set sl to {}
set IL to {}
repeat (count L) times
set the low_item to ""
repeat with i from 1 to (count L)
if i is not in the IL then
set this_item to item i of L as text
if the low_item is "" then
set the low_item to this_item
set the low_item_index to i
else if this_item comes before the low_item then
set the low_item to this_item
set the low_item_index to i
end if
end if
end repeat
set the end of sl to the low_item
set the end of the IL to the low_item_index
end repeat
end considering
return sl
end tagSort