Jump to content

Can Alfred process multiple query like this - keyword "123456789" "text with spaces"?


Recommended Posts

Hello,

 

I'm on the verge of purchasing the PowerPack because I'm hoping the following is possible, and if it is possible I'll buy it, but I just need help implementing it please. :)

 

I would like to feed the following query into Alfred to launch a URL to a website:

 

"subscribe 123456789 Reminder phrase with multiple spaces"

 

The first two will always be in the same format, with subscribe being the keyword and the number being the reference. The text after the number will vary and will have multiple spaces, but needs to be treated as the second query that can be any size (though not more than 255 chars).

 

Keyword - "subscribe"

Query1 - "<an ID with no spaces>"

Query2 - "A text phrase with multiple spaces"

 

The URL that needs to be generated looks like this:

https://domain.com/subscription/sub?s&what=data&id={query1}&note={query2}

e.g.

https://domain.com/subscription/sub?s&what=data&id=123456789&note=Reminder%20phrase%20with%20multiple%20spaces

I'm no good at scripting so I'm hoping someone wouldn't mind helping out with this if possible please?

 

Thanks!

Edited by jarrah
Link to comment

Hello,

 

I'm on the verge of purchasing the PowerPack because I'm hoping the following is possible, and if it is possible I'll buy it, but I just need help implementing it please. :)

 

I'm no good at scripting so I'm hoping someone wouldn't mind helping out with this if possible please?

 

Thanks!

 

This is entirely possible with the Powerpack - you can split arguments any way you like and formulate URLs. I actually have a few nice examples of doing just this, so let me know if you buy the Powerpack and I'll share them with you :)

 

Cheers,

Andrew

Link to comment

Sounds great, thank you Andrew and Vitor!

 

PowerPack is now purchased and I'm very appreciative and looking forward to your help. This should save me around 10 clicks and 3 lots of extra text entry so can't wait! :)

 

Thanks for buying!

 

There are plenty of ways of splitting args, and this is dependent on the script language you use. Here is a nice simple example workflow of splitting arguments and opening a url using bash:

 

https://dl.dropboxusercontent.com/u/6749767/Alfred/Workflows/Kitten%20Image.alfredworkflow

 

Let me know if you need any help modifying this for your purpose :)

 

Cheers,

Andrew

Link to comment

Thanks for pointing me in the right direction Andrew.

 

After a bit of Google'ing I found the following solution works for me:

 

#!/bin/bash


alfin="{query}"


idnum=`echo $alfin | awk '{print $1}'`
descrip=$(echo $alfin | cut -d " " -f2-)


open "https://domain.com/subscription/sub?s&what=data&id=$idnum&note=$descrip"
Edited by jarrah
Link to comment

You’re mixing a lot of different syntaxes and commands, using different approaches to do the same thing. You also have unnecessary elements for it to work as a workflow. It should work as you have it now, but if you want to make it cleaner and more maintainable, I suggest something like (without changing yours too much)

alfin="{query}"

idnum=$(echo "${alfin}" | cut -d ' ' -f1)
descrip=$(echo "${alfin}" | cut -d ' ' -f2-)

open "https://domain.com/subscription/sub?s&what=data&id=$idnum&note=$descrip"

 
You might also have some trouble in the future, if your phrase ever uses a character that needs to be url encoded. For that to work, change the line descrip=$(echo "${alfin}" | cut -d ' ' -f2-) to descrip=$(echo "${alfin}" | cut -d ' ' -f2- | perl -MURI::Escape -wlne 'print uri_escape ($_)'). That last bit will take your characters and encode them so they can be used as part of a url.

Edited by Vítor
Link to comment
  • 1 month later...

I've been looking into how Alfred can open custom Web Searches in a specific browser and found a useful post that said to use the Workflow template "Open custom URL in specified browser", and this works great. 

 

However, how do I do the same for the script in this post? The "open" command is tied to the script because of the variables, so not sure how I can pass that to another object that says open Chrome for example.

 

Thanks.

Link to comment

I've been looking into how Alfred can open custom Web Searches in a specific browser and found a useful post that said to use the Workflow template "Open custom URL in specified browser", and this works great. 

 

However, how do I do the same for the script in this post? The "open" command is tied to the script because of the variables, so not sure how I can pass that to another object that says open Chrome for example.

 

Thanks.

 

You can specify an app with the open command. By just telling it to open a URL it will open it with the default web browser. You could however do something like this...

open -a /Applications/Google\ Chrome.app http://yahoo.com

to open a url in Chrome.

Link to comment
  • 6 months later...

Thanks for this topic. I've got an additional question: how to remove diacritic marks in queries using commands of this bash script? I've been messing with commands such as sed and tr, but to no avail.

 

Apart from this, some may find useful my tweaking with the script: I've discovered that for this script to work, the option of 'escape spaces' has to be unchecked.

Link to comment

Does no one know an answer?

 

At least tell me how to add to this script a snippet with a php code? In php, which I know a bit, I believe this getting rid of diacritics may be easier achieved.

 

By the way, I've tried yet another bash method, which I googled fortunately, namely that of iconv command, but again to no avail:

e.g. following that: http://stackoverflow.com/questions/1975057/bash-convert-non-ascii-characters-to-ascii

Link to comment
  • 3 years later...

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