woodenzen / zkdb

Zettelkasten Stats Dashboard
4 stars 2 forks source link

Here is how you can get the zettelkasten path from The Archive settings #3

Closed pryley closed 2 years ago

pryley commented 3 years ago

You can get the zettelkasten path with this (alfred-path.py):

#!/usr/bin/env python
# encoding: utf-8

from __future__ import absolute_import
from Foundation import NSData
from Foundation import NSPropertyListMutableContainers
from Foundation import NSPropertyListSerialization
import os
import sys
import urllib2

class FoundationPlistException(Exception):
    """Basic exception for plist errors"""
    pass

class NSPropertyListSerializationException(FoundationPlistException):
    """Read/parse error for plists"""
    pass

class TheArchive:
    @staticmethod
    def path():
        archive_url = TheArchive.setting('archiveURL')
        path = urllib2.unquote(archive_url[len("file://"):])
        return path

    @staticmethod
    def plist():
        bundle_id = "de.zettelkasten.TheArchive"
        team_id = "FRMDA3XRGC"
        filepath = os.path.expanduser("~/Library/Group Containers/{0}.{1}.prefs/Library/Preferences/{0}.{1}.prefs.plist".format(team_id, bundle_id))
        if os.path.exists(filepath):
            plistData = NSData.dataWithContentsOfFile_(filepath)
            data, dummy_plistFormat, error = (
                NSPropertyListSerialization.propertyListFromData_mutabilityOption_format_errorDescription_(
                    plistData, NSPropertyListMutableContainers, None, None
                )
            )
            if data is None:
                if error:
                    error = error.encode('ascii', 'ignore')
                else:
                    error = "Unknown error"
                raise NSPropertyListSerializationException("{0} in file {1}".format(error, filepath))
            else:
                return data
        else:
            raise Exception("Error: Cannot find The Archive plist.")

    @staticmethod
    def setting(key):
        data = TheArchive.plist()
        try:
            return data[key]
        except KeyError:
            raise Exception(u"Warning: Cannot get The Archive setting: {0}".format(key))

path = TheArchive.path()
sys.stdout.write(path)
$ python alfred-path.py
woodenzen commented 3 years ago

Wow! Thanks, @pryley. Sorry to bother you with such a mundane beginner question.

I ran this in my setup Python 3.9.7, and got the following exception. ModuleNotFoundError: No module named 'urllib2' I tried pip install urlliband found via StackOverflow that urllib was split into urllib.parse, urllib.request, and urllib.error in Python 3. But trying to pip install any of these, I get this error. ERROR: No matching distribution found for urllib.parse. Do you have suggestions? Maybe pip isn't used to install this module. Does it come packaged with python?

pryley commented 3 years ago

Ah, for python3 you can do this: from urllib.parse import unquote

And then: path = unquote(archive_url[len("file://"):])

woodenzen commented 3 years ago

Thanks again. It worked perfectly. I looked again at the urllib.parse documentation and now see the syntax for unquote. I stumbled at my confused idea that the module needed to be installed. The from urllib.parse import unquote wasn't clear. But all is better in the universe.