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 post
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 post

thanks!

 

        items.append({
            'title': d['content'],
            'subtitle': datetime.fromtimestamp(d['creationDate']).strftime('%d.%m.%y - %H:%M'),
            'arg': d['localkey']
        })

 

Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...