Skip to content

Instantly share code, notes, and snippets.

@persquare
Last active March 5, 2020 15:28
Show Gist options
  • Save persquare/840b96d2da2bf8c3f9eb to your computer and use it in GitHub Desktop.
Save persquare/840b96d2da2bf8c3f9eb to your computer and use it in GitHub Desktop.
Create a ZK-note and return the file path
#!/usr/bin/env python
# zk - create a new zettelkast entry and return the path
import sys
import os
import pwd
import argparse
from datetime import datetime
def create_zk(path, author=''):
template = "Date: %s\nAuthor: %s\nID: %s\nTags: \n\n"
time = datetime.now()
date = time.strftime("%Y-%m-%d %H:%M:%S")
zkid = time.strftime("%y%m%d%H%M%S")
filename = "zk" + zkid + ".md"
path = os.path.join(path, filename)
with open(path, 'w') as f:
f.write(template % (date, author, zkid))
return path
def txmt_URI(path):
uri = "txmt://open?url=file://%s&line=6&column=0" % path
return uri
if __name__ == '__main__':
# Defaults
author = os.getenv('ZK_AUTHOR', pwd.getpwuid(os.getuid())[4])
path = os.getenv('ZK_DIR', '~/Desktop')
path = os.path.expanduser(path)
path = os.path.expandvars(path)
path = os.path.abspath(path)
long_desc = """
Create a zk-note file with some defaults filled in.
The name of the created file matches the ID of the note, i.e.: "zk<ID>.md"
Returns the full path to the newly created file on success.
"""
epilog = """
The default author is read from environment variable 'ZK_AUTHOR' if it exists,
otherwise it is set to full name of the current user.
The default directory to create the not in is read from environment variable 'ZK_DIR' if it exists,
otherwise it is set to $HOME/Desktop.
"""
parser = argparse.ArgumentParser(description=long_desc, epilog=epilog)
parser.add_argument('-a', '--author', dest='author', metavar='<name>',
type=str, default=author,
help='Override default author name (%s)' % author)
parser.add_argument('-d', '--dir', dest='path', metavar='<dir>',
type=str, default=path,
help='Override default Zettelkasten directory (%s)' % path)
parser.add_argument('--txmt', action="store_true",
help='format output as a URI for use with TextMate 2')
args = parser.parse_args()
try:
retval = create_zk(args.path, args.author)
except:
sys.stderr.write('Error: Could not create zk-note\n')
sys.exit(1)
if args.txmt:
retval = txmt_URI(retval)
print retval
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment