Jump to content

Returning array values to Alfred with PHP


Recommended Posts

Hi there,

 

I've been trying to get a workflow that's sort of like text expansion working, but with a specific task of storing URLs for our internal sites.

 

For example, I don't want to have to maintain a giant list of inputs and outputs to clipboard for this - I'd rather have something like:

 

Input: ,fa <space> <keywords>

 

In the array would be:

 

$data = array(

  'Production Server 1' => 'http://www1.production'

  'Production Server 2' => 'http://www2.production'

  'Staging Server 1' => 'http://www1.staging'

  'Staging Server 2' => 'http://www2.staging'

);

 

So for example I'd like to start typing `,fa pro` and have it return the two production servers to the list as their names, and when selecting one have the URL from the data be sent to clipboard.

 

I had a working version but nothing would get returned to Alfred to let me select - I would have to either type out the whole string (e.g. 'Production Server 1'), or have short codes (i.e. instead of Production Server 1 have ps1).

 

Any help on this would be much appreciated!

Link to comment

Hi there,

 

I've been trying to get a workflow that's sort of like text expansion working, but with a specific task of storing URLs for our internal sites.

 

For example, I don't want to have to maintain a giant list of inputs and outputs to clipboard for this - I'd rather have something like:

 

Input: ,fa <space> <keywords>

 

In the array would be:

 

$data = array(

  'Production Server 1' => 'http://www1.production'

  'Production Server 2' => 'http://www2.production'

  'Staging Server 1' => 'http://www1.staging'

  'Staging Server 2' => 'http://www2.staging'

);

 

So for example I'd like to start typing `,fa pro` and have it return the two production servers to the list as their names, and when selecting one have the URL from the data be sent to clipboard.

 

I had a working version but nothing would get returned to Alfred to let me select - I would have to either type out the whole string (e.g. 'Production Server 1'), or have short codes (i.e. instead of Production Server 1 have ps1).

 

Any help on this would be much appreciated!

 

You can't return array values to Alfred. Alfred accepts feedback via XML. You could use something like my Workflows class (found here) to make adding objects as results a little easier. As for searching the array and showing only certain results, I would suggest something like this..(I've used it in several workflows now).

$data = array(
  'Production Server 1' => 'http://www1.production'
  'Production Server 2' => 'http://www2.production'
  'Staging Server 1' => 'http://www1.staging'
  'Staging Server 2' => 'http://www2.staging'
);

$filter = "{query}";

foreach( $data as $name => $site ):
  if ( $filter && ( strpos( strtolower( $site ), strtolower( $site ) ) !== false || strpos( strtolower( $name ), strtolower( $name ) ) !== false ) ):
    $w->result( 'saved.site', $site, $name, $site, 'icon.png', 'yes', $name );
  endif;
endforeach;

$w->toxml();

 

May want to check over that. I typed it all in the editor here very quickly with no syntax highlighting :) Using my workflows class that should take input from the user loop through your sites and only show the ones that match the input in the name or url, and then create feedback results of those items. Creating feedback items requires the use of a Script Filter, not just a keyword.

 

Check full XML feedback syntax here

Link to comment

Thanks very much - the if statement might need fixed as it's returning all results - but other than that it's working as I expected (you had $name twice in the strpos)

 

Awesome job :)

 

edit:

 

here is my updated code:

 

require_once('workflows.php');
$w = new Workflows();

$data = array(
  'Web 1 Production Site' => 'http://www1.production',
  'Web 2 Production Site' => 'http://www2.production',
  'Web 1 Staging Site' => 'http://www1.staging',
  'Web 2 Staging Site' => 'http://www2.staging',
);

$filter = "{query}";

foreach( $data as $name => $site ):
  if ( $filter && ( strpos( strtolower( $site ), strtolower( $filter ) ) !== false || strpos( strtolower( $name ), strtolower( $filter ) ) !== false ) ):
    $w->result( 'saved.site', $site, $name, $site, 'icon.png', 'yes', $name );
  endif;
endforeach;

echo $w->toxml();
Edited by tanepiper
Link to comment

Thanks very much - the if statement might need fixed as it's returning all results - but other than that it's working as I expected (you had $name twice in the strpos)

 

Awesome job :)

 

edit:

 

here is my updated code:

 

require_once('workflows.php');
$w = new Workflows();

$data = array(
  'Web 1 Production Site' => 'http://www1.production',
  'Web 2 Production Site' => 'http://www2.production',
  'Web 1 Staging Site' => 'http://www1.staging',
  'Web 2 Staging Site' => 'http://www2.staging',
);

$filter = "{query}";

foreach( $data as $name => $site ):
  if ( $filter && ( strpos( strtolower( $site ), strtolower( $filter ) ) !== false || strpos( strtolower( $name ), strtolower( $filter ) ) !== false ) ):
    $w->result( 'saved.site', $site, $name, $site, 'icon.png', 'yes', $name );
  endif;
endforeach;

echo $w->toxml();

 

Haha yeah as I said I was in a heck of a hurry when I typed it up. I was about to run out the door and was trying to get SOMETHING out to you so you could get going :)

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