Skip to content

Instantly share code, notes, and snippets.

@salimhb
Last active December 25, 2015 13:29
Show Gist options
  • Save salimhb/6983555 to your computer and use it in GitHub Desktop.
Save salimhb/6983555 to your computer and use it in GitHub Desktop.
Add Whatsapp Social Profile Link to Addressbook looking up Whatsapp ABID
# This applescript adds whatsapp social profile link to your address book
# 1. Backup whatsapp's chat to your iCloud account
# 2. Make sure that you are using the same iCloud account on your mac and that "Contacts" and "Documents & Data" are enabled
# 3. IMPORTANT: Backup your address book before running this script!
# 4. RUN IT
set originalDelimiter to AppleScript's text item delimiters
set db_path to (do shell script "find ~/Library/Mobile\\ Documents/*WhatsApp/Accounts/*/* -maxdepth 1 -type f | grep ChatStorage.sqlite")
set AppleScript's text item delimiters to ASCII character 13 # \r
set dbs to every text item of db_path
if (count of dbs) > 1 then
repeat with db in dbs
set AppleScript's text item delimiters to linefeed
display dialog "More than one Whatsapp account were found in your iCloud folder." & linefeed & linefeed & "Click Select to choose: " & linefeed & db & linefeed & linefeed & "Click Next to see the next database" buttons {"Select", "Next", "Cancel"} default button "Select"
if button returned of the result = "Select" then
set db_path to contents of db
exit repeat
end if
end repeat
end if
set loc to space & quoted form of db_path & space
set head to "sqlite3" & loc & quote
set tail to quote
set query to "select ZCONTACTABID, ZCONTACTJID, ZPARTNERNAME from ZWACHATSESSION where ZCONTACTABID != 0;"
set rows to do shell script head & query & tail
set AppleScript's text item delimiters to return
set rowsArray to every text item of rows
tell application "Contacts"
repeat with row in rowsArray
set AppleScript's text item delimiters to "|"
set {abid, whatsappID, contactName} to every text item of row
set thePeople to (every person whose name = contactName)
if (count of thePeople) > 1 then
repeat with aPerson in thePeople
set AppleScript's text item delimiters to linefeed
display dialog "More than one person in your Address Book are named: " & contactName & linefeed & linefeed & "The whatsapp address we're looking for is: " & whatsappID & linefeed & linefeed & "Does it match with any of these phone numbers?" & linefeed & linefeed & value of every phone of aPerson & linefeed & linefeed & "Click Next if this is an incorrect match." buttons {"Select", "Next", "Cancel"} default button "Select"
if button returned of the result = "Select" then
set thePerson to contents of aPerson
exit repeat
end if
end repeat
else if (count of thePeople) = 1 then
set thePerson to first item of thePeople
end if
# delete old data
# delete (every url of thePerson whose label is "Whatsapp")
# delete (every social profile of thePerson whose service name is "Whatsapp")
set existingWhatsappSocialProfiles to (every social profile of thePerson whose service name is "Whatsapp")
if (count of existingWhatsappSocialProfiles) > 0 then
repeat with existingWhatsappSocialProfile in existingWhatsappSocialProfiles
set user identifier of existingWhatsappSocialProfile to abid
set user name of existingWhatsappSocialProfile to "✅ " & contactName
set url of existingWhatsappSocialProfile to "whatsapp://send?abid=" & abid
end repeat
else
make new social profile at end of every social profile of thePerson with properties {user identifier:abid, user name:"✅ " & contactName, service name:"Whatsapp", url:"whatsapp://send?abid=" & abid}
end if
save thePerson
end repeat
end tell
set AppleScript's text item delimiters to originalDelimiter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment