Jump to content

Dynamic Script Filter


Misha

Recommended Posts

Hi all, is it possible to create a dynamic script filter, in the sense that the list of options is created on the basis of the contents of a directory? I currently have this code, which seems to work in the terminal, but it doesn't produce the list that I would like:

 

for f in mydirectory/*;

do

x=$(basename "$f")
x1="${x%.*}"
x12=$(echo $x | sed 's/_.*//')

cat << EOB

{"items": [
    {
        "uid": "$x1",
        "type": "file",
        "title": "$x1",
        "subtitle": "",
        "arg": "mydirectory/$x1",
        "autocomplete": "$x1",
        "icon": {
            "path": "colour_$x12.png"
        }
    },
]}
EOB
done

 

 

 

Edited by Misha
Link to comment

It's wrong in a few ways. First of all, generating valid JSON with bash is tricky. There's a good chance that even when the logic is fixed, it's still going to break on certain filenames.


The two big problems are:

  1. You must only output one JSON items object. You're outputting one for each file, and {...}{...} is not valid JSON, let alone the correct format for Alfred.
  2. You're using a relative filepath (mydirectory/*). Workflows are run with the workflow's own directory as the working directory, so unless you are actually trying to list the files in a subdirectory of the workflow, you'll need an absolute path.

 

Edited by deanishe
Link to comment

Thanks Deanishe for your quick reply. The item point solved the problem! (I changed the directory to "mydirectory" because of privacy reasons). I am sure there are more elegant ways of doing this, but for anyone trying to do the same, this code works for me:

 

cat << EOB

{"items": [

EOB

for f in /mydirectory/*; 

do

x=$(basename "$f")
x1="${x%.*}"
x12=$(echo $x | sed 's/_.*//')

cat << EOB


    {
        "uid": "$x1",
        "type": "file",
        "title": "$x1",
        "subtitle": "",
        "arg": "/mydirectory/$x1",
        "autocomplete": "$x1",
        "icon": {
            "path": "colour_$x12.png"
        }
    },


EOB
done

cat << EOB
]}
EOB

 

Link to comment

I'm definitely doing funky things. And I'm also sure the things I do could be done differently and better. (I built my own time-clock (checking in and out for certain tasks). For that, this  setup works for me because 1. it shows all the options at once, while (I believe) a file filter wouldn't 2. I can adjust the icons (they are dynamic) and 3. I can attach arguments to these options that I use later on in the workflow.)

Link to comment
  • 2 years later...
On 11/25/2017 at 7:24 AM, deanishe said:

 

It shouldn't, tbh: the JSON is invalid. If Alfred is accepting it, it must be using a non-standard JSON parser, as trailing commas are not permitted in JSON.

Hi @deanishe may I ask what the correct syntax should be? I am trying to learn how to replace XML with JSON in my script filters (mostly serving the output of grep commands, typically multiple matches), and this from the OP seems to me similar to what is provided as an example in Alfred. 

Thanks for educating us newbies!

G. 

Edited by giovanni
Link to comment

Thanks!

To clarify, I am trying to convert an XML script filter to JSON and I was wondering about the appropriate way to generate dynamically a JSON object containing multiple items (from grep matches), and avoid the trailing comma.

thanks again!

giovanni

Link to comment
On 5/21/2020 at 6:22 PM, giovanni said:

the appropriate way to generate dynamically a JSON object containing multiple items (from grep matches), and avoid the trailing comma

 

TBH, the only safe (and sane) way to do it is to use a real JSON library or tool. You can get away with cat for static (or very, very limited) JSON, but for anything that isn't super simple, you shouldn’t try to generate JSON by smushing strings together.

 

So you should either use a language with support for JSON (essentially anything but shell or AppleScript), or if you simply must use bash even when it’s not up to the job, you can use jq (install it with Homebrew).

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