Created
February 27, 2021 01:27
-
-
Save bkyle/94743b9b35d34fd71a74001ee23c1779 to your computer and use it in GitHub Desktop.
Alfred Script Filter to list Johnny.Decimal index
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import print_function | |
import json | |
import os | |
import subprocess | |
import sys | |
def ls(wd): | |
"""Returns a list of directories under ``wd`` that match the johnny.decimal | |
pattern. The directories returned will be related to the passed working directory.""" | |
pattern = "[1234567890][1234567890]-*/[1234567890][1234567890]\ */[1234567890][1234567890].[1234567890][1234567890]*" | |
cmd = ["/bin/sh", "-c", "ls -1d %s" % pattern] | |
listing = subprocess.check_output(cmd, cwd=wd) | |
return listing.splitlines() | |
def item(relpath, wd): | |
"""Returns a dict to be included in the search filer results.""" | |
basename = os.path.basename(relpath) | |
abspath = os.path.join(wd, relpath) | |
return { | |
"type": "file", | |
"title": basename, | |
"subtitle": relpath, | |
"arg": abspath, | |
"icon": { | |
"type": "fileicon", | |
"path": abspath, | |
}, | |
} | |
wd = sys.argv[1] if len(sys.argv) == 2 else None | |
if not wd: | |
wd = os.getenv("JOHNNY_ROOT") | |
if not wd: | |
wd = os.getcwd() | |
wd = os.path.expanduser(wd) | |
results = {"items": [item(relpath, wd) for relpath in ls(wd)]} | |
print(json.dumps(results)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment