Modified Entourage Address Book List X

Okay, so I’m a total newbie - therefore this script is not very polished - it’s Address Book List X [v1.0.0]
by Paul Berkowitz at it’s core, with 2 rounds of PB Progress Bar by Bruce Phillips mashed in to monitor the data build and the Entourage Note creation.

I added the ability to access all the Instant Messenger data fields and modified their output into the Note so it fit what I was trying to do. The code is fairly heavily commented throughout so you can see what I added and modified if you compare Paul’s original side by side (I did leave all the orig coding in place - just put the code I didn’t want to use behind comment marks (–).

Both files are modified, and you’ll need to install Progress Bar, and modify the PB code in Address Book script to locate the progress bar folder.

I built this to allow me to get Palm Desktop data that had been mickey moused for years into something I could edit/massage/make useable again. The IM fields were used, since there’s no limit to them, to handle stacks upon stacks of phone number custom labels and phone numbers w/ names and extensions that were stuck into various Palm Custom/Nicknames/Title, etc fields.

I hope someone will find this useful - either as a perfect example of how NOT to script :smiley: or perhaps as an example of how forgiving AS really is…or to actually create something even more usefull!

Cheers!
Michael
ps, there are 2 scripts here:

Address Book List X - modified:



(* Address Book List X  [v1.0.0] 
See the Readme for details. 

Paul Berkowitz <berkowit28@mac.com>  *)

--with new coding added by Michael Leister (mremjayel-bbs@yahoo.com), 10/2006. That's a throw away email address, so don't bother using it if you're a spammer! I'm a noob, so the coding is messy and prolly inefficient, YMMV!
--       PB Progress Bar by Bruce Phillips woven into this script as well, so that script needs to be d/l'd as well.

property init : false
property surnameLast : true
property whichFields : {"   Default Address (Home or Work)", "   Default Email Address Only", "Home Phone", "Work Phone"}
property whichFormat : "Run-On"
property emptyLine : true
property w : 80
property reverting : false
property oldSurnameLast : true
property oldWhichFields : {"   Default Address (Home or Work)", "   Default Email Address Only", "Home Phone", "Work Phone"}
property oldWhichFormat : "Run-On"
property oldEmptyLine : ""
property oldw : 80

if not init then
	beep 2
	display dialog "By default, the script will print Last Name, First Name, default postal address, default email address, home phone and work phone, in run-on format." & return & return & "To have a full choice of fields and format, run the PREFS script first." buttons {"Cancel", "Continue"} with icon 2
	set init to true
end if

tell application "Microsoft Entourage"
	activate
	set whichOnes to (choose from list {"Selected Contacts", "Flagged Contacts", "By Category", "All Contacts"} with prompt "Include ") as string
	
	if whichOnes = "false" then return
	--hacked by mjl - removed following line
	--display dialog "Please wait a few moments while the script collates the contacts and makes the Address Book List." with icon 1
	
	if whichOnes = "Selected Contacts" then
		try
			set theContacts to the selection as list
			if class of item 1 of theContacts ≠ contact and (count theContacts) = 1 then error number -128
		on error
			beep
			display dialog "! You must first select some contacts in the Address Book or a Custom View, and leave its window in the front, if you wish to use the \"Selected Contacts\" option." buttons {"OK"} default button "OK" with icon 0
			return
		end try
	else if whichOnes = "By Category" then
		set categoryList to ({"None"} & (name of every category))
		set catName to (choose from list categoryList with prompt "Print contacts of which category?") as string
		if catName = "false" then
			return
		else if catName = "None" then
			set theContacts to every contact where its category is {}
		else
			set theContacts to every contact where its category contains {category catName}
		end if
		
	else if whichOnes = "Flagged Contacts" then
		set theContacts to every contact whose flagged is true
	else
		set theContacts to every contact
	end if
	--**********PROGRESS BAR HACK - COLLECTING THE DATA, 1of3
	
	-- initialize BP Progress
	---------------------
	-- **** the next line must be edited to point to wherever you actually put ProgressBar. ****
	---------------------
	set ProgressBar to load script alias (((path to scripts folder) as text) & "BP Progress Bar:BP Progress Bar Controller.scpt")
	tell ProgressBar to initialize("Address Book List X") -- put name of script here (same as title bar of script)
	
	-- Start of Script to use ProgressBar Controller
	tell ProgressBar
		barberPole(true)
		setStatusTop to "Preparing for Note creation"
		setStatusBottom to "Only a moment..."
	end tell
	
	--end 1of3
	--*************begin 2of3
	-- Stop the barber pole, set up for the progress bar
	tell ProgressBar
		barberPole(false)
		setMax to count of items in theContacts -- to match the items to be processed below
		setStatusTop to "Processing Contacts"
	end tell
	-- Run the loop that collects each item
	
	-- end 2of3
	
	tell address book 1
		
		set {custPhoneName1, custPhoneName2, custPhoneName3, custPhoneName4, custFieldName1, custFieldName2, custFieldName3, custFieldName4, custFieldName5, custFieldName6, custFieldName7, custFieldName8, custDateName1, custDateName2} to {custom phone number one name as Unicode text, custom phone number two name as Unicode text, custom phone number three name as Unicode text, custom phone number four name as Unicode text, custom field one name as Unicode text, custom field two name as Unicode text, custom field three name as Unicode text, custom field four name as Unicode text, custom field five name as Unicode text, custom field six name as Unicode text, custom field seven name as Unicode text, custom field eight name as Unicode text, custom date field one name as Unicode text, custom date field two name as Unicode text}
		
	end tell
	
	
	set ls to result
	repeat with i from 1 to 14
		set anItem to item i of ls
		if anItem ends with ":" then set item i of ls to text 1 thru -2 of anItem
	end repeat
	set {custPhoneName1, custPhoneName2, custPhoneName3, custPhoneName4, custFieldName1, custFieldName2, custFieldName3, custFieldName4, custFieldName5, custFieldName6, custFieldName7, custFieldName8, custDateName1, custDateName2} to ls
	
	
	if custPhoneName1 = "Custom 1" then set custPhoneName1 to "Custom Phone 1"
	if custPhoneName2 = "Custom 2" then set custPhoneName2 to "Custom Phone 2"
	if custPhoneName3 = "Custom 3" then set custPhoneName3 to "Custom Phone 3"
	if custPhoneName4 = "Custom 4" then set custPhoneName4 to "Custom Phone 4"
	
	
	set whichFields to my ReCustomize(whichFields, custPhoneName1, custPhoneName2, custPhoneName3, custPhoneName4, custFieldName1, custFieldName2, custFieldName3, custFieldName4, custFieldName5, custFieldName6, custFieldName7, custFieldName8, custDateName1, custDateName2)
	
	
	set addrList to {}
	repeat with n from 1 to count (my theContacts)
		
		set theContact to item n of my theContacts
		repeat 1 times
			
			if class of theContact ≠ contact then
				try
					set theClass to class of theContact as text
				on error
					set theClass to "[unknown object]"
				end try
				try
					set theName to name of theContact
				on error
					try
						set theName to subject of theContact
					on error
						set theName to "[unknown name]"
					end try
				end try
				
				if n ≠ (count theContacts) then
					set p to "The script will continue on to the next item(s), or you can cancel."
				else
					set p to "(This was the last selected item.)"
				end if
				
				beep
				display dialog "The selected item \"" & theName & "\" is a " & theClass & ", not a contact." & return & return & p with icon 2
				
				exit repeat
			end if
			
			try
				
				tell theContact
					set theBug to "Name"
					set lName to last name as Unicode text
					set fName to first name as Unicode text
					
					if whichFields contains {"Title"} then
						set theBug to "Title"
						set theTitle to title as Unicode text
						if theTitle ≠ "" then
							if fName ≠ "" then
								set fName to (theTitle & " " & fName) as Unicode text
							else -- no first name, to read as "Mr. Smith"	
								set fName to theTitle
							end if
						end if
					end if
					
					if whichFields contains {"Suffix"} then
						set theBug to "Suffix"
						set theSuffix to suffix as Unicode text
						if theSuffix ≠ "" then
							if lName ≠ "" then
								if fName ≠ "" then
									set lName to (lName & ", " & theSuffix) as Unicode text --
								else -- no comma, will be read as first name
									set lName to (lName & " " & theSuffix) as Unicode text
								end if
							else -- no last name, to read as "Joe Jr." - no comma	
								set fName to (fName & " " & theSuffix) as Unicode text
							end if
						end if
					end if
					
					set theBug to "Name 2"
					if lName ≠ "" and fName ≠ "" then
						set revListingN to (lName & "" & fName & "") as Unicode text -- reversed with signs						
					else
						set revListingN to name as Unicode text -- may be ""
					end if
					
					
					
					if whichFields contains {"Nickname"} then
						set theBug to "Nickname"
						set theNickname to nickname as Unicode text
						if theNickname ≠ "" then
							if revListingN ≠ "" then
								set revListingN to (revListingN & " \"" & theNickname & "\"") as Unicode text
							else -- nickname only
								set revListingN to ("\"" & theNickname & "\"") as Unicode text
							end if
						end if
					end if
					
					set theBug to "Comp/Dept/Job"
					set {theComp, theDept, theJobTitle} to {company as Unicode text, department as Unicode text, job title as Unicode text}
					if revListingN = "" then -- company listing?				
						if theComp ≠ "" then -- if a company name is listed
							set revListingN to theComp
							if whichFields contains {"Job Title"} and theJobTitle ≠ "" then
								if whichFields contains {"Department"} and theDept ≠ "" then
									set revListingN to (revListingN & "  (" & theJobTitle & ", " & theDept & ")") as Unicode text
								else
									set revListingN to (revListingN & "  (" & theJobTitle & ")") as Unicode text
								end if
							else if whichFields contains {"Department"} and theDept ≠ "" then
								set revListingN to (revListingN & "  (" & theDept & ")") as Unicode text
							end if
						else
							try
								set revListingN to (("<" & (default email address) as string) & ">  ") as Unicode text
							end try
						end if
						
					else -- name listed
						
						set jobDept to ""
						if whichFields contains {"Job Title"} and theJobTitle ≠ "" then
							set jobDept to theJobTitle
							if whichFields contains {"Department"} and theDept ≠ "" then set jobDept to (jobDept & ", " & theDept) as Unicode text
						else if whichFields contains {"Department"} and theDept ≠ "" then
							set jobDept to theDept as Unicode text
						end if
						if jobDept ≠ "" then
							if whichFormat = "Run-On" then
								set revListingN to (revListingN & ",  " & jobDept) as Unicode text
							else
								set revListingN to (revListingN & return & "      " & jobDept) as Unicode text
							end if
						end if
						
						if whichFields contains {"Company"} and theComp ≠ "" and theComp ≠ name as Unicode text then
							if whichFormat = "Run-On" then
								set revListingN to (revListingN & ",  " & theComp) as Unicode text
							else
								set revListingN to (revListingN & return & "      " & theComp) as Unicode text
							end if
						end if
						
					end if
					
					if whichFields contains {"   Default Address (Home or Work)"} and not (whichFields contains {"Home Address"} and whichFields contains {"Work Address"}) then
						set theBug to "Default Address"
						if default postal address = home then
							set defAddress to home address
							set addrLabel to "Home Address: "
						else
							set defAddress to business address
							set addrLabel to "Work Address: "
						end if
						set postalAddress to my postAddress(defAddress)
						
						if postalAddress ≠ "" then
							if whichFormat = "Run-On" then
								set revListingN to (revListingN & ",  " & addrLabel & postalAddress)
							else
								set revListingN to (revListingN & return & "      " & addrLabel & postalAddress) as Unicode text
							end if
						end if
					end if
					
					if whichFields contains {"Home Address"} then
						set theBug to "Home Address"
						set hmAddress to home address
						set postalAddress to my postAddress(hmAddress)
						if postalAddress ≠ "" then
							if whichFormat = "Run-On" then
								set revListingN to (revListingN & ",  " & "Home Address: " & postalAddress) as Unicode text
							else
								set revListingN to (revListingN & return & "      " & "Home Address: " & postalAddress) as Unicode text
							end if
						end if
					end if
					
					if whichFields contains {"Work Address"} then
						set theBug to "Work Address"
						set wkAddress to business address
						set postalAddress to my postAddress(wkAddress)
						if postalAddress ≠ "" then
							if whichFormat = "Run-On" then
								set revListingN to (revListingN & ",  " & "Work Address: " & postalAddress) as Unicode text
							else
								set revListingN to (revListingN & return & "      " & "Work Address: " & postalAddress) as Unicode text
							end if
						end if
					end if
					
					
					if whichFields contains {"   Default Email Address Only"} and whichFields does not contain {"All Email Addresses"} then
						set theBug to "Default Email"
						try
							set defEmail to (("Email Address: <" & (default email address) as string) & ">") as Unicode text -- only if exists
							if whichFormat = "Run-On" then
								set revListingN to (revListingN & ",  " & defEmail) as Unicode text
							else
								set revListingN to (revListingN & return & "      " & defEmail) as Unicode text
							end if
						end try
					end if
					
					if whichFields contains {"All Email Addresses"} then
						set theBug to "All Email"
						try
							
							set defEmail to (("<" & (default email address) as string) & ">") as Unicode text -- only if exists
							set allEmails to ("Email Address: " & defEmail) as Unicode text
							repeat with i from 1 to (count email addresses)
								set anotherEmailAddress to email address i
								if anotherEmailAddress ≠ default email address then set allEmails to (allEmails & ", <" & anotherEmailAddress & ">") as Unicode text
							end repeat
							if whichFormat = "Run-On" then
								set revListingN to (revListingN & ",  " & allEmails) as Unicode text
							else
								set revListingN to (revListingN & return & "      " & allEmails) as Unicode text
							end if
						end try
					end if
					
					--mjl's added hack for data imported to Ent IM fields (w/ unique formatting)
					--this section is essentially the email addresses section (above) copy/pasted
					--so, to see actual changes, compare the two blocks of code
					
					if whichFields contains {"All Instant Message Addresses"} then --"all IM's" is the only choice
						set theBug to "All IM"
						try
							
							set defIM to (("<" & (default instant message address) as string) & ">") as Unicode text -- only if exists
							--hack within a hack part 1of2
							--in this particular case, for my formatted output, *replaced* ("IM: " & defIM)	with ("Tel: ")
							set allIMs to "Tel: " as Unicode text
							repeat with i from 1 to (count instant message addresses)
								set anotherIMAddress to instant message address i
								--hack within a hack
								--removed the following line since I want every listing of the IM fields (using some as field names, ie, B, H, C, DD, F (phone labels))
								--if anotherIMAddress ≠ default instant message address then set allIMs to (allIMs & ", <" & anotherIMAddress & ">") as Unicode text
								
								--add this if/then nested jumble to construct the formatted output I want
								if anotherIMAddress is not {} then
									set lg to length of anotherIMAddress
									if lg ≤ 2 and lg > 0 then
										if i > 1 then
											set allIMs to (allIMs & return & "             " & "[" & anotherIMAddress & ":") as Unicode text
										else
											set allIMs to (allIMs & "[" & anotherIMAddress & ":") as Unicode text
										end if
										
									else
										set allIMs to (allIMs & " >" & anotherIMAddress) as Unicode text
									end if
								end if
								
								--end hack within a hack
							end repeat
							if whichFormat = "Run-On" then
								set revListingN to (revListingN & ",  " & allIMs) as Unicode text
							else
								set revListingN to (revListingN & return & "      " & allIMs) as Unicode text
							end if
						end try
					end if
					
					--end mjl's hack
					
					--------------------------------
					
					
					if whichFields contains {"   Default Phone Only (Home or Work)"} and not (whichFields contains {"Home Phone"} and whichFields contains {"Work Phone"}) then
						set theBug to "Default Phone"
						set {hmPhone, wkPhone} to {home phone number as Unicode text, business phone number as Unicode text}
						if default postal address = home then
							set defPhone to hmPhone
							set addrLabel to "Home Phone: "
							if defPhone = "" and wkPhone ≠ "" then
								set defPhone to wkPhone
								set addrLabel to "Work Phone: "
							end if
						else
							set defPhone to wkPhone
							set addrLabel to "Work Phone: "
							if defPhone = "" and hmPhone ≠ "" then
								set defPhone to hmPhone
								set addrLabel to "Home Phone: "
							end if
						end if
						
						if defPhone ≠ "" then
							if whichFormat = "Run-On" then
								set revListingN to (revListingN & ",  " & addrLabel & defPhone) as Unicode text
							else
								set revListingN to (revListingN & return & "      " & addrLabel & defPhone) as Unicode text
							end if
						end if
						
					end if
					
					
					set theBug to "All Phones"
					set allPhoneNumbers to {home phone number as Unicode text, other home phone number as Unicode text, home fax phone number as Unicode text, business phone number as Unicode text, other business phone number as Unicode text, business fax phone number as Unicode text, pager phone number as Unicode text, mobile phone number as Unicode text, main phone number as Unicode text, assistant phone number as Unicode text, custom phone number one as Unicode text, custom phone number two as Unicode text, custom phone number three as Unicode text, custom phone number four as Unicode text}
					
					repeat with i from 1 to 14
						set thisPhoneLabel to item i of {"Home Phone", "Home Phone 2", "Home Fax", "Work Phone", "Work Phone 2", "Work Fax", "Pager", "Mobile Phone", "Main Phone", "Assistant Phone", custPhoneName1, custPhoneName2, custPhoneName3, custPhoneName4}
						
						if whichFields contains {thisPhoneLabel} then
							set theBug to thisPhoneLabel
							set thisPhone to item i of allPhoneNumbers
							if thisPhone ≠ "" then
								if whichFormat = "Run-On" then
									set revListingN to (revListingN & ",  " & thisPhoneLabel & ": " & thisPhone) as Unicode text
								else
									set revListingN to (revListingN & return & "      " & thisPhoneLabel & ": " & thisPhone) as Unicode text
								end if
							end if
						end if
					end repeat
					
					
					-- end phones
					set theBug to "Other Fields"
					set allOtherFields to {custom field one as Unicode text, custom field two as Unicode text, custom field three as Unicode text, custom field four as Unicode text, custom field five as Unicode text, custom field six as Unicode text, custom field seven as Unicode text, custom field eight as Unicode text, birthday as Unicode text, age as Unicode text, anniversary as Unicode text, spouse as Unicode text, children, astrology sign as Unicode text, interests as Unicode text, custom date field one as Unicode text, custom date field two as Unicode text, description as Unicode text}
					
					repeat with i from 1 to 18
						set thisCustomField to item i of {custFieldName1, custFieldName2, custFieldName3, custFieldName4, custFieldName5, custFieldName6, custFieldName7, custFieldName8, "Birthday", "Age", "Anniversary", "Spouse", "Children", "Astrology Sign", "Interests", custDateName1, custDateName2, "Notes"}
						
						if whichFields contains {thisCustomField} then
							set theBug to thisCustomField
							set thisFieldValue to item i of allOtherFields
							if thisCustomField = "Children" then
								set AppleScript's text item delimiters to {", "}
								set thisFieldValue to thisFieldValue as string
								set AppleScript's text item delimiters to {""}
							end if
							if thisFieldValue ≠ "" then
								if whichFormat = "Run-On" then
									set revListingN to (revListingN & ",  " & thisCustomField & ": " & thisFieldValue) as Unicode text
								else
									set revListingN to (revListingN & return & "      " & thisCustomField & ": " & thisFieldValue) as Unicode text
								end if
							end if
						end if
					end repeat
					
				end tell -- (theContact)	
				----------------------------------------
				
				set revListingNN to ""
				set theBug to "Paragraphs"
				repeat with j from 1 to (count paragraphs of revListingN)
					set apara to paragraph j of revListingN -- if "Run-On ", only one para
					
					if (count apara) > w then
						set theLine to apara
						set newLines to ""
						set AppleScript's text item delimiters to {" "}
						repeat while (count theLine) > w
							set firstPart to text 1 thru w of theLine
							try
								if character (w + 1) of theLine ≠ " " then
									set {newLines, leftOver} to {(newLines & (text items 1 thru -2 of firstPart) & return) as Unicode text, ("      " & text item -1 of firstPart) as Unicode text}
									set theLine to leftOver & text (w + 1) thru -1 of theLine
								else
									set {newLines, leftOver} to {(newLines & firstPart & return) as Unicode text, ("      ") as Unicode text}
									try
										if character (w + 2) of theLine ≠ " " then
											set theLine to leftOver & text (w + 2) thru -1 of theLine
										else
											set theLine to leftOver & text (w + 3) thru -1 of theLine
										end if
									on error -- just one space left hanging at end
										set theLine to ""
									end try
								end if
							on error -- no space breaks
								set {newLines, leftOver} to {(newLines & firstPart & return) as Unicode text, ("      ") as Unicode text}
								set theLine to leftOver & text (w + 1) thru -1 of theLine
							end try
							if whichFormat = "Separate Lines" then set theLine to ("      " & theLine) as Unicode text -- 2nd indent
							
						end repeat
						set AppleScript's text item delimiters to {""}
						set apara to newLines & theLine
					end if
					
					if revListingNN ≠ "" then set revListingNN to (revListingNN & return) as Unicode text
					set revListingNN to (revListingNN & apara) as Unicode text
					
				end repeat
				
				if emptyLine is "Yes" then set revListingNN to (revListingNN & return) as Unicode text
				
				set end of addrList to revListingNN
				
			on error errMsg
				beep
				if n ≠ (count theContacts) then
					set p to "The script will continue on to the next contact(s), or you can cancel."
				else
					set p to "(This was the last contact.)"
				end if
				try
					set theName to (name of theContact) as Unicode text
				on error
					set theName to "[No contact name.]"
				end try
				display dialog errMsg & return & return & "In " & theBug & ": " & theName & return & return & p with icon 2
				
			end try
		end repeat -- 1 times
		--*********** begin 3of3 (progress bar)
		--repeat with j from 1 to count of items in theContacts -- ??Some of the last items in SysDat take too long, looks better short 1 ??
		tell ProgressBar
			setStatusTop to "Collecting " & n
			setStatusBottom to "Contacts " --& item j of SysDat
		end tell
		--set thisDat to do shell script "system_profiler " & item j of SysDat
		--set end of AllData to thisDat
		tell ProgressBar to increase by 1
		--end repeat
		
		--tell ProgressBar to quit
		--AllData as string
		
		--****** end 3of3
	end repeat
	
end tell

------------------------------------------------------

--################ANOTHER MJL HACK - STATUS BAR, POPULATING NOTE - 1of3
-- initialize BP Progress
---------------------
-- **** the next line must be edited to point to wherever you actually put ProgressBar. ****
---------------------
--set ProgressBar to load script alias (((path to scripts folder) as text) & "BP Progress Bar:BP Progress Bar Controller.scpt")
tell ProgressBar to initialize("Address Book List X - Populating Note") -- put name of script here (same as title bar of script)

-- Start of Script to use ProgressBar Controller
tell ProgressBar
	barberPole(true)
	setStatusTop to "Preparing for Note sorting and populating"
	setStatusBottom to "Only a moment..."
end tell

--###### end 1of3

tell application "Microsoft Entourage"
	set alphList to my sort(addrList)
end tell

--##########begin 2of3

-- Stop the barber pole, set up for the progress bar
tell ProgressBar
	barberPole(false)
	setMax to count of items in alphList -- to match the items to be processed below
	setStatusTop to "Sorting and Populating"
end tell
-- Run the loop that collects each item

-- end 2of3


set newList to {}
repeat with i from 1 to (count my alphList)
	set revListing to item i of my alphList
	
	set AppleScript's text item delimiters to {"" as Unicode text}
	if (count of (text items of revListing)) > 2 then -- if there is a reversed last name, first name 		
		set lName to text item 1 of revListing
		set fName to text item 2 of revListing
		
		if surnameLast is true then
			set restoredListing to (lName & ", " & fName & (text items 3 thru -1 of revListing)) as Unicode text
		else
			set restoredListing to (fName & " " & lName & (text items 3 thru -1 of revListing)) as Unicode text
		end if
	else
		set restoredListing to revListing -- if only a single name
	end if
	set AppleScript's text item delimiters to {""}
	
	set end of newList to restoredListing
	--########### begin 30f3
	
	--repeat with i from 1 to count of items in alphList -- ??Some of the last items in SysDat take too long, looks better short 1 ??
	tell ProgressBar
		setStatusTop to "Sorting " & i
		setStatusBottom to "Populating " --& item j of SysDat
	end tell
	--set thisDat to do shell script "system_profiler " & item j of SysDat
	--set end of AllData to thisDat
	tell ProgressBar to increase by 1
	--end repeat
	
	--tell ProgressBar to quit
	--AllData as string
	
	--****** end 3of3
	
end repeat
tell ProgressBar to quit

set AppleScript's text item delimiters to {return as Unicode text}
set AddrBook to newList as Unicode text
set AppleScript's text item delimiters to {""}

set dateFormat to my FormatDate()
set today to my ShortDate(current date, dateFormat) as Unicode text

tell application "Microsoft Entourage"
	
	set addrNote to make new note with properties {name:("Address Book List  -  " & today) as Unicode text, content:("ADDRESS BOOK LIST  -  " & today & return & return & AddrBook & return) as Unicode text}
	open addrNote
	
end tell

beep

if reverting then set {surnameLast, whichFields, whichFormat, emptyLine, w, reverting} to {oldSurnameLast, oldWhichFields, oldWhichFormat, oldEmptyLine, oldw, false}

------------------------------------------------------------------- 


to postAddress(anAddress) -- anAddress is the record, don't need contact reference
	
	tell application "Microsoft Entourage"
		set {streetAddress, theCity, theState, theZip, theCountry} to {anAddress's street address as Unicode text, anAddress's city as Unicode text, anAddress's state as Unicode text, anAddress's zip as Unicode text, anAddress's country as Unicode text}
	end tell
	set postalAddress to streetAddress
	set AppleScript's text item delimiters to {return}
	set streetLines to text items of postalAddress
	set AppleScript's text item delimiters to {", "}
	set postalAddress to streetLines as Unicode text
	set AppleScript's text item delimiters to {""}
	if postalAddress ≠ "" then
		if theCity ≠ "" then set postalAddress to (postalAddress & ", " & theCity) as Unicode text
	else
		if theCity ≠ "" then set postalAddress to theCity
	end if
	if postalAddress ≠ "" then
		if theState ≠ "" then set postalAddress to (postalAddress & ", " & theState) as Unicode text
	else
		if theState ≠ "" then set postalAddress to theState
	end if
	if postalAddress ≠ "" then
		if theZip ≠ "" then set postalAddress to (postalAddress & " " & theZip) as Unicode text
	else
		if theZip ≠ "" then set postalAddress to theZip
	end if
	if postalAddress ≠ "" then
		if theCountry ≠ "" then set postalAddress to (postalAddress & ", " & theCountry) as Unicode text
	else
		if theCountry ≠ "" then set postalAddress to theCountry
	end if
	
	return postalAddress
end postAddress


on sort(theList)
	-- Emmanuel Levy's latest (with credits to SBE & FD)
	set theLength to theList's length
	if theLength = 0 or theLength = 1 then return theList
	if theLength = 2 then
		set {xItem, yItem} to theList
		if xItem > yItem then return {yItem, xItem}
		return theList
	end if
	script listHolder
		property theList : {}
		property lowList : {}
		property highList : {}
	end script
	set listHolder's theList to theList
	set xMiddle to (theLength + 1) div 2
	set xMedian to listHolder's theList's item xMiddle
	repeat with i from 1 to (xMiddle - 1)
		set xItem to listHolder's theList's item i
		if xItem < xMedian then
			set listHolder's lowList's end to xItem
		else
			set listHolder's highList's end to xItem
		end if
	end repeat
	repeat with i from (xMiddle + 1) to theLength
		set xItem to listHolder's theList's item i
		if xItem < xMedian then
			set listHolder's lowList's end to xItem
		else
			set listHolder's highList's end to xItem
		end if
	end repeat
	if listHolder's lowList's length > 1 then set listHolder's lowList to sort(listHolder's lowList)
	if listHolder's highList's length > 1 then set listHolder's highList to sort(listHolder's highList)
	return listHolder's lowList & xMedian & listHolder's highList
end sort


to ReCustomize(whichFields, custPhoneName1, custPhoneName2, custPhoneName3, custPhoneName4, custFieldName1, custFieldName2, custFieldName3, custFieldName4, custFieldName5, custFieldName6, custFieldName7, custFieldName8, custDateName1, custDateName2)
	
	repeat with i from 1 to (count whichFields)
		set aField to item i of whichFields
		
		if aField = "custPhoneName1" then
			set item i of whichFields to custPhoneName1
		else if aField = "custPhoneName2" then
			set item i of whichFields to custPhoneName2
		else if aField = "custPhoneName3" then
			set item i of whichFields to custPhoneName3
		else if aField = "custPhoneName4" then
			set item i of whichFields to custPhoneName4
		else if aField = "custFieldName1" then
			set item i of whichFields to custFieldName1
		else if aField = "custFieldName2" then
			set item i of whichFields to custFieldName2
		else if aField = "custFieldName3" then
			set item i of whichFields to custFieldName3
		else if aField = "custFieldName4" then
			set item i of whichFields to custFieldName4
		else if aField = "custFieldName5" then
			set item i of whichFields to custFieldName5
		else if aField = "custFieldName6" then
			set item i of whichFields to custFieldName6
		else if aField = "custFieldName7" then
			set item i of whichFields to custFieldName7
		else if aField = "custFieldName8" then
			set item i of whichFields to custFieldName8
		else if aField = "custDateName1" then
			set item i of whichFields to custDateName1
		else if aField = "custDateName2" then
			set item i of whichFields to custDateName2
		end if
		
	end repeat
	
	return whichFields
	
	
end ReCustomize

to FormatDate()
	
	get "1/2/3"
	set testDate to date (result)
	
	set theFormat to ""
	repeat with i from 1 to 3 -- as in "1/2/3"
		if i = the day of testDate then
			get "dd"
		else if (i + 2000) = the year of testDate then
			get "yy"
		else
			get "mm"
		end if
		set theFormat to theFormat & the result & "/"
	end repeat
	
	return text 1 thru -2 of theFormat
	
end FormatDate


on ShortDate(theDate, dateFormat) --with thanks to Emmanuel Levy - no time
	
	copy theDate to d
	set day of d to 1
	copy d to f
	set month of f to January
	
	
	if dateFormat = "mm/dd/yy" then
		return "" & (1 + (d - f + 1314900) div 2629800) & "/" & (day of theDate) & "/" & (text -2 thru -1 of ("" & year of theDate))
	else if dateFormat = "dd/mm/yy" then
		return (text -2 thru -1 of ("0" & (day of theDate))) & "/" & (text -2 thru -1 of ("0" & (1 + (d - f + 1314900) div 2629800))) & "/" & (text -2 thru -1 of ("" & year of theDate))
	else if dateFormat = "yy/mm/dd" then
		return "" & year of theDate & "-" & (text -2 thru -1 of ("0" & (1 + (d - f + 1314900) div 2629800))) & "-" & (text -2 thru -1 of ("0" & (day of theDate)))
	else -- some wierd thing
		return (theDate as string) -- leave it in long form
	end if
	
end ShortDate


***PREFs creation file:

(* Address Book List  X PREFS [v1.0.0] 
See the Readme for details. 

Paul Berkowitz <berkowit28@mac.com>  *)

--with minor mods by Michael Leister (mremjayel-bbs@yahoo.com) - 10/06

property mainScript : ""


set mainScript to GetMainScript(mainScript, "Address Book List X")

try
	set f to load script mainScript
on error errMsg
	beep 3
	display dialog "The file you selected	as \"Address Book List X\" cannot be accessed as a script." & return & return & "Either you selected the wrong file, or something is wrong with the file." & return & return & "Press \"See Error\" if you want to report the AppleScript error." buttons {"Cancel", "See Error"} default button 1 with icon 0
	if button returned of result = "Cancel" then
		return
	else
		error errMsg
		return
	end if
end try

--editted - removed following 3 lines from script

--set {surnameLast, whichFields, whichFormat, emptyLine, w, reverting} to f's {surnameLast, whichFields, whichFormat, emptyLine, w, reverting}

--set oldRemember to remember
--set reverting to false


set {oldSurnameLast, oldWhichFields, oldWhichFormat, oldEmptyLine, oldw} to f's {surnameLast, whichFields, whichFormat, emptyLine, w}

tell application "Microsoft Entourage"
	
	tell address book 1
		
		set {custPhoneName1, custPhoneName2, custPhoneName3, custPhoneName4, custFieldName1, custFieldName2, custFieldName3, custFieldName4, custFieldName5, custFieldName6, custFieldName7, custFieldName8, custDateName1, custDateName2} to {custom phone number one name as Unicode text, custom phone number two name as Unicode text, custom phone number three name as Unicode text, custom phone number four name as Unicode text, custom field one name as Unicode text, custom field two name as Unicode text, custom field three name as Unicode text, custom field four name as Unicode text, custom field five name as Unicode text, custom field six name as Unicode text, custom field seven name as Unicode text, custom field eight name as Unicode text, custom date field one name as Unicode text, custom date field two name as Unicode text}
		
	end tell
end tell

set ls to result
repeat with i from 1 to 14
	set anItem to item i of ls
	if anItem ends with ":" then set item i of ls to text 1 thru -2 of anItem
end repeat
set {custPhoneName1, custPhoneName2, custPhoneName3, custPhoneName4, custFieldName1, custFieldName2, custFieldName3, custFieldName4, custFieldName5, custFieldName6, custFieldName7, custFieldName8, custDateName1, custDateName2} to ls


if custPhoneName1 = "Custom 1" then set custPhoneName1 to "Custom Phone 1"
if custPhoneName2 = "Custom 2" then set custPhoneName2 to "Custom Phone 2"
if custPhoneName3 = "Custom 3" then set custPhoneName3 to "Custom Phone 3"
if custPhoneName4 = "Custom 4" then set custPhoneName4 to "Custom Phone 4"

--editted - added "All Instant Message Addresses" to list
--note there is no 'just default IM' option

set allFieldList to {"Title", "Suffix", "Nickname", "Company", "Department", "Job Title", "   Default Address (Home or Work)", "Home Address", "Work Address", "   Default Email Address Only", "All Email Addresses", "All Instant Message Addresses", "   Default Phone Only (Home or Work)", "Home Phone", "Home Phone 2", "Home Fax", "Work Phone", "Work Phone 2", "Work Fax", "Pager", "Mobile Phone", "Main Phone", "Assistant Phone", custPhoneName1, custPhoneName2, custPhoneName3, custPhoneName4, custFieldName1, custFieldName2, custFieldName3, custFieldName4, custFieldName5, custFieldName6, custFieldName7, custFieldName8, "Birthday", "Age", "Anniversary", "Spouse", "Children", "Astrology Sign", "Interests", custDateName1, custDateName2, "Notes"}


display dialog "Do you wish to print contacts' names as " & return & return & "¢ FirstName LastName or" & return & return & "¢ LastName, FirstName?" & return & return & "(In either case, the list of contacts will be alphabetical by last name.)" buttons {" Cancel ", "LastName, FirstName", "FirstName LastName"} default button 3 with icon 1

if button returned of result = "LastName, FirstName" then
	set surnameLast to true
else if button returned of result = "FirstName LastName" then
	set surnameLast to false
else
	display dialog "No changes have been made to any preferences." buttons {"OK"} default button "OK" with icon 2
	return
end if

display dialog "In the next list, choose which fields (if not empty) to print for each contact." & return & return & "(Title, Suffix and Nickname will be included as part of the name.)" & return & return & "You will have an opportunity to save your choice for future print runs." with icon 1

set whichFields to choose from list allFieldList with prompt "Print which fields?  (Command-click each field you want to include.)" with multiple selections allowed

if whichFields = false then
	display dialog "No changes have been made to any preferences." buttons {"OK"} default button "OK" with icon 2
	returnh
end if

set whichFields to GenericCustomize(whichFields, custPhoneName1, custPhoneName2, custPhoneName3, custPhoneName4, custFieldName1, custFieldName2, custFieldName3, custFieldName4, custFieldName5, custFieldName6, custFieldName7, custFieldName8, custDateName1, custDateName2)

display dialog "The print list will indent continuation lines for each contact for clarity. Do you prefer" & return & return & "¢ Run-On Format:  a  single run-on line for each contact, with fields separated by commas, or" & return & return & "¢ Separate Lines Format:  a separate indented line for each field?" buttons {" Cancel ", "Run-On", "Separate Lines"} default button 3 with icon 1
if button returned of result = " Cancel " then
	display dialog "No changes have been made to any preferences." buttons {"OK"} default button "OK" with icon 2
	return
else
	set whichFormat to button returned of result
end if

display dialog "The script is currently set to print " & oldw & " characters per line (suitable for Times Medium or Geneva Smaller)." & return & return & "You may enter a number for a different character count per line (fewer for larger type) to avoid bad line breaks:" default answer ("" & oldw) buttons {" Cancel ", " OK "} with icon 1
if button returned of result = " Cancel " then
	display dialog "No changes have been made to any preferences." buttons {"OK"} default button "OK" with icon 2
	return
else
	try
		set w to (text returned of result) as integer
	on error
		set done to false
		repeat until done
			display dialog "! You have not entered a valid character width." & return & return & "You may enter only an integer (whole number).  Try again." & return & return & "You may enter a number for a different character count per line:" default answer ("" & w) buttons {" Cancel ", " OK "} with icon 2
			if button returned of result = " Cancel " then
				display dialog "No changes have been made to any preferences." buttons {"OK"} default button "OK" with icon 2
				return
			else
				try
					set w to (text returned of result) as integer
					set done to true
				end try
			end if
		end repeat
	end try
end if

display dialog "Leave an empty line between contacts?" & return & return buttons {" Cancel ", "No", "Yes"} default button "Yes" with icon 1
set emptyLine to button returned of result
if emptyLine = " Cancel " then
	display dialog "No changes have been made to any preferences." buttons {"OK"} default button "OK" with icon 2
	return
end if

display dialog "SAVE PREFERENCES?" & return & return & "Do you want the main script to remember these preferences for all future script runs, or use them just once?" & return & return & "You can always reset them or undo them by running this PREFS script again." buttons {" Cancel ", "Don't Save", "Save"} default button "Save" with icon 1

if button returned of result = " Cancel " then
	display dialog "No changes have been made to any preferences." buttons {"OK"} default button "OK" with icon 2
	return
else if button returned of result = "Save" then
	set reverting to false
else
	set reverting to true
end if

set {f's init, f's surnameLast, f's whichFields, f's whichFormat, f's emptyLine, f's w, f's reverting, f's oldSurnameLast, f's oldWhichFields, f's oldWhichFormat, f's oldEmptyLine, f's oldw} to {true, surnameLast, whichFields, whichFormat, emptyLine, w, reverting, oldSurnameLast, oldWhichFields, oldWhichFormat, oldEmptyLine, oldw}

store script f in mainScript replacing yes

beep

display dialog "All done!" & return & return & "You can now use Address Book List with the new settings you've just entered." & return & return & "(Run this PREFS script again any time to reset the preferences." & return & return with icon 1


on GetMainScript(mainScript, scriptName)
	
	if mainScript ≠ "" then
		try
			get mainScript
		on error
			set mainScript to ""
		end try
	end if
	
	if mainScript = "" then
		try
			tell application "Microsoft Entourage" to set MUDpath to (path to MUD as Unicode text)
			set mainScript to alias ((MUDpath & "Entourage Script Menu Items:" & scriptName) as Unicode text)
			
		on error -- renamed, or non-English system	
			set mainScript to choose file of type {"osas"} with prompt "Select your \"" & scriptName & "\" script in the Entourage Script Menu Items folder in Microsoft User Data folder [normally in Documents folder of your UserName folder [\"Home\"] in Users folder of OS X]."
		end try
	end if
	
	return mainScript
	
end GetMainScript


to GenericCustomize(whichFields, custPhoneName1, custPhoneName2, custPhoneName3, custPhoneName4, custFieldName1, custFieldName2, custFieldName3, custFieldName4, custFieldName5, custFieldName6, custFieldName7, custFieldName8, custDateName1, custDateName2)
	
	repeat with i from 1 to (count whichFields)
		set aField to item i of whichFields
		
		if aField = custPhoneName1 then
			set item i of whichFields to "custPhoneName1"
		else if aField = custPhoneName2 then
			set item i of whichFields to "custPhoneName2"
		else if aField = custPhoneName3 then
			set item i of whichFields to "custPhoneName3"
		else if aField = custPhoneName4 then
			set item i of whichFields to "custPhoneName4"
		else if aField = custFieldName1 then
			set item i of whichFields to "custFieldName1"
		else if aField = custFieldName2 then
			set item i of whichFields to "custFieldName2"
		else if aField = custFieldName3 then
			set item i of whichFields to "custFieldName3"
		else if aField = custFieldName4 then
			set item i of whichFields to "custFieldName4"
		else if aField = custFieldName5 then
			set item i of whichFields to "custFieldName5"
		else if aField = custFieldName6 then
			set item i of whichFields to "custFieldName6"
		else if aField = custFieldName7 then
			set item i of whichFields to "custFieldName7"
		else if aField = custFieldName8 then
			set item i of whichFields to "custFieldName8"
		else if aField = custDateName1 then
			set item i of whichFields to "custDateName1"
		else if aField = custDateName2 then
			set item i of whichFields to "custDateName2"
		end if
		
	end repeat
	
	return whichFields
	
	
end GenericCustomize

Model: Imacs, G4’s, G3’s - all manner of fun
AppleScript: 1.10.7
Browser: Safari 419.3
Operating System: Mac OS X (10.4)