diff --git a/getTagsList.py b/getTagsList.py index 35e3602..670a4b3 100644 --- a/getTagsList.py +++ b/getTagsList.py @@ -10,22 +10,48 @@ import shlex import subprocess -selectedDbUUID = os.getenv('selectedDbUUID', "") +def runForCertainDB(selectedDbUUID): + cmdScript = "osascript -e 'tell application \"DEVONthink 3\" to get name of tag group of (get database with uuid \"{}\")' -s s".format(selectedDbUUID) + cmdList = shlex.split(cmdScript) + cmdResult = subprocess.check_output(cmdList) + tagList = ast.literal_eval( + "[" + cmdResult[1:-2].decode("UTF8") + "]") + + result = [] + for tag in tagList: + result.append({ + "title": tag, + "subtitle": "Press Enter to list all files with this tag", + # "arg": tag, + "variables": {"selectedTag": tag, "selectedDbUUID": selectedDbUUID} + }) + return result -cmdScript = "osascript -e 'tell application \"DEVONthink 3\" to get name of tag group of (get database with uuid \"{}\")' -s s".format(selectedDbUUID) -cmdList = shlex.split(cmdScript) -cmdResult = subprocess.check_output(cmdList) -tagList = ast.literal_eval( - "[" + cmdResult[1:-2].decode("UTF8") + "]") +selectedDbUUID = os.getenv('selectedDbUUID', "") result = {"items": []} -for tag in tagList: - result["items"].append({ - "title": tag, - "subtitle": "Press Enter to list all files with this tag", - # "arg": tag, - "variables": {"selectedTag": tag, "selectedDbUUID": selectedDbUUID} - }) + +if selectedDbUUID == '': + getAllUUIDs = """'set dbUUIDs to {} + tell application "DEVONthink 3" + set allDb to every database + repeat with theDb in allDb + set end of dbUUIDs to (get uuid of theDb) + end repeat + end tell + get dbUUIDs'""" + proc = subprocess.Popen("osascript -e " + getAllUUIDs.strip(), + stdout=subprocess.PIPE, shell=True) + (out, err) = proc.communicate() + DbUUIDs = out.decode('utf-8').strip().split(', ') + for db_uuid in DbUUIDs: + dbresult = runForCertainDB(db_uuid) + result['items'].extend(dbresult) + +else: + dbresult = runForCertainDB(selectedDbUUID) + result['items'].extend(dbresult) + if result['items']: print(json.dumps(result))