Created
May 25, 2024 03:30
-
-
Save sychou/ea20a00dc93cb3a17da772a485384c38 to your computer and use it in GitHub Desktop.
Export Contacts to Obsidian Markdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Set the folder path for the Obsidian notes | |
set folderPath to "/Users/sean/Vaults/Test/People" | |
-- Function to convert special labels to human-readable form and title case them | |
on getReadableLabel(originalLabel) | |
if originalLabel is "_$!<Home>!$_" then | |
return "Home" | |
else if originalLabel is "_$!<Work>!$_" then | |
return "Work" | |
else if originalLabel is "_$!<Mobile>!$_" then | |
return "Mobile" | |
else if originalLabel is "_$!<Main>!$_" then | |
return "Main" | |
else if originalLabel is "_$!<HomeFAX>!$_" then | |
return "Home Fax" | |
else if originalLabel is "_$!<WorkFAX>!$_" then | |
return "Work Fax" | |
else if originalLabel is "_$!<OtherFAX>!$_" then | |
return "Other Fax" | |
else if originalLabel is "_$!<Pager>!$_" then | |
return "Pager" | |
else | |
return originalLabel | |
end if | |
end getReadableLabel | |
-- Function to title case a label and append "Phone" or "Email" as appropriate | |
on titleCaseLabel(theLabel, isPhone) | |
set theLabel to words of theLabel | |
set titleCasedLabel to "" | |
repeat with aWord in theLabel | |
set firstChar to (character 1 of aWord) as text | |
set firstChar to do shell script "echo " & firstChar & " | tr '[:lower:]' '[:upper:]'" | |
set titleCasedLabel to titleCasedLabel & firstChar & (text 2 thru -1 of aWord) & " " | |
end repeat | |
set titleCasedLabel to text 1 thru -2 of titleCasedLabel | |
if isPhone then | |
if titleCasedLabel does not end with "Phone" then | |
return titleCasedLabel & " Phone" | |
else | |
return titleCasedLabel | |
end if | |
else | |
if titleCasedLabel does not end with "Email" then | |
return titleCasedLabel & " Email" | |
else | |
return titleCasedLabel | |
end if | |
end if | |
end titleCaseLabel | |
-- Function to pretty format phone numbers | |
on prettyFormatPhoneNumber(phoneNumber) | |
set phoneNumber to do shell script "echo " & quoted form of phoneNumber & " | sed 's/[^0-9+]*//g'" | |
set lengthOfNumber to length of phoneNumber | |
if (phoneNumber starts with "+1" or phoneNumber starts with "1") and lengthOfNumber ≥ 11 then | |
if phoneNumber starts with "+1" then | |
set phoneNumber to text 3 thru -1 of phoneNumber | |
else if phoneNumber starts with "1" then | |
set phoneNumber to text 2 thru -1 of phoneNumber | |
end if | |
set formattedNumber to "+1-" & text 1 thru 3 of phoneNumber & "-" & text 4 thru 6 of phoneNumber & "-" & text 7 thru 10 of phoneNumber | |
else if phoneNumber starts with "+" then | |
set formattedNumber to "+" & text 2 thru -1 of phoneNumber | |
else if lengthOfNumber ≥ 10 then | |
set formattedNumber to "+1-" & text 1 thru 3 of phoneNumber & "-" & text 4 thru 6 of phoneNumber & "-" & text 7 thru 10 of phoneNumber | |
else | |
set formattedNumber to phoneNumber | |
end if | |
return formattedNumber | |
end prettyFormatPhoneNumber | |
tell application "Contacts" | |
-- Get all the contacts | |
set allPeople to every person | |
repeat with aPerson in allPeople | |
-- Get the name of the contact | |
set aName to name of aPerson | |
log aName | |
-- Create the note content that will be saved to the file | |
set aNote to "# " & aName & linefeed & linefeed & "## Contact" & linefeed & linefeed | |
-- Add phone numbers | |
if (count of phones of aPerson) > 0 then | |
set phoneList to phones of aPerson | |
if (count of phoneList) is 1 then | |
set formattedPhone to my prettyFormatPhoneNumber(value of item 1 of phoneList) | |
set aNote to aNote & "Phone: " & formattedPhone & linefeed | |
else | |
repeat with aPhone in phoneList | |
set phoneLabel to my getReadableLabel(label of aPhone) | |
set phoneLabel to my titleCaseLabel(phoneLabel, true) | |
set formattedPhone to my prettyFormatPhoneNumber(value of aPhone) | |
set aNote to aNote & phoneLabel & ": " & formattedPhone & linefeed | |
end repeat | |
end if | |
end if | |
-- Add email addresses | |
if (count of emails of aPerson) > 0 then | |
set emailList to emails of aPerson | |
if (count of emailList) is 1 then | |
set aNote to aNote & "Email: " & (value of item 1 of emailList) & linefeed | |
else | |
repeat with anEmail in emailList | |
set emailLabel to my getReadableLabel(label of anEmail) | |
set emailLabel to my titleCaseLabel(emailLabel, false) | |
set aNote to aNote & emailLabel & ": " & (value of anEmail) & linefeed | |
end repeat | |
end if | |
end if | |
-- Add addresses | |
if (count of addresses of aPerson) > 0 then | |
set addressList to addresses of aPerson | |
if (count of addressList) is 1 then | |
set singleAddress to formatted address of item 1 of addressList | |
set aNote to aNote & "Address: " & my singleLineAddress(singleAddress) & linefeed | |
else | |
repeat with anAddress in addressList | |
set singleAddress to formatted address of anAddress | |
set aNote to aNote & "Address: " & my singleLineAddress(singleAddress) & linefeed | |
end repeat | |
end if | |
end if | |
-- Add the Notes section header | |
set aNote to aNote & linefeed & "## Notes" & linefeed & linefeed | |
-- Add any notes from the contact | |
if (note of aPerson is not missing value) then | |
set noteContent to note of aPerson | |
set noteLines to paragraphs of noteContent | |
repeat with aLine in noteLines | |
if aLine starts with "# " or aLine is "" then | |
-- Skip this line | |
else | |
set aNote to aNote & aLine & linefeed | |
end if | |
end repeat | |
end if | |
-- Create the Markdown file | |
set fn to folderPath & "/" & aName & ".md" | |
set fileRef to null | |
try | |
set fileRef to open for access fn with write permission | |
write aNote to fileRef | |
close access fileRef | |
on error | |
if fileRef is not null then | |
close access fileRef | |
end if | |
end try | |
end repeat | |
end tell | |
-- Function to convert a multiline address to a single line | |
on singleLineAddress(multilineAddress) | |
set singleLine to do shell script "echo " & quoted form of multilineAddress & " | tr '\\n' ','" | |
set singleLine to do shell script "echo " & quoted form of singleLine & " | sed 's/,,/,/g'" | |
set singleLine to do shell script "echo " & quoted form of singleLine & " | sed 's/,/, /g'" | |
set singleLine to do shell script "echo " & quoted form of singleLine & " | sed 's/, *$//'" | |
return singleLine | |
end singleLineAddress |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment