Jump to content

Web Search : allow basic {query} string manipulation


Recommended Posts

Hello,

 

I love the "Web Search" functions but I'm really missing a simple way to "manipulate" the {query} string parameter...

 

e.g.: lots of my "Web Search" are based on a URL, so my {query} parameter is something like this "http://www.alfredforum.com/index.php"...

 

but my "Web search" (i.e. the service I'm using) only needs :

 

- the URL without http://www. in front of it

OR

- the root domain

- ...etc

 

Unfortunately there is no way to do basic string manipulation with the "Web Search" functionality... so I have to manually delete the "http://www."everytime which is time consuming and defeats the purpose of Alfred...

 

Could you add such a function ??

 

Thanks,

 

Cheers,

Link to comment
Share on other sites

You can do this with a workflow easily enough.

 

Keyword -> Run Script -> Open URL.

 

URLs can be manipulated much more powerfully in Python or Ruby than via some additional options in the Web Search dialog.

 

Here are a few examples in Python:

from urlparse import urlsplit, urlunsplit

url = "{query}"
parsed = urlsplit(url)

# Domain
print(parsed.netloc)

# Without scheme://
print(urlunsplit(('',) + parsed[1:]).lstrip('/'))

# Hostname
print(parsed.hostname)

# Resolve URL redirects / Expand shortened URLs
import urllib2
print(urllib2.urlopen(url).geturl())
Edited by deanishe
Link to comment
Share on other sites

  • 2 weeks later...

You put url = "{query}" at the top of your script, and print the altered URL. This script takes the URL the user entered and expands any shortened URLs.

from urlparse import urlsplit, urlunsplit
from urllib2 import urlopen

url = "{query}"

# Expand short URL/resolve redirects
print(urlopen(url).geturl())
Edited by deanishe
Link to comment
Share on other sites

Thanks, but somehow I still can't connect the dots...

 

I would have expectected to be able to "pass" the manipulated string to the next block "Open URL", but that doesn't seems possible :

 

http://screencast.com/t/uKPwhljiwkxP

 

It's so basic it's driving me crazy not to have this in the standard doc (well if it is, I haven't found it, AlfredApps example workflows are lame lame lame... and don't help in anyway).

 

Any idea ?

 

Thanks,

 

Cheers,

Link to comment
Share on other sites

From a Run Script action, any output to STDOUT is sent to the next action as {query}.
 
That means, as demonstrated above, what you print or echo in your script becomes {query} for the next action.
 
In the case of Script Filters, the arg parameter of their results becomes the {query} of the following action.

Link to comment
Share on other sites

So it's sent to the next action without actually having to connect the "script" box with the "open URL" Box ??

 

I still can't get this to work.

 

http://screencast.com/t/pCXg1dsDy

 

I'm using this as script:

 

from urlparse import urlsplit, urlunsplit

url = "{query}"
parsed = urlsplit(url)

# Hostname
print(parsed.hostname)

 

in an attempt to the OpenURL with the hostname instead of the long URL but it doesn't seem to work.

 

Another tip ? :)

 

Thanks !

Link to comment
Share on other sites

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