Platforms to show: All Mac Windows Linux Cross-Platform
/MacCocoa/Addressbook/Add person
Required plugins for this example: MBS MacBase Plugin, MBS Main Plugin, MBS MacCocoa Plugin, MBS Images Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /MacCocoa/Addressbook/Add person
This example is the version from Sat, 8th Mar 2013.
Project "Add person.xojo_binary_project"
Class Window1 Inherits Window
Control StaticText1 Inherits Label
ControlInstance StaticText1 Inherits Label
End Control
Control StaticText2 Inherits Label
ControlInstance StaticText2 Inherits Label
End Control
Control StaticText3 Inherits Label
ControlInstance StaticText3 Inherits Label
End Control
Control GroupBox1 Inherits GroupBox
ControlInstance GroupBox1 Inherits GroupBox
End Control
Control StaticText4 Inherits Label
ControlInstance StaticText4 Inherits Label
End Control
Control StaticText5 Inherits Label
ControlInstance StaticText5 Inherits Label
End Control
Control StaticText6 Inherits Label
ControlInstance StaticText6 Inherits Label
End Control
Control emailwork Inherits TextField
ControlInstance emailwork Inherits TextField
End Control
Control emailhome Inherits TextField
ControlInstance emailhome Inherits TextField
End Control
Control PopupEmail Inherits PopupMenu
ControlInstance PopupEmail Inherits PopupMenu
End Control
Control first Inherits TextField
ControlInstance first Inherits TextField
End Control
Control last Inherits TextField
ControlInstance last Inherits TextField
End Control
Control company Inherits TextField
ControlInstance company Inherits TextField
End Control
Control homepage Inherits TextField
ControlInstance homepage Inherits TextField
End Control
Control GroupBox2 Inherits GroupBox
ControlInstance GroupBox2 Inherits GroupBox
End Control
Control StaticText7 Inherits Label
ControlInstance StaticText7 Inherits Label
End Control
Control StaticText8 Inherits Label
ControlInstance StaticText8 Inherits Label
End Control
Control StaticText9 Inherits Label
ControlInstance StaticText9 Inherits Label
End Control
Control PhoneWork Inherits TextField
ControlInstance PhoneWork Inherits TextField
End Control
Control PhoneHome Inherits TextField
ControlInstance PhoneHome Inherits TextField
End Control
Control StaticText10 Inherits Label
ControlInstance StaticText10 Inherits Label
End Control
Control PhoneMobile Inherits TextField
ControlInstance PhoneMobile Inherits TextField
End Control
Control StaticText11 Inherits Label
ControlInstance StaticText11 Inherits Label
End Control
Control PhonePager Inherits TextField
ControlInstance PhonePager Inherits TextField
End Control
Control PopupPhone Inherits PopupMenu
ControlInstance PopupPhone Inherits PopupMenu
End Control
Control StaticText12 Inherits Label
ControlInstance StaticText12 Inherits Label
End Control
Control PushButton1 Inherits PushButton
ControlInstance PushButton1 Inherits PushButton
EventHandler Sub Action()
Add
End EventHandler
End Control
Control PushButton2 Inherits PushButton
ControlInstance PushButton2 Inherits PushButton
EventHandler Sub Action()
dim n as integer
n=rnd*100
first.text="first"
last.text="last"+str(n)
company.text="company inc."
homepage.text="http://homepage"
emailhome.text="email@home.test"
emailwork.text="email@work.test"
PopupEmail.ListIndex=1
PhoneHome.text="phone home"
PhoneWork.text="phone work"
PhonePager.text="phone pager"
PhoneMobile.text="phone mobile"
PopupPhone.ListIndex=1
End EventHandler
End Control
Protected Sub add()
dim p as ABPersonMBS
dim m as ABMutableMultivalueMBS
dim homeid,workid,mobileid,pagerid,s as string
dim o as string
dim d as Dictionary
a=new ABAddressBookMBS
// search for duplicte
// 1. by Email
dim s1 as ABSearchElementMBS = a.searchElementForPersonProperty(a.kABEmailProperty, "", "", emailwork.text, a.kABEqualCaseInsensitive)
dim s2 as ABSearchElementMBS = a.searchElementForPersonProperty(a.kABEmailProperty, "", "", emailhome.text, a.kABEqualCaseInsensitive)
dim s3 as ABSearchElementMBS = ABSearchElementMBS.searchElementForConjunction(ABSearchElementMBS.kABSearchOr, array(s1,s2))
dim records() as ABRecordMBS = a.recordsMatchingSearchElement(s3)
if UBound(records)>=0 then
MsgBox "This person exists already! (with same email)"
if records(0) isa ABPersonMBS then
p = ABPersonMBS(records(0))
call p.ShowInAddressbook
Return
end if
end if
// 2. by Name
dim s4 as ABSearchElementMBS = a.searchElementForPersonProperty(a.kABFirstNameProperty, "", "", first.text, a.kABEqualCaseInsensitive)
dim s5 as ABSearchElementMBS = a.searchElementForPersonProperty(a.kABLastNameProperty, "", "", last.text, a.kABEqualCaseInsensitive)
dim s6 as ABSearchElementMBS = ABSearchElementMBS.searchElementForConjunction(ABSearchElementMBS.kABSearchAnd, array(s4,s5))
records = a.recordsMatchingSearchElement(s6)
if UBound(records)>=0 then
MsgBox "This person exists already! (with same name)"
if records(0) isa ABPersonMBS then
p = ABPersonMBS(records(0))
call p.ShowInAddressbook
Return
end if
end if
// make new entry
p=new ABPersonMBS
call p.setValue first.text,a.kABFirstNameProperty
o=p.valueForProperty(a.kABFirstNameProperty)
call p.SetValue last.text,a.kABLastNameProperty
call p.SetValue company.text,a.kABOrganizationProperty
call p.SetValue homepage.text,a.kABHomePageProperty
m=new ABMutableMultiValueMBS
if m.Handle=0 then
MsgBox "Failed to create a valid ABMutableMultiValueMBS object."
end if
homeid=m.addValue(emailhome.text,a.kABEmailHomeLabel)
workid=m.addValue(emailwork.text,a.kABEmailWorkLabel)
select case popupemail.listindex
case 1
call m.setprimaryIdentifier homeid
case 2
call m.setprimaryIdentifier workid
end select
call p.setValue m,a.kABEmailProperty
m=nil
m=new ABMutableMultiValueMBS
homeid =m.addValue(phonehome.text, a.KABPhoneHomeLabel)
workid =m.addValue(phonework.text, a.KABPhoneWorkLabel)
mobileid=m.addValue(phoneMobile.text, a.kABPhoneMobileLabel)
pagerid =m.addValue(phonepager.text, a.KABPhonePagerLabel)
select case popupphone.listindex
case 1
call m.setprimaryIdentifier homeid
case 2
call m.setprimaryIdentifier workid
case 3
call m.setprimaryIdentifier mobileid
case 4
call m.setprimaryIdentifier pagerid
end select
call p.setValue m,a.kABPhoneProperty
m=nil
// add an address
d=new Dictionary
d.Value(a.kABAddressCityKey)="Berlin"
d.Value(a.kABAddressCountryKey)="Germany"
d.Value(a.kABAddressStateKey)="New York"
d.Value(a.kABAddressStreetKey)="Sixth Avenue 5"
d.Value(a.kABAddressCountryCodeKey)="de"
d.Value(a.kABAddressZIPKey)="12345"
m=new ABMutableMultiValueMBS
homeid=m.addValue(d,a.kABAddressHomeLabel)
call m.setPrimaryIdentifier(homeid)
call p.setValue m,a.kABAddressProperty
m=nil
// add image
dim logo as Picture = LogoMBS(500)
dim jpeg as string = PictureToJPEGStringMBS(logo, 80)
call p.setImageData jpeg
// now add it
if a.addRecord(p) then
if a.save then
call p.ShowInAddressbook
else
msgBox "Addressbook could not be saved."
end if
else
msgBox "Person could not be added."
end if
End Sub
Property Protected a As abaddressBookMBS
End Class
MenuBar Menu
MenuItem UntitledMenu3 = ""
MenuItem UntitledMenu2 = "File"
MenuItem FileQuit = "Quit"
MenuItem UntitledMenu0 = "Edit"
MenuItem EditUndo = "Undo"
MenuItem UntitledMenu1 = "-"
MenuItem EditCut = "Cut"
MenuItem EditCopy = "Copy"
MenuItem EditPaste = "Paste"
MenuItem EditClear = "Clear"
End MenuBar
Class App Inherits Application
EventHandler Sub Open()
if TargetMachO=false then
MsgBox "This example needs a MachO target running on Mac OS X."
quit
end if
End EventHandler
End Class
End Project
See also:
The items on this page are in the following plugins: MBS MacCocoa Plugin.