Jump to content

Pass Script Output to Script Filter


Recommended Posts

Hi,

 I'm trying to create a workflow that takes an Keyword argument, passes that argument to a script (as an input argument), and then uses that input argument to query an SQLlite databases for matches. The items in the database are a Title and URL of particular web pages I have saved over the years. I then want to display the results in Alfred so I can select the URL (List Filter?) and then launch Firefox to go to the URL selected.

  I have done simple keyword searches to bash scripts workflows in the past sucessfully so I have an idea how those work.

 I have the script working, testing from the command line and it actualy outputs the JSON format that I belive that Alfred wants. My question is, now what? :)

 Looking at the templates, they show examples of Script Filter objects going to a bash script. I think I want to do the reverse. The results from the script will be different each time, so I don't think a List FIlter object will work.

 

 So what I think the work flow should be: Keyword Search-> Bash Script -> Script Filter -> (Display Results) -> Launch Browser with Selection

 

 Do I have this right?

 

Ken

Link to post
18 minutes ago, KennyBoy said:

Do I have this right?

 

Not quite. You appear to have misunderstood the way Script Filters work. They're basically your first four steps (Keyword -> Display Results) all in one. The script in a Script Filter should receive the user's query from Alfred (via ARGV) and print corresponding results in Alfred's JSON format to STDOUT. Alfred will call your script repeatedly as the query changes.

 

Alternatively, you can select the "Alfred filters results" option, in which case Alfred doesn't pass your script a query. Instead, your script should output all items at once, and Alfred filters them.

Link to post

Thanks for the reply.

 

 So, here is the script, and I have placed in a Script Filter object. Any example I have found, typically uses a langauge (AppleScript, Ruby, Java) that I am not familiar with, but all of them seem to just out put the script results in JSON format and send them to another object (List Filter, Copy to Clipboard, etc). I'm sorry, I guess I'm just not grasping what I should do with the output at this point. Any pointers, would greatly be appreciated.

 

this_match=$1

 

while read line
do

        results+=("$line")
done < <(sqlite3 /Users/storage/sqlite_databases/bookmark.sqlite "select b.title, a.url, a.title from moz_places a, moz_bookmarks b where a.id=b.fk AND b.title LIKE '%${this_match}%' OR a.title LIKE '%${this_match}%' order by a.url,a.title,b.title " | sed 's/|//2')

 

echo '{"items": ['
echo '{'

 

for i in "${results[@]}"
do
       
        IFS="|" read name url <<< "$i"
        echo '"title"': ${name},
        echo '"arg"': ${url},
done


echo '}'
echo ']}'

 

 

And here is the output

 

{"items": [
{
"title": Alfred Blog - Productivity Tips & Tricks and Alfred News,
"arg": https://www.alfredapp.com/blog/,
"title": Tips and Tricks - Alfred Blog,
"arg": https://www.alfredapp.com/blog/category/tips-and-tricks/,
"title": Five Quick Ways To Start New Time-Saving Habits With Alfred - Alfred Blog,
"arg": https://www.alfredapp.com/blog/tips-and-tricks/5-quick-ways-to-get-into-new-time-saving-habits/,
"title": 7 Workflow Tips for Alfred 3 - Alfred Blog,
"arg": https://www.alfredapp.com/blog/tips-and-tricks/7-workflow-tips-for-alfred-3/,
"title": Perform File Actions Easily from Alfred or Finder - Alfred Blog,
"arg": https://www.alfredapp.com/blog/tips-and-tricks/file-actions-from-alfred-or-finder/,
"title": Tutorial: Creating the Fastest Hotkey-Based Web Search - Alfred Blog,
"arg": https://www.alfredapp.com/blog/tips-and-tricks/tutorial-fastest-hotkey-based-search/,
}
]}

 

Ken

Link to post
48 minutes ago, KennyBoy said:

but all of them seem to just out put the script results in JSON format and send them to another object

 

That's not how a Script Filter works.

 

Alfred reads the JSON from your Script Filter's script and shows the results to the user. If the user selects a result, the value of that item's arg is sent to the next element (Copy to Clipboard etc.).

 

A Script Filter that isn't connected to anything generally isn't very useful because it only shows stuff, but doesn't do anything with it.

 

48 minutes ago, KennyBoy said:

So, here is the script

 

Please don't post scripts. Upload the entire workflow somewhere (Dropbox?) and post a link, so we can see what's really going on. We generally can't say why a workflow doesn't work without the workflow.

 

48 minutes ago, KennyBoy said:

And here is the output

 

Except in this case your JSON is obviously invalid. Alfred is certainly showing an error message in its debugger that says as much. You have to quote the values, not just the keys, each title-arg pair needs to be in its own object, and you also mustn't have a trailing comma after the last entry in an array or object.

 

The JSON should look more like this:

 

{
    "items": [
        {
            "arg": "https:\/\/www.alfredapp.com\/blog\/",
            "title": "Alfred Blog - Productivity Tips & Tricks and Alfred News"
        },
        {
            "arg": "https:\/\/www.alfredapp.com\/blog\/category\/tips-and-tricks\/",
            "title": "Tips and Tricks - Alfred Blog"
        }
    ]
}

 

You shouldn't even try to cobble your own JSON together in bash, tbh: it's almost impossible to get it right (not least because of the trailing comma rule). Use a proper scripting language that has real support for JSON. Python, Ruby etc.

 

Edited by deanishe
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...