Jump to content

What is argv in a osascript (JavaScript) script filter?


Recommended Posts

The documentation says I should pass argv instead of query because argv is properly quoted. Now, when I pass argv, I seem to get all arguments to the filter as a single string in argv[0]. Is that assumption correct? If so, it might be worthwhile mentioning it in the documentation – it is very terse on (not only these) details.

 

And what exactly would be the difference between passing argv and query in this case (script filter with osascript/JavaScript)?

Link to comment
11 hours ago, chrillek said:

Now, when I pass argv, I seem to get all arguments to the filter as a single string in argv[0]. Is that assumption correct?


You’re not sending multiple arguments, you’re only sending one. The concept of making arguments separated by spaces is a convention of the shell. What you type in Alfred is like sending an argument to the shell quoted, which is the proper behaviour. Otherwise you’d have to quote the text or escape the space. If you want that, you can use {query} and not escape spaces, for instance. Alfred is not a Terminal, its window is not a shell prompt.

 

11 hours ago, chrillek said:

And what exactly would be the difference between passing argv and query in this case (script filter with osascript/JavaScript)?

 

It’s the same in every case: {query} requires that you do the escaping, and argv does not. The latter is like passing a properly escaped argument to the shell, the former gives extra flexibility (as explained above) at the expense of having to determine what will be escaped. argv is preferred unless you know you need {query} and that you can handle every edge case.

Link to comment
8 hours ago, chrillek said:

Which begs the question why argv is an array/list in the first place.


Because that’s how the programming languages work. They always see an array of arguments, regardless if you pass, zero, one, or fifty. Plus you can send multiple arguments at once to an object, for example by using a File Action Trigger.

 

8 hours ago, chrillek said:

The semantics are different.


They are not. Again, Alfred is not a shell prompt. If you have a text field in a webpage or app and type something in it, that will be a single argument regardless of how many spaces it has. You don’t have to quote the text or escape characters for it to be that way.

 

Splitting on spaces is a shell convention, and even that is only a default. Shells separate on spaces, newlines, and tabs and you can change it to only include some of those or entirely different characters. It’s called the Input Field Separators.

 

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