Skip to content

Instantly share code, notes, and snippets.

@henrik
Last active January 14, 2017 20:31
Show Gist options
  • Save henrik/4677108 to your computer and use it in GitHub Desktop.
Save henrik/4677108 to your computer and use it in GitHub Desktop.
Mail notes to self with Alfred.app, a Ruby script and Gmail. Inspired by Captio: http://boonbits.com/captio/

Save the Ruby script in ~/.bin/note_to_self.rb. Configure it, install the gems it uses.

Add an Alfred extension manually as described below.

Alfred 3

Use the "Keyword to Script to Notification" template; use the below as your script.

Keyword: note | [x] with space | Argument Required

Alfred 2

Keyword: note

Options: [x] Silent

Command:

source ~/.rvm/scripts/rvm
~/.bin/note_to_self.rb {query}

Parameter: Required parameter

Advanced: Output: Output to Notification Center

#!/usr/bin/env ruby
# encoding: utf-8
# Command-line tool to mail notes to self with Gmail.
#
# Install gems if you haven't:
# gem install gmail activesupport
#
# Configure:
TO = "henrik@nyh.se"
LOGIN = "robot@nyh.se"
PW = File.read(File.expand_path("~/.gmailpw")).chop
# Intended to be used with an Alfred.app extension like:
#
# source ~/.rvm/scripts/rvm
# ~/.bin/note_to_self.rb {query}
begin
# https://github.com/nu7hatch/gmail
require "gmail"
# To fix Alfred.app encoding :(
require "active_support/all"
rescue LoadError => e
puts e.message
exit 1
end
def init
message = ARGV.first
# Alfred passes decomposed characters which cause the mail to silently fail.
message = message.mb_chars.normalize
note_to_self(message)
end
def note_to_self(message)
send_mail(LOGIN, PW, TO, message, message)
puts "Sent: #{message}"
rescue StandardError => e
puts "Error sending: #{e.class.name}: #{e.message}"
end
def send_mail(login, pw, _to, _subject, _body)
Gmail.connect!(login, pw) do |gmail|
gmail.deliver do
to _to
subject _subject
text_part do
content_type 'text/plain; charset=UTF-8'
body _body
end
end
end
end
init
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment