Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Gmail Notifier for Mac OSX
['gmail','terminal-notifier'].each {|x| require x}
username = 'emailid'
password = %x(security 2>&1 find-internet-password -ga #{username} | grep password | cut -d '"' -f2).strip
gmail = Gmail.new(username, password)
puts "logged in to gmail"
path = "#{ENV['HOME']}/.gmail_notifier_last_running_time"
last_running_time = File.exists?(path) ? Time.now.to_i : File.read(path).strip.to_i
flag, idx = 0, 0
search_queries = [[:subject, "sweets"],[:subject, "chocolates"],[:body, "sweets"],[:body, "chocolates"]]
while flag == 0
puts "Trying search_query = #{search_queries[idx]}"
mail = gmail.inbox.search(:unread, search_queries[idx][0] => search_queries[idx][1], :after => Time.at(last_running_time))
last_running_time = Time.now.to_i
mail = mail.select {|x| x.subject.nil? || !x.subject.start_with?("Re:") }.compact
if mail.length > 0
content_to_display = mail[0].subject.nil? ? mail[0].text_part.body : mail[0].subject
o = %x(afplay -v 4 -q 1 /System/Library/Sounds/Glass.aiff)
TerminalNotifier.notify("Subject : #{content_to_display}", :title => 'Fab Mail')
mail[0].read!
flag = 1
end
idx += 1
break if idx >= search_queries.length
end
File.write(path, "#{last_running_time}")
# Save your username and password in keychain ring
# > security add-internet-password -a username -w password -s http://mail.google.com
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment