Skip to content

Instantly share code, notes, and snippets.

@n8henrie
Last active July 10, 2022 14:59
Show Gist options
  • Save n8henrie/5814757 to your computer and use it in GitHub Desktop.
Save n8henrie/5814757 to your computer and use it in GitHub Desktop.
Send iMessage messages with Quicksilver -- version 2. This version uses some Python to search a plaintext "phonebook" for non-email / non-phone number third-pane input (like a name). See the full post at http://n8henrie.com/2013/06/send-imessages-with-quicksilver-v2/
import os
import re
import sys
vcf_file = sys.argv[1]
def parse_file(infile):
card = ''
for line in infile:
if line.startswith('BEGIN:VCARD') and card.endswith('END:VCARD\r\n'):
yield card
card = ''
card += line
yield card
iphone_count = 0
messages_phonebook = []
with open(vcf_file,'rb') as infile:
for i, card in enumerate(parse_file(infile)):
num_match = re.search(r'(?<=TEL;type=IPHONE)(.*?:)(.*)', card)
if num_match:
iphone_count += 1
name = re.search(r'(?<=FN:)(.*)\r\n', card).group(1)
number = re.sub(r'\D','', num_match.group(2))
messages_phonebook.append((name, number))
with open(os.getenv("HOME") + '/Desktop/messages_phonebook.txt','ab') as w:
w.write('\n{}, {}'.format(name, number))
with open(os.getenv("HOME") + '/Desktop/messages_phonebook.txt','rb') as r:
names_sorted = (name.strip() for name in sorted(r.read().splitlines() ) if name.strip() != '' )
with open(os.getenv("HOME") + '/Desktop/messages_phonebook.txt','wb') as w:
w.writelines('\n'.join(names_sorted))
#!/usr/bin/env python
import sys
import re
script, name, phonebook_file = sys.argv
match = re.search(r'^\d{7}$|^\d{10}$|^(\d{3}\-)?\d{3}-\d{4}$|(^.+?@.+?\..+?$)', name)
# The input was a phone number or email, go with it.
if match:
print name
exit(0)
else:
name = name.lower()
with open(phonebook_file, 'r') as f:
contacts_list = [ tuple(
line.lower().strip().split(', ') )
for line in f if len(line) > 1]
for contact, number in contacts_list:
if name == contact:
print number
exit(0)
else:
pass
# Returns the original name if no match was found.
print name
property appleID : "yourAppleID"
property path_to_python : "python3"
property path_to_qs_messages : "/Users/[username]/path/to/qs_messages.py"
property abs_path_to_messages_phonebook : "/Users/[username]/path/to/messages_phonebook.txt"
(*
See post at: http://n8henrie.com/2013/04/send-imessage-messages-with-quicksilver
Version 2 of the script at: http://n8henrie.com/2013/06/send-imessages-with-quicksilver-v2/
Send an iMessage to a buddy with Quicksilver.
Install by placing in ~/Library/Application Support/Quicksilver/Actions and restarting Quicksilver
First pane: type text to send.
Second pane: choose this action.
Third pane: pick buddy (iMessage number, iMessage email address, or their name if you've put it with their number into messages_phonebook.txt)
*)
using terms from application "Quicksilver"
on get direct types
return {"NSStringPboardType"}
end get direct types
on get indirect types
return {"qs.contact.phone", "qs.contact.email"}
end get indirect types
on process text firstPane with thirdPane
if appleID is "yourAppleID" then
display dialog "You need to open the \"Send with Messages\" Action script and set your Apple ID. Exiting."
return
else
-- If the python part is set up...
try
set the_command to "PATH=/opt/homebrew/bin:/usr/local/bin:$PATH " & path_to_python & " " & path_to_qs_messages & " " & quoted form of thirdPane & " " & quoted form of abs_path_to_messages_phonebook
set thirdPane to do shell script the_command
end try
try
tell application "Messages"
tell (first account whose (service type is iMessage and description is ("E:" & appleID)))
login
send firstPane to participant thirdPane
end tell
end tell
on error a number b
activate
display dialog a with title "error with your QS action script"
end try
end if
end process text
on get argument count
return 2
end get argument count
end using terms from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment