Jump to content

Workflow presents options, then opens URL based on chosen option


Recommended Posts

Hey guys!

 

I am not a programmer at all, but I am trying to create a workflow for something I use a lot.

What the workflow should do is after typing a keyword it presents a list of options (around 35). based on what option I choose it opens a specific URL.

 

I managed to create the list of options after the keyword, but now I am stuck at finding a way to open a specific URL based on the chosen option/query.

 

is there a way similar to this, or am I completely on the wrong way?

 

if {query}="option1"

then

  open -a Safari http://url1.com

elif {query}="option2"

then

  open -a Safari http://url2.com

else

  open a- Safari http://url3.com

fi

 

Thank you!

 

Link to comment

Hey guys!

 

I am not a programmer at all, but I am trying to create a workflow for something I use a lot.

What the workflow should do is after typing a keyword it presents a list of options (around 35). based on what option I choose it opens a specific URL.

 

I managed to create the list of options after the keyword, but now I am stuck at finding a way to open a specific URL based on the chosen option/query.

 

is there a way similar to this, or am I completely on the wrong way?

 

if {query}="option1"

then

  open -a Safari http://url1.com

elif {query}="option2"

then

  open -a Safari http://url2.com

else

  open a- Safari http://url3.com

fi

 

Thank you!

 

This can be done multiple ways, it just kinda depends on what exactly you want it to do. If you just want to scroll through the list of options, you could do this without a workflow by just creating numerous web searches that use the same keyword. That is the easiest thing to do. If you wanted to filter the results by typing a portion of the name, you would use a workflow. So, how would you like this to function?

Link to comment

Thank you so far! I want to filter the results, by typing part of the name. But this already works. I already have a working script filter that gives me a list of options I can scroll through or search in by typing a portion of the name.

 

What I want to do though is after hitting enter on a specific option in the list it opens a certain URL. The script filter passes the chosen option to the "run script" part (this works already. if I just add "large type" as output instead of a script, it shows the chosen option in large type) and then the "run script" choses what URL to access based on the input it gets from the script filter.

 

I hope that clears it up a bit

Link to comment

Sorry, I had to run earlier, hence the short explanation.

 

The workflow that I uploaded (dropbox link), is a simple implementation. I wrote it in PHP using my Alphred library. Let me take you through it. The script filter is just a few lines:

<?php
require_once( __DIR__ . '/Alphred.phar' );

$workflow = new Alphred([ 'error_on_empty' => true ]);

$sites = json_decode( file_get_contents( __DIR__ . '/sites.json' ), true );

if ( isset( $argv[1] ) ) {
	$sites = $workflow->filter( $sites, trim( $argv[1] ), 'name' );
}

foreach ( $sites as $site ) :
	$workflow->add_result([
	               'title' => 'Open ' .  $site['name'],
	               'subtitle' => $site['url'],
	               'arg' => $site['url']
	]);
endforeach;

$workflow->to_xml();

And the sample JSON file that I used:

[
  {
    "name": "Google",
    "url": "http://www.google.com"
  },
  {
    "name": "Packal",
    "url": "http://www.packal.org"
  }
]

Let me take you through it.

 

The $workflow->filter is part of the Alphred library, and it just filters out anything that doesn't match what you've typed into the query ($argv[1] is the query). Then, the foreach loop just cycles through all of the items that are defined in the JSON file and displays what matches (or displays all if there is no query). As an added bonus, the $workflow->filter will also sort the results so the matches will be sorted via best and descending. The $workflow->add_result just adds an item to what will be the script filter. The key part is that the `arg` is the url. This works because the workflow is setup so that the script filter leads to an "Open URL" action.

 

You can add more items to the JSON file by replicating the same structure. Just note, however, that JSON can get a bit finicky. If you open the sites.json file with TextEdit, make sure that you're using plain text because TextEdit will try to convert the quotation marks to smart quotes, which will break the JSON file. Also, make sure that the commas are in the correct places; otherwise, the JSON will break.

 

Does this accomplish what you need, and do you see how you can expand it?

Link to comment
  • 1 year later...

Hey @rice.shawn - thanks for this! 

 

I just hacked it a little to serve as a way of maintaining a simple system of text keywords/tags that I can paste into documents whenever needed. It fixes something I've been trying to get my head around for ages— a way to insert tags into text documents in a way that will also work on an iOS device (i.e. not OpenMeta, not app dependent, just plain text keywords in the body of the document). 

 

I'm wondering how difficult it would be to script a function that would allow someone to add keywords to the json file on the fly?

 

I've managed to get as far as adding a "write_tag.php" file that contains the following: 

 

<?php
require_once( __DIR__ . '/Alphred.phar' );

if ( isset( $argv[1] ) ) {
	$tags = json_decode( file_get_contents( __DIR__ . '/tags.json' ), true );	
}

$tags[] = array('name'=>trim( $argv[1] ), 'tag'=>'@'.trim( $argv[1] ));

$json = json_encode($tags);

echo "$json";

I set this up through a keyword input to run script. I've figured out how to read the JSON, and I've almost figured out how to add an entry to the array, but I'm not getting the correct output, and I've yet to figure out how to write to the file. It's been a while since I've scripted anything with php, and this is my first time playing with an Alfred workflow. 

 

Thanks for any pointers...

Edited by jslr
Link to comment

Okay. Figured out my own solution yesterday, using a a keyword trigger ("+@") for a "run script" action containing the following: 

 

$name = trim("{query}");
$tag = "@".$name;

$file = file_get_contents('path-to-tags.json file', true);
    $data = json_decode($file,true);
    unset($file);

    $data[] = array('name' => $name, 'tag' => $tag);
    $result=json_encode($data);
    file_put_contents('path-to-tags.json file', $result);
    unset($result);

When I invoke Alfred, "@" pulls up a list of tags I've already pushed to the json file. If the tag doesn't already exist, I can cursor back to the beginning of the line and simply insert "+" to add it to the list.

 

I use this mostly in nvALT. It's better than text expansion because I don't have to remember exactly what the keywords are, thanks to filtering/auto-completion. While nvALT offers its own tagging system, tag keywords in text allow for compatibility with other text editors (for example, when I'm working on the same text files via iOS). Currently works like a dream. Probably wouldn't have arrived at this point without your solution as a starting point— apologies for hijacking the thread, but thanks so much for the solution. 

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