Skip to content

Instantly share code, notes, and snippets.

@harizvi
Created May 27, 2021 21:37
Show Gist options
  • Save harizvi/817617bcb2ad9cef0e98c483d59e0351 to your computer and use it in GitHub Desktop.
Save harizvi/817617bcb2ad9cef0e98c483d59e0351 to your computer and use it in GitHub Desktop.
Hammerspoon code to extract a caller's number and issue a ddg search.
local char_to_hex = function(c)
return string.format("%%%02X", string.byte(c))
end
local function urlencode(url)
if url == nil then
return
end
url = url:gsub("\n", "\r\n")
url = url:gsub("([^%w ])", char_to_hex)
url = url:gsub(" ", "+")
return url
end
-- ** Find a caller's info before picking up a call
function misc_fn.findCallerId()
local rc, retobj, notification_string, phone_number
rc, retobj, notification_string = hs.osascript.applescript([[
-- display notification "from unknown number, +1 (480) 555-1234"
-- delay 1
tell application "System Events" to ¬
if exists window 1 of ¬
process "Notification Center" then ¬
get value of ¬
static text of ¬
group 1 of UI element 1 of ¬
scroll area 1 of ¬
window 1 of ¬
process "Notification Center"
]])
-- notification_string should end with a phone-number.
if (rc) then
print("Notification: ", notification_string, #(notification_string))
if (#(retobj) >= 3) then
phone_number = retobj[3]
else
if (#(retobj) == 1) then
phone_number = retobj[1]
end
end
-- print("Obj:: ", retobj, #(retobj))
-- print("Phone_number: ", phone_number) -- print the last entry of the return object
end
-- split the phone_number string on ,
local w
local words = {}
for w in phone_number:gmatch("[^,]+") do
table.insert(words, w)
end
-- print (words[#(words)]) -- finally, this is the phone number!
local url = 'http://duckduckgo.com/?q=' .. urlencode(words[#(words)])
local shell_cmd = "open " .. url
print (shell_cmd)
hs.execute(shell_cmd)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment