Skip to content

Instantly share code, notes, and snippets.

@doctorallen
Created October 2, 2015 16:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doctorallen/dcbf02a91096882157ce to your computer and use it in GitHub Desktop.
Save doctorallen/dcbf02a91096882157ce to your computer and use it in GitHub Desktop.
Project Switching Alfred Workflow
require_once('workflows.php');
$wf = new Workflows();
$query = "{query}";
$wf->search($query);
echo $wf->toxml();
#!/usr/bin/env bash
. ~/.bashrc
subl /var/www/{query}
open "/var/www/{query}"
osascript -e 'tell application "iTerm" to activate end tell'
osascript -e 'tell application "iTerm"
set newWindow to (create window with default profile)
end tell'
osascript -e 'tell application "iTerm" to tell current session of first window to write text "p {query}"'
<?php
class Workflows
{
private $path;
public function __construct()
{
$this->path = '/var/www/';
$this->results = array();
}
public function search($query)
{
$list = array();
foreach ($this->getProjectsList() as $project) {
if (strpos($project, $query) !== false) {
$list[] = array(
'title' => $project,
'uid' => $project,
'subtitle' => $this->path . $project,
'valid' => 'yes',
'autocomplete' => $project,
'arg' => $project,
);
}
}
$this->results = $list;
}
public function getProjectsList()
{
return preg_split('/[\r\n]+/ ', shell_exec('ls ' . $this->path));
}
/**
* Description:
* Convert an associative array into XML format
*
* @param $a - An associative array to convert
* @param $format - format of data being passed (json or array), defaults to array
* @return - XML string representation of the array
*/
public function toxml( $a=null, $format='array' ) {
if ( $format == 'json' ):
$a = json_decode( $a, TRUE );
endif;
if ( is_null( $a ) && !empty( $this->results ) ):
$a = $this->results;
elseif ( is_null( $a ) && empty( $this->results ) ):
return false;
endif;
$items = new SimpleXMLElement("<items></items>"); // Create new XML element
foreach( $a as $b ): // Lop through each object in the array
$c = $items->addChild( 'item' ); // Add a new 'item' element for each object
$c_keys = array_keys( $b ); // Grab all the keys for that item
foreach( $c_keys as $key ): // For each of those keys
if ( $key == 'uid' ):
$c->addAttribute( 'uid', $b[$key] );
elseif ( $key == 'arg' ):
$c->addAttribute( 'arg', $b[$key] );
elseif ( $key == 'type' ):
$c->addAttribute( 'type', $b[$key] );
elseif ( $key == 'valid' ):
if ( $b[$key] == 'yes' || $b[$key] == 'no' ):
$c->addAttribute( 'valid', $b[$key] );
endif;
elseif ( $key == 'autocomplete' ):
$c->addAttribute( 'autocomplete', $b[$key] );
elseif ( $key == 'icon' ):
if ( substr( $b[$key], 0, 9 ) == 'fileicon:' ):
$val = substr( $b[$key], 9 );
$c->$key = $val;
$c->$key->addAttribute( 'type', 'fileicon' );
elseif ( substr( $b[$key], 0, 9 ) == 'filetype:' ):
$val = substr( $b[$key], 9 );
$c->$key = $val;
$c->$key->addAttribute( 'type', 'filetype' );
else:
$c->$key = $b[$key];
endif;
else:
$c->$key = $b[$key];
endif;
endforeach;
endforeach;
return $items->asXML(); // Return XML string representation of the array
}
}
@doctorallen
Copy link
Author

alfred_preferences

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment