Skip to content

Instantly share code, notes, and snippets.

@dbreunig
Last active December 7, 2020 22:45
Show Gist options
  • Save dbreunig/635331b1956f6240ec80f78d785ad120 to your computer and use it in GitHub Desktop.
Save dbreunig/635331b1956f6240ec80f78d785ad120 to your computer and use it in GitHub Desktop.
A script that creates a note with a specified, date stamped file name in a defined location, appends a divider and timestamp, then opens the file with TextEdit (macOS). This is what I'm reduced to now that Notational Velocity doesn't work so hot.
#!/usr/bin/env ruby
#
# qn stands for 'Quick Note.' This app quickly creates a text file
# in a hardcoded or specified directory with a default filename.
#
# The filename is {project}_YYYY-MM-DD.txt
#
# One file per day, per project is created. If the file exist, the
# new note is appended.
#
require 'slop'
require 'date'
@version = 0.1
@directory = "/Users/dbreunig/Dropbox/Library/Notational_Data/"
@project = "PIQ"
# Helper to clean the project name
def sanitize_filename(filename)
return filename.strip do |name|
# General Cleaning
name.gsub!(/^.*(\\|\/)/, '')
# Strip out the non-ascii character
name.gsub!(/[^0-9A-Za-z.\-]/, '_')
end
end
# Argument handling
opts = Slop.parse do |o|
o.string '-d', '--dir', 'the target directory'
o.on '-v', '--version', 'print the version' do
puts @version
end
o.on '-h', '--help', 'provide help' do
puts "usage: qn [-d/--dir YOUR-DIRECTORY] your-project"
puts "If a project name is not provided, the default will be used."
end
end
# Set the directory if present
@directory = opts[:dir] if opts[:dir]
# Set the project name if present
@project = opts.arguments.first.upcase unless opts.arguments.empty?
# Generate the file name
today = Date.today
filename = "#{sanitize_filename(@project)}_#{today.strftime('%Y-%m-%d')}.txt"
path = File.join(@directory, filename)
# Check if the file exists before opening
file_exists = File.exist?(path)
open(path, 'a') do |f|
if file_exists
f << "\n\n"
end
f << "———————————\n"
f << Time.now.strftime('%l:%M %p').strip # Not a fan of the space padded hour
f << "\n\n"
end
exec( "open -a TextEdit #{path}" )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment