Alfred Command Line Parameter Parsing

alfred

#1

I have an Alfred (Powerpack) workflow which I invoke by Alt+Space and then typing e.g. c f 2 -h where the 2 is an always-present numeric value (that changes from invocation to invocation) and the -h is a switch that might not be present.

This invokes some Python which expects to see at least the first of these two parms, if not both.

What is the best way to parse command line parameters in Alfred such that they can be passed to a Python script?


#2

Alfred is going to pass the query string and you’ll need to parse it. But with only one flag and variable it might be easier to just create two keywords one for the -h and one without. Or a list filter with both options.

I thought there was a pinned post on this topic in the Alfred forums but I can’t come up with it now. Here’s a post re the same question but in AppleScript. The language is different but issues are the same:


#3

Thanks @dfay.

It has also occurred to me that rather “c v” (and its companions “c s” and “c m”) I could trigger on “cv” and what I pass to the python script is just the rest - " 2 -h".

It would mean, I guess, coding cv, cm and cs as separate workflows


#4

I typically just select the option with input as argv and then parse the input as I normally would in a python script I’d be running from the command line.