Jump to content

Storing static data as JSON -> script

Recommended Posts

Hey all!


I'm making great progress on the workflow i'm working on, but I have hit a roadblock trying to move a large dictionary of settings out of a python script into...somewhere else.


I need to be able to add to and change this data, and it really clutters the script it's running in, so I would love to try to move it into a JSON object in the workflow itself and then import it as a dictionary into the script.  I'm running into an error when I try to read the os.environ variable into python.  Has anyone done anything like this, or does anyone know a better way to store a bbunch of data for my scripts to read?





Link to comment

A little more info.  Here's the workflow JSON I'm using in the workflow.  I tested the "foo" variable, and I can read it just fine.  It's the tableDict variable that doesn't want to read into python:


  "alfredworkflow" : {
    "arg" : "{query}",
    "variables" : {
      "foo" : "bar",
      "tableDict" : {
        "rack" : {
          "title_field" : "sys_class_name",
          "primary_search_field" : "name",
          "prefix" : "",
          "table" : "cmdb_ci_rack",
          "instance" : "datacenter",
          "subtitle_field" : "dv_location"
        "ci" : {
          "title_field" : "sys_class_name",
          "primary_search_field" : "name",
          "prefix" : "",
          "table" : "cmdb_ci",
          "instance" : "datacenter",
          "subtitle_field" : ""


Link to comment

A bit more info.  I've tried various methods to read this info into python.  Even trying to print the variable gives me an error:


import os

print("imported var foo is a " + str(type(os.environ['foo']))) # this totally works

print("imported var tableDict is a " + str(type(os.environ['tableDict']))) # this totally doesn't




imported var foo is a <type 'str'>

[2019-02-05 11:45:03][ERROR: input.scriptfilter] Code 1: Traceback (most recent call last):

  File "/Users/*****/Library/Caches/com.runningwithcrayons.Alfred-3/Workflow Scripts/5A6C1B81-78E5-42B0-9512-5DFD13560853", line 20, in <module>

    print("imported var tableDict is a " + str(type(os.environ['tableDict'])))

  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/UserDict.py", line 23, in __getitem__

    raise KeyError(key)

KeyError: 'tableDict'

[2019-02-05 11:55:24][ERROR: input.scriptfilter] JSON error: JSON text did not start with array or object and option to allow fragments not set. in JSON:


Edited by Cybrosis
Link to comment

Is there a way to integrate that into the workflow itself?  I guess some sort of "set config" option that just opens the config file in the default text editor?  I would rather not convert to a string; the option to have the options in a separate file on disk seems much more elegant.

Link to comment
On 2/6/2019 at 6:17 PM, Cybrosis said:

I guess some sort of "set config" option that just opens the config file in the default text editor?


It's hard to say without knowing more about exactly what you're trying to achieve. In most cases, opening a config file in the default editor is a bad idea: the default macOS editor is TextEdit, and it defaults to "smart" quotes, which will screw up most source code. I would only recommend opening the config file directly if your workflow is aimed at users likely to have associated a real editor with JSON/YAML/TOML/<your format here> files.


So if your workflow isn't aimed at developers, it's probably better to just reveal the file in Finder: then it's the user's fault if they screw it up by opening it in TextEdit.


Alfred does have a decent AppleScript settings API, but it's based on workflow/environment variables, which are strings. Here's the skinny on workflow/environment variables in Alfred.


You can store JSON in Alfred's variables, which can be a handy way to pass stuff between workflow elements, but you're probably better off having your workflow handle its own configuration via a file in the alfred_workflow_data directory.

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