jeffsui Posted August 4, 2016 Share Posted August 4, 2016 I'm having a difficulty with using Alfred to filter script results I have a python script that queries an internal wiki #!/usr/bin/env python from __future__ import print_function import sys from workflow.workflow3 import Workflow3 from workflow import web QUERY_URL = 'http://MYCOMPANY.ORG/mediawiki/api.php?action=opensearch&format=json&search={}&namespace=0&limit=50&suggest=' PAGE_URL = 'https://MYCOMPANY.ORG/mediawiki/index.php?search={}&title=Special%3ASearch' def main(wf): Workflow3().logger.info(wf.args[0]) Workflow3().logger.info("HI") query = str(wf.args[0]).replace(' ', '%20') results = web.get(QUERY_URL.format(query)).json() for keyword in results[1]: url = PAGE_URL.format(keyword.replace(' ', '+')) wf.add_item(keyword, '', arg=url, copytext=url, icon='logo.png', valid=True) wf.send_feedback() if __name__ == '__main__': wf = Workflow3() sys.exit(wf.run(main)) I have this in a script filter and if I run with bash and use the command: python wiki.py $1 Everything works fine. If i select "Alfred filter results" I get the following error func(self) File "wiki.py", line 13, in main query = wf.args[0].replace(' ', '%20') IndexError: list index out of range Is there something different in the way the script is being called if i have the Alfred filter results button pressed? I'm assuming the query parameter is perhaps passed differently? Any insight? Link to comment
deanishe Posted August 4, 2016 Share Posted August 4, 2016 There are a few things you're doing wrong there, I'm afraid.The actual error you're getting is because Alfred doesn't pass your script an argument if you select "Alfred filters results", and you haven't enclosed $1 in double quotes, so if there is no argument, your script gets no argument. If you use "$1", your script will receive an empty string in wf.args[0], and not die in flames.As a rule, you should always use "$1" (or "{query}") with the default escaping options. Anything else is broken in some way.All the Alfred-Workflow documentation is written with the assumption that you use "$1" or "{query}", so wf.args[0] will always exist, even if it's empty.With regard to other issues: There's no need to use Workflow3() anywhere but in if __name__ == '__main__':. Elsewhere, just use wf.logger.info(). The wf object is a Workflow3 instance, and is global to your script. Don't URL encode query before passing it to web.get(). web.py will do all of that for you. Just pass in the unicode query as-is. You do it like this: query = wf.args[0] if not query: # Show some default here? pass else: # No search parameter! url = 'https://MYCOMPANY.ORG/mediawiki/index.php?title=Special%3ASearch' # Don't try to encode `query`. The second argument is a dict of GET parameters. # web.py will automatically encode the parameters and add them to the URL. r = web.get(url, {'search': query}) # Re-raise any error if the request failed r.raise_for_status() results = r.json() You might want to look at the Alfred-Workflow tutorial.I'm not sure that combining a search engine workflow with "Alfred filters results" is what you're after. When you select "Alfred filters results", Alfred calls your workflow once only. It caches that set of results and captures the user query to filter those results. It won't call your script again until the user starts your workflow all over again.When you're calling a search engine, you generally want to let it do the filtering and just pass the results straight through. Link to comment
jeffsui Posted August 5, 2016 Author Share Posted August 5, 2016 Thanks for the assistance. Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now