Jump to content

Getting Args from Query (Script Filter)


Recommended Posts

Hi,

 

I've got the following (truncated  here to show 2 results) json in a script filter. I'm passing out two args:
 

{
  "items": [ 
	{ "title": "Business", 
     "subtitle": "Practice", 
     "arg": ["Business", "B5F64C25-6C8D-4771-A9D7-7A86F7D98B95"], 
     "match": "Business " }, 
    { "title": "Design Services", 
     "subtitle": "Business", 
     "arg": ["Services", "4A7B6A86-460D-4C90-8E20-82DD061E0016"], 
     "match": "Design Services " 
    } 
  ] 
}

 

I'm outputted multiple arguments based on this:

Quote

While optional, it's highly recommended that you populate arg as it's the string which is passed to your connected output actions. If excluded, you won't know which result item the user has selected.

It is also possible to pass multiple arguments via an array of strings:

"arg": ["~/Desktop", "~/Pictures"]

https://www.alfredapp.com/help/workflows/inputs/script-filter/json/

 

How do I parse the arg into two variables I can access later in my script? I've tried a split arg with newline as delimiter, which isn't working.

 

Can I access them directly (without split arg)? `{var:1}` or `{var:var2}` doesn't seem to work either.

 

Edited by c-c-c-c
added more contex
Link to comment

You’re conflating two different concepts. Either send separate arguments then read them back as arguments from their position or set them both as variables instead of arguments (you can do that per item) and then read them as variables.

 

To be more precise in the instructions, I’ll need to know more about your workflow and what languages you’re using.

Link to comment

Yes you're right, I'm conflating arguments/variables (I thought they were the same and couldn't find any documentation clarifying the difference).

 

Attaching a screenshot of the workflow.

 

Ultimately, what I'm curious about is how to use arguments downstream in my workflow: 

 

Given I'm passing this (in a bash script filter):

"arg": ["Services", "4A7B6A86-460D-4C90-8E20-82DD061E0016"]

 

How do I access those arguments in a copy to clipboard or open url action:

 

to generate a markdown link in copy to clipboard:

[{Services <-- what is the syntax to echo the first argument }](x-devonthink-item://{4A7B6A86-460D-4C90-8E20-82DD061E001 <-- what is the syntax to echo the 2nd argument

 

to generate a url in open url:

x-devonthink-item://{4A7B6A86-460D-4C90-8E20-82DD061E0016 <-- what is the syntax to echo the 2nd argument}?reveal=1

 

I've solved this by avoiding (multiple) arguments all together and using @deanishe's excellent guide Workflow/environment variables in Alfred — deanishe.net

and outputted a variable instead in my script filter:

 

(excerpt)

{
"title": "Design Services",
"subtitle": "Business",
"arg": ["Design Services"],
"variables": {"uuid": "4A7B6A86-460D-4C90-8E20-82DD061E0016"},
"match": "Design Services "
},

 

and accessing the variable (var:uuid) and first arg(query) downstream using:

 

[{query}](x-devonthink-item://{var:uuid})

 

but how would I do this using only arguments? 

 

Screen Shot 2023-02-23 at 9.31.45 AM.png

Link to comment
1 hour ago, c-c-c-c said:

Ultimately, what I'm curious about is how to use arguments downstream in my workflow

 

It depends entirely on the language used. When you have a Run Script of Script filter and change the language in the dropdown, you’ll see how to get the first argument, getting the second and others is usually a matter of incrementing the number.


On the other objects you can only get one at a time through {query}. This has the advantage that you could for example send multiple arguments to a single Open URL and it would open multiple URLs, replacing {query} each time with each argument.

 

1 hour ago, c-c-c-c said:

and accessing the variable (var:uuid) and first arg(query) downstream using

 

Yes, that’s a good solution. You could have also defined two variables and no argument.

Link to comment
37 minutes ago, vitor said:

On the other objects you can only get one at a time through {query}.

 

Ah got it - this is what was tripping me up. Thanks for the explanation. I thought there might be a possibility of accessing the individual arguments within a workflow object (copy to clipboard, open url, et.)

 

It might be helpful if the example in the help be amended with that caveat (or an explanation of the difference between args and vars):

 

https://www.alfredapp.com/help/workflows/inputs/script-filter/json/

  Quote

While optional, it's highly recommended that you populate arg as it's the string which is passed to your connected output actions. If excluded, you won't know which result item the user has selected.

It is also possible to pass multiple arguments via an array of strings:

"arg": ["~/Desktop", "~/Pictures"]

 

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