Skip to content

Instantly share code, notes, and snippets.

@schnoddelbotz
Last active August 29, 2015 14:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schnoddelbotz/707b9d31f5d162c59322 to your computer and use it in GitHub Desktop.
Save schnoddelbotz/707b9d31f5d162c59322 to your computer and use it in GitHub Desktop.
Place a call from terminal using your iPhone - by providing contact's (partial) name (works for local/OnMyMac Contacts.app/AddressBook records only)
#!/usr/bin/perl
# call-x
# no beauty but some joy:
# use osx address book from terminal for (i)phone dialing purposes.
#
# usage:
# $ call-x your-pal's-name...or-partial-number...
#
# In response to the wonderful link max shared with me:
# https://gist.github.com/mattes/d1c9ebf4c9d85aff5c03
# This made me intially aware of that new cool, yosemite-built-in
# protocol handler. thanks to both of you!
# only works nice atm. for no more than 10 address book matches. and it seems
# the buttons to finally initiate the call are fully keyboard-unaccessible (except esc).
# :-( :-( currently works for "On my Mac" contacts only. How to iCloud...?
# ... tab completion for bash+$this would also be nice ;)
use DBI;
use Term::ReadKey;
$counter = 0;
$chosen_one = 0;
$name_to_call = $ARGV[0] || die "bad usage. try: call-by-name mommy";
$username = getlogin();
# ADB location seems to be stable since 10.5... (although 'Contacts.app' now)
$adbpath = "/Users/$username/Library/Application Support/AddressBook";
$adbfile = "$adbpath/AddressBook-v22.abcddb";
$sqlstmt ="SELECT ZFIRSTNAME, ZLASTNAME, ZLABEL, ZORGANIZATION, ZFULLNUMBER
FROM ZABCDRECORD rt, ZABCDCONTACTINDEX it, ZABCDPHONENUMBER pn
WHERE rt.ZCONTACTINDEX=it.Z_PK AND pn.ZOWNER=it.ZCONTACT AND it.ZSTRINGFORINDEXING like ?";
$dbh = DBI->connect( "dbi:SQLite:$adbfile" ) || die "Cannot connect: $DBI::errstr";
$sth = $dbh->prepare($sqlstmt) || die "Cannot prepare query";
$sth->execute("%".$name_to_call."%");
print "\nPhone numbers found for '$name_to_call':\n\n";
while (%r = %{$sth->fetchrow_hashref}) {
$r{ZLABEL}=~s/[\W_]//g;
$displayName = $r{ZFIRSTNAME}." ".$r{ZLASTNAME};
$dialNumber = $r{ZFULLNUMBER};
$dialNumber =~ s/[^\d\+]//g;
push @numbers, $dialNumber;
printf " #%d : %s (%8s) [%s] - %s\n", $counter++, $displayName,
$r{ZLABEL}, $r{ZORGANIZATION}, $r{ZFULLNUMBER};
}
if (@numbers>1) {
ReadMode('cbreak');
print "\nPlease choose number to call: ";
$chosen_one = ReadKey();
print $chosen_one;
ReadMode('normal');
} elsif (@numbers == 0) {
print "Sorry, no matches.\n";
exit;
}
$command = 'open "tel://'.$numbers[$chosen_one].'"';
print "\nExecuting: $command\n\n";
system $command;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment