Contacts db in Database Exchange

There were circumstances that could cause the synch FROM Apple Contacts to be missing expected elements. Below is an improved procedure that replaces the contents of “.GetContact”

Local lvResult, lvFName, lvLName, lvErrors
lvErrors = ""

If fgSyncPick contains "THIS"
    ShortCall DoScript
    nsnotify ?(fgResult contains "~","Synch for "+Contact+" Completed",fgResult)
    Stop
EndIf

Alert 1014 "Synch the "+pattern(info("Selected"),"#,")+" Selected Contacts from the Apple Contacts?"
If info("DialogTrigger") = "No" Stop EndIf

FirstRecord
Loop
    ShortCall DoScript
    If fgResult notcontains "~"
        lvErrors = sandwich("",lvErrors,¶)+Contact+" - "+fgResult
    EndIf
    DownRecord
Until Info("Stopped") = -1    

Beep
If lvErrors ≠ ""
    SuperAlert lvErrors,{height=400 width=400 size=9 scroll="yes" buttons="Ok"}
Else
    nsnotify "Synch completed."
EndIf

Rtn

DoScript:
lvLName = ?(Contact≠"",?(Contact contains " ",Contact[" ",-1]["A-Z,a-z",-1],Contact),"")
lvLName = ?(length(lvLName[".",-1]["A-Z",-1])>0,lvLName[".",-1]["A-Z",-1],lvLName)
lvLName = ?(length(lvLName[1,"- "])>4 and lvLName[-1,-1]≠".",lvLName[" ",-1]["A-Z",-1],lvLName)
lvLName = ?(Contact contains "  ",extract(Contact,"  ",2),lvLName)
lvFName =strip( replace(Contact,lvLName,""))
;lvResult = "No Entry was found in the Apple Contacts for " +Contact

FileGlobal fgScript, fgResult
fgResult = ""

fgScript = “AppleScript |||
    tell application "Contacts"
	set theWho to "”+Contact+“"
	try
		set theContact to first person whose (name is theWho and organization is "”+Company+“")
	on error
		try
			set theContact to first person whose (name is theWho)
		on error
			display notification "No Entry was found in the Apple Contacts for " & theWho
			set theResult to "No Entry was found in the Apple Contacts for " & theWho
			return theResult 
		end try
	end try
	
	set selection to theContact
	set theName to name of theContact
	try
		set theOrg to organization of theContact
	on error
		set theOrg to ""
	end try
	try
		set Pos to the job title of theContact
	on error
		set Pos to ""
	end try
	try
		set Memo1 to (label of first phone) of theContact
	on error
		set Memo1 to ""
	end try
	try
		set Phone1 to (value of first phone) of theContact
	on error
		set Phone1 to ""
	end try
	try
		set Memo2 to (label of second phone) of theContact
	on error
		set Memo2 to ""
	end try
	try
		set Phone2 to (value of second phone) of theContact
	on error
		set Phone2 to ""
	end try
	try
		set Memo3 to (label of third phone) of theContact
	on error
		set Memo3 to ""
	end try
	try
		set Phone3 to (value of third phone) of theContact
	on error
		set Phone3 to ""
	end try
	try
		set Memo4 to (label of fourth phone) of theContact
	on error
		set Memo4 to ""
	end try
	try
		set Phone4 to (value of fourth phone) of theContact
	on error
		set Phone4 to ""
	end try
	try
		set Memo5 to (label of fifth phone) of theContact
	on error
		set Memo5 to ""
	end try
	try
		set Phone5 to (value of fifth phone) of theContact
	on error
		set Phone5 to ""
	end try
	try
		set Email1 to (value of first email) of theContact
	on error
		set Email1 to ""
	end try
	try
		set Email2 to (value of second email) of theContact
	on error
		set Email2 to ""
	end try
	try
		set Email3 to (value of third email) of theContact
	on error
		set Email3 to ""
	end try
	try
		set web to (value of first url) of theContact
	on error
		set web to ""
	end try
	try
		set AddSt to (street of first address) of theContact
	on error
		set AddSt to ""
	end try
	try
		set Cty to (city of first address) of theContact
	on error
		set Cty to ""
	end try
	try
		set Stt to (state of first address) of theContact
	on error
		set Stt to ""
	end try
	try
		set ZC to (zip of first address) of theContact
	on error
		set ZC to ""
	end try
	try
		set Cntry to (country of first address) of theContact
	on error
		set Cntry to ""
	end try
	try
		set Cmmnt to (note) of theContact
	on error
		set Cmmnt to ""
	end try
end tell

return theWho & "~" & theOrg & "~" & Pos & "~" & Memo1 & "~" & Phone1 & "~" & Memo2 & "~" & Phone2 & "~" & Memo3 & "~" & Phone3 & "~" & Memo4 & "~" & Phone4 & "~" & Memo5 & "~" & Phone5 & "~" & Email1 & "~" & Email2 & "~" & web & "~" & AddSt & "~" & Cty & "~" & Stt & "~" & ZC & "~" & Cntry & "~" & Cmmnt & "~"
|||,fgResult”

Execute fgScript

If fgResult beginswith “"No Entry” or fgResult = ""
    Rtn
Else


    If fgResult beginswith “"” fgResult = fgResult[2,-1] EndIf
    fgResult  = replace(arrayrange(fgResult,2,100,"~"),"missing value","")
    Local lvProcesor
    Company = array(fgResult,1,"~")
    Title = array(fgResult,2,"~")
    «Memo1» = array(fgResult,3,"~")
    lvProcesor = striptonum(array(fgResult,4,"~"))
    If lvProcesor = ""
        «Area Code1» = ""
        «Phone1» = ""
    Else
        «Area Code1» = sandwich("(",lvProcesor[1,-8],")")
        «Phone1» = lvProcesor[-7,-5]+"-"+lvProcesor[-4,-1]
    EndIf
    «Memo2» = array(fgResult,5,"~")
    lvProcesor = striptonum(array(fgResult,6,"~"))
    If lvProcesor = ""
        «Area Code2» = ""
        «Phone2» = ""
    Else
        «Area Code2» = sandwich("(",lvProcesor[1,-8],")")
        «Phone2» = lvProcesor[-7,-5]+"-"+lvProcesor[-4,-1]
    EndIf
    «Memo3» = array(fgResult,7,"~")
    lvProcesor = striptonum(array(fgResult,8,"~"))
    If lvProcesor = ""
        «Area Code3» = ""
        «Phone3» = ""
    Else
        «Area Code3» = sandwich("(",lvProcesor[1,-8],")")
        «Phone3» = lvProcesor[-7,-5]+"-"+lvProcesor[-4,-1]
    EndIf
    «Memo4» = array(fgResult,9,"~")
    lvProcesor = striptonum(array(fgResult,10,"~"))
    If lvProcesor = ""
        «Area Code4» = ""
        «Phone4» = ""
    Else
        «Area Code4» = sandwich("(",lvProcesor[1,-8],")")
        «Phone4» = lvProcesor[-7,-5]+"-"+lvProcesor[-4,-1]
    EndIf
    «Memo5» = array(fgResult,11,"~")
    lvProcesor = striptonum(array(fgResult,12,"~"))
    If lvProcesor = ""
        «Area Code5» = ""
        «Phone5» = ""
    Else
        «Area Code5» = sandwich("(",lvProcesor[1,-8],")")
        «Phone5» = lvProcesor[-7,-5]+"-"+lvProcesor[-4,-1]
    EndIf
    
    «Email Addresses» = strip(array(fgResult,13,"~")+¶+array(fgResult,14,"~"))
    
    URL = array(fgResult,15,"~")
    Address = array(fgResult,16,"~")
    City = array(fgResult,17,"~")

    State = array(fgResult,18,"~")
    «Zip Code» = array(fgResult,19,"~")
    Country = array(fgResult,20,"~")

    Comments  = array(fgResult,21,"~")
    ShowPage 
EndIf