Jump to content

How do i convert this to Alfred ?


Recommended Posts

Posted

I am new to this, so sorry if I am a little short here.

How do I convert this to Alfred and return the data to alfred, and can open the URL via alfred ?

#!/usr/bin/python

# -*- coding: utf-8 -*-

import sys

import json

import urllib2

if len(sys.argv) < 2:

print ("Please add a search query")

sys.exit(1)

search = str(sys.argv[1])

req = urllib2.Request("http://example.com/?query="+search, None, {'user-agent':'json/'})

opener = urllib2.build_opener()

f = opener.open(req)

d = f.read()

u = json.loads(d)

if 'entries' in u.keys():

for info in u['entries']:

country = info['country']

zip = info['zip']

state = info['state']

name = info['name']

adress = info['adress']

url = 'http://example.com/s...kup/p?zip=' zip

print name.encode('utf-8')+" -", adresse.encode('utf-8'), zip, state.encode('utf-8')

Posted

In order to put it into Alfred, 

 

(1) make a blank workflow

(2) with a keyword input. On that, have "argument required" and "space".

(3) add a "execute a script" action, and change the language to python.

(4) Copy/paste the code above and change the argv variable (this is the input variable, right?) to "{query}" without the quotes.

(5) Where you have the "print" at the end, you could just make a system call to run the command "open {URL}" where url is what you just printed.

 

If you want to have some feedback, then put in an output "display notification" and make it so that it will open only if there is text in the {query} variable. Put the {query} variable in the body of the notification.

 

(6) Make sure you connect each step by clicking on the little nubs on the sides of the items and dragging to the next item so that you can properly chain them together.

 

That should be it.

Posted

Thank you for the fast replay. 


 


4. Should this be enough ? search = ({query})


How can I return the result in a list e.g like the default search result in Alfreds HUD?

 


And if i select one of the rows it will open the url in my default browser?

Posted

(4) I'm not so great with the python, so don't take me as authoritative, but search = str({query}) should work. Otherwise, you might need search = str("{query}"). Just try them and see what works.

 

To return things in the HUD is a bit more complicated. What you're looking for here is a Script Filter. The script filter needs returns an some XML to Alfred, which displays the results. Read more about script filters here. What's great is that a lot of people have put together libraries for Alfred workflows that people can use. Read this thread to find out more about the libraries. Since you're working in Python, you'll probably want to look into either Alp or Alfred-Python.

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