Jump to content

How to pass a processed {query} to an open url action


Recommended Posts

Newbie question here: I have created a script filter to convert a {query} to lower case and replace https with http:

 

import sys

query = "{query}"

query = query.lower()

if query.startswith('https://'):
    query = query.replace('https://', 'http://')

sys.stdout.write('{"items":[{"title": "test", "args":"'+query+'"}]}')

 

Then, I'd like to pass the processed {query} to an open url action. (See the attached image below)

 

After I enter the keyword: ala a_url, and press enter, nothing happens. I can see the debug console printing out the correct JSON, but I am not sure how to pass the processed {query} to the "open url action", please help

截图 2016-10-30 23时25分17秒.jpg

Edited by Cheng
Link to comment

A Script Filter is the wrong solution. You should have a Keyword connected to a Run Script connected to Open URL. Yes, that is more nodes, but will actually be simpler code and will be more efficient since it won’t run with every character press.


When asking for help with your workflow please also post a link a to it. We’re not seeing the whole picture and the problem may be somewhere you’re not thinking of. Also give us your Alfred version.

Link to comment

You'd be better off just using a keyword input to capture the query, then run it through a Run Script which does your processing. This simplifies things as you just output the URL from the script which will be used as the input of the Open URL.

 

You could even do this without scripting using:

Keyword input -> Replace util (replace http) -> Process util (lower case) -> Open URL.

 

Cheers,

Andrew

Link to comment
22 minutes ago, Andrew said:

You'd be better off just using a keyword input to capture the query, then run it through a Run Script which does your processing. This simplifies things as you just output the URL from the script which will be used as the input of the Open URL.

 

You could even do this without scripting using:

Keyword input -> Replace util (replace http) -> Process util (lower case) -> Open URL.

 

Cheers,

Andrew

 

Really appreciate your help  "Keyword input -> Replace util (replace http) -> Process util (lower case) -> Open URL" => this one is much easier. The "Process util" is called "Transform" in my alfred.

Link to comment
23 minutes ago, vitor said:

A Script Filter is the wrong solution. You should have a Keyword connected to a Run Script connected to Open URL. Yes, that is more nodes, but will actually be simpler code and will be more efficient since it won’t run with every character press.


When asking for help with your workflow please also post a link a to it. We’re not seeing the whole picture and the problem may be somewhere you’re not thinking of. Also give us your Alfred version.

Thanks Vitor!

Link to comment

FWIW, simply replacing https with http is not super robust, as it's possible that https appears more than once in the URL, e.g.:

 

https://web.archive.org/web/*/https://www.alfredapp.com/

 

Arguably better to use a Run Script with a slightly-modified version of your original code that only replaces https:// at the start of the URL:

if query.startswith('https://'):
    query = 'http://' + query[8:]

sys.stdout.write(query)

 

Edited by deanishe
Link to comment
8 minutes ago, deanishe said:

FWIW, simply replacing https with http is not super robust, as it's possible that https appears more than once in the URL, e.g.:

 

https://web.archive.org/web/*/https://www.alfredapp.com/

 

Arguably better to use a Run Script with a slightly-modified version of your original code that only replaces https:// at the start of the URL

 

 

That’s a fair point, but that case can also be covered by the Replace utility if we make it a regex substitution:

U25lSjj.png

Link to comment
5 hours ago, deanishe said:

FWIW, simply replacing https with http is not super robust, as it's possible that https appears more than once in the URL, e.g.:

 

https://web.archive.org/web/*/https://www.alfredapp.com/

 

Arguably better to use a Run Script with a slightly-modified version of your original code that only replaces https:// at the start of the URL:


if query.startswith('https://'):
    query = 'http://' + query[8:]

sys.stdout.write(query)

Cool, How can I hook up a script with a open url action? As I tried to use a script filter but being told that it is not what script filter is for.

 

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