Jump to content

Conditionals for length of arguments


Recommended Posts

I am using Alfred to run a bash script after taking X amount of arguments from Alfred (using the split args to vars utility). I am using sys in my Python program which I have added conditionals to deal with any amount of arguments. My question is if anyone knows the best way to use conditionals (or some better method) in Alfred to detect how many arguments were given, and then to be able to use that amount of arguments to execute the python file 

 

right now I have to deal with each amount of potential inputs like

/pathtofile/myfile.py "$split1" "$split2"

/pathtofile/myfile.py "$split1" "$split2" "$split3"

/pathtofile/myfile.py "$split1" "$split2" "$split3" "$split4"

 

 

Edited by fwdalpha
Link to comment
2 hours ago, fwdalpha said:

detect how many arguments were given

 

In Alfred 5 there is a Get Number of Arguments Automation Task. It outputs a number, so you need to first keep your arguments in a variable and split them when you need to.


But it seems like the proper solution would be to have the python script deal with the multiple numbers of arguments. To avoid the XY problem, could you explain what the script does and why is the number of arguments important? Why not, for example, count the number of arguments inside the script (len(sys.argv))?

Link to comment

That new Get Number of Arguments functionality will be useful. I have a solution I can use python to solve for this, but was trying to figure out if Alfred had a way to do it. My script appends X number of argument strings to json file into a certain array. The easy way is to just send the entire argument as one string with each argument delimited by a comma and then split it using python based on that, and append accordingly. The whole idea is that I have to write "$var:1" "$var:2" etc for each possible argument I might give to the python file. So if I had 8 arguments, there would need to be 8 of those already written to store the arguments I gave Alfred. That's way to repetitive. Anyways, the simple fix is to just send it all as one argument (delimited for python to parse) unless you see something I don't. 

Link to comment

Ah, I see what you‘re doing. Yes, there’s a way simpler (and more correct) solution. In Alfred 5, Split Args to Vars is now Split Args. Use that and in Output as: set it to Arguments. Then in the run script you just do /pathtofile/myfile.py "${@}" (make sure you have with input as argv set).

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