Skip to content

Instantly share code, notes, and snippets.

@niallsmart
Last active August 29, 2015 14:12
Show Gist options
  • Save niallsmart/c39c743f447eb2856df5 to your computer and use it in GitHub Desktop.
Save niallsmart/c39c743f447eb2856df5 to your computer and use it in GitHub Desktop.
Alfred workflow to search Gmail
alfred_script("foo")
on alfred_script(q)
search_gmail("niall.smart@gmail.com", q)
end alfred_script
on search_gmail(account, searchString)
tell application "Google Chrome"
set windowCount to number of windows
set found to false
repeat with windowNumber from 1 to windowCount
if found then
exit repeat
end if
set tabCount to number of tabs in window windowNumber
repeat with tabNumber from 1 to tabCount
set u to (URL of tab tabNumber of window windowNumber)
set t to (title of tab tabNumber of window windowNumber)
if u contains "mail.google.com" and t contains account then
set p to (characters 1 thru (offset of "#" in u) of u) as string
if searchString is "" then
set p to (p & "inbox")
else
set p to (p & "search/" & my encode_text(searchString, true, true))
end if
set found to true
set active tab index of window windowNumber to tabNumber
set URL of tab tabNumber of window windowNumber to p
exit repeat
end if
end repeat
end repeat
if not found then
set t to make new tab at end of tabs of window 1
set URL of t to ("https://mail.google.com/mail/u/?authuser=" & account)
end if
activate
end tell
end search_gmail
on encode_char(this_char)
set the ASCII_num to (the ASCII number this_char)
set the hex_list to {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}
set x to item ((ASCII_num div 16) + 1) of the hex_list
set y to item ((ASCII_num mod 16) + 1) of the hex_list
return ("%" & x & y) as string
end encode_char
on encode_text(this_text, encode_URL_A, encode_URL_B)
set the standard_characters to "abcdefghijklmnopqrstuvwxyz0123456789"
set the URL_A_chars to "$+!'/?;&@=#%><{}[]\"~`^\\|*"
set the URL_B_chars to ".-_:"
set the acceptable_characters to the standard_characters
if encode_URL_A is false then set the acceptable_characters to the acceptable_characters & the URL_A_chars
if encode_URL_B is false then set the acceptable_characters to the acceptable_characters & the URL_B_chars
set the encoded_text to ""
repeat with this_char in this_text
if this_char is in the acceptable_characters then
set the encoded_text to (the encoded_text & this_char)
else
set the encoded_text to (the encoded_text & encode_char(this_char)) as string
end if
end repeat
return the encoded_text
end encode_text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment