Jump to content

Script Filter Help - Searching Simplenote via CLI


Recommended Posts

Hi! Im using Simplenote and found out there is a CLI available. Now I want to create a workflow to search through my notes. I´m just starting to use the Script Filter more and still I don´t know how I can use json output from the cli to display the results in alfred.

 

If I type something with the Script Filter:

 

/usr/local/bin/sncli -n export {query}

 

debugging shows me:

 

ERROR: Simplenote Search[Script Filter] Unable to decode JSON results, top level type is not dictionary in JSON:
[
  {
    "tags": [],
    "deleted": false,
    "shareURL": "",
    "publishURL": "",
    "content": "gmail",
    "systemTags": [
      "markdown"
    ],
    "modificationDate": 1599312951.145001,
    "creationDate": 1569875316,
    "key": "XXX",
    "version": 58,
    "syncdate": 1600667523.891809,
    "localkey": "XXX",
    "savedate": 1602487240.5917828
  },
  {
    "tags": [],
    "deleted": 0,
    "shareURL": "",
    "publishURL": "",
    "content": "gmail 2",
    "systemTags": [
      "markdown"
    ],
    "modificationDate": 1587040951.0199919,
    "creationDate": 1587031436.520257,
    "key": "XXX",
    "version": 23,
    "syncdate": 1587154334.194783,
    "localkey": "XXX",
    "savedate": 1602487240.5917828
  }
]

 

How is it possible to create an item for each search result, for example with title: content, subtitle: creationDate, argument: localkey?

Link to comment
36 minutes ago, drompono said:

I don´t know how I can use json output from the cli to display the results in alfred

 

You can't just directly feed any old JSON into Alfred: it requires a specific format.

 

You need a script that decodes the Simplenote JSON, converts the data structure to one Alfred understands, then encodes that to JSON for Alfred.

 

I don't have Simplenote, so I can't actually test this script, but it should be close to correct:

 

#!/usr/bin/python

import json
import subprocess
import sys


def simple_search(query):
    """Search Simplenote via CLI."""
    cmd = ['/usr/local/bin/sncli', '-n', 'export']
    if query:
        cmd += [query]
    data = subprocess.check_output(cmd)
    return json.loads(data)


def sn2alfred(results):
    """Convert sncli search results to Alfred results."""
    items = []
    for d in results:
        items.append({
            'title': d['content'],
            'subtitle': d['modificationDate'],
            'arg': d['key']
        })

    return items


def main():
    """Run script."""
    query = sys.argv[1] if len(sys.argv) > 1 else ''
    items = sn2alfred(simple_search(query))
    json.dump({'items': items}, sys.stdout)


if __name__ == '__main__':
    main()

 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...