Store your ID in URLs in Mac address book
As you may know, we can query address book in our plugins. Now you can store your own IDs for users in the contacts database by using an extra URL.
The key today is that we use an URL in the addressbook and make it a FMP URL, so the user can actually click on the URL and go to the FileMaker database right away. When synchronizing the IDs between database, we can store our own ID this way. To search, we use Addressbook.searchElementForProperty function to search for our label in URLs property and compare to the ID.
To store the value, we use the following script:
Set Variable [ $PersonID ; Value: MBS( "Addressbook.Me") ]
Set Variable [ $myID ; Value: "FMP://~/Clients?script=ShowContact¶m=123456" ]
Set Variable [ $MyLabel ; Value: "FileMakerID" ]
#
# Query current URLs
Set Variable [ $r ; Value: MBS( "Addressbook.record.valueForProperty"; $PersonID; "URLsProperty" ) ]
Set Variable [ $Count ; Value: MBS( "Addressbook.multivalue.count" ) ]
Set Variable [ $index ; Value: 0 ]
Set Variable [ $foundIndex ; Value: -1 ]
Loop
Set Variable [ $label ; Value: MBS( "Addressbook.multivalue.labelAtIndex"; $index ) ]
If [ $label = $MyLabel ]
Set Variable [ $foundIndex ; Value: $index ]
Exit Loop If [ 1 ]
End If
# next
Set Variable [ $index ; Value: $index + 1 ]
Exit Loop If [ $index >= $count ]
End Loop
If [ $foundIndex >= 0 ]
# found, so update
Set Variable [ $r ; Value: MBS( "Addressbook.multivalue.replaceValueAtIndex"; $foundIndex; $myID ) ]
Else
# not found, so add
Set Variable [ $r ; Value: MBS( "Addressbook.multivalue.addValue"; $myID; $MyLabel ) ]
End If
Set Variable [ $r ; Value: MBS( "Addressbook.record.SetValueForProperty"; $PersonID; "URLsProperty"; "multi" ) ]
# save...
Set Variable [ $r ; Value: MBS( "Addressbook.save" ) ]
And to read back the value, we can use a script like this:
Set Variable [ $PersonID ; Value: MBS( "Addressbook.Me") ]
Set Variable [ $MyLabel ; Value: "FileMakerID" ]
#
# Query current URLs
Set Variable [ $r ; Value: MBS( "Addressbook.record.valueForProperty"; $PersonID; "URLsProperty" ) ]
Set Variable [ $MyID ; Value: MBS( "Addressbook.multivalue.valueForLabel"; $myLabel ) ]
Show Custom Dialog [ "MyID" ; $MyID ]