Skip to content

Instantly share code, notes, and snippets.

@jasonsnell
Last active February 4, 2023 08:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonsnell/e1821aada020e7b8fc66411c6cfac4e7 to your computer and use it in GitHub Desktop.
Save jasonsnell/e1821aada020e7b8fc66411c6cfac4e7 to your computer and use it in GitHub Desktop.
Echo Shortcut Notifier
#! /usr/bin/env python3
# <xbar.title>Echo Shortcut Notifier</xbar.title>
# <xbar.version>v1.01</xbar.version>
# <xbar.author>Jason Snell</xbar.author>
# <xbar.author.github>jasonsnell</xbar.author.github>
# <xbar.desc>Display status from Shortcuts.</xbar.desc>
import os
import datetime
theFilePath = '/Users/jsnell/shortcuts-status.txt'
modTimesinceEpoc = os.path.getmtime(theFilePath)
modificationTime = datetime.datetime.fromtimestamp(modTimesinceEpoc).strftime('%-I:%M %p')
with open(theFilePath) as f:
for i, line in enumerate(f):
print(line.strip())
if i == 0:
print('---')
print ("Last updated at", modificationTime)
@dchevell
Copy link

dchevell commented Jan 6, 2023

FWIW you can simplify this a bit - you don't need to manually count up the lines to get indexes. You can use enumerate(iterable) to give you an index, and do this:

with open(theFilePath) as f:
    for i, line in enumerate(f):
        print(line.strip())
        if i == 0:
            print('---')

Oh, and modificationTime is already a string, so may as well use print()'s native concatenation:

print("Last updated", modificationTime)

@nojohnny101
Copy link

Don't know version of python you are using, but f-strings read nicer and easier to understand when printing things

print(f"Last updated at {modificationTime}")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment