<?php

require_once __DIR__ . '/vendor/autoload.php';
use Alfred\Workflow;
use Guzzle\Http\Client;
$wf = new Workflow('com.ryanparman.workflow.packagist');
$http = new Client();
$int = 0;
$query = isset($argv[1]) ? $argv[1] : "{query}";
$request = $http->get("https://packagist.org/search.json?q=" . urlencode($query));
$response = $request->send()->json();
foreach ($response['results'] as $result) {
    $int++;
    $wf->result(array('uid' => $result['name'] . time(), 'arg' => $result['url'], 'title' => $result['name'], 'subtitle' => $result['description'], 'icon' => __DIR__ . '/icon.png', 'valid' => 'yes', 'autocomplete' => 'autocomplete'));
}
if (count($wf->results) === 0) {
    $wf->result(array('uid' => 'none', 'arg' => $query, 'title' => 'No results', 'subtitle' => 'No results found.', 'icon' => __DIR__ . '/icon.png', 'valid' => 'no'));
}
echo $wf->toXML();
<?php

require_once __DIR__ . "/vendor/autoload.php";
use Alfred\Workflow;
use Cake\Collection\Collection as C;
use CFPropertyList\CFPropertyList;
$w = new Workflow();
$query = isset($argv[1]) ? trim($argv[1]) : false;
//glob all textexpander files named group_...
$snippets = (new C(glob(getenv('HOME') . '/Library/Application Support/TextExpander/Settings.textexpandersettings/group*.xml')))->map(function ($file) {
    return (new CFPropertyList($file))->toArray()['snippetPlists'];
})->unfold()->filter(function ($s) {
    return strlen($s['abbreviation']) > 0;
})->filter(function ($s) use($query) {
    if (!$query) {
        return true;
    }
    return (new C(['abbreviation', 'label', 'plainText']))->some(function ($k) use($s, $query) {
        return mb_stripos($s[$k], $query) !== false;
    });
});
if ($snippets->isEmpty()) {
    //404 if collection is empty
    $results = [['title' => '404 Not Found', 'icon' => false, 'valid' => 'yes']];
} else {
    //build results array
    $results = $snippets->map(function ($s) {
        return ['uid' => $s['uuidString'], 'arg' => $s['abbreviation'], 'title' => $s['label'] ? $s['label'] : $s['abbreviation'], 'subtitle' => $s['abbreviation'], 'icon' => false, 'valid' => 'yes'];
    })->toList();
}
//set results
<?php

require_once __DIR__ . '/vendor/autoload.php';
use Alfred\Workflow;
$wf = new Workflow('com.ryanparman.workflow.manpages');
$int = 0;
$query = isset($argv[1]) ? $argv[1] : "{query}";
$query = stripslashes($query);
$pieces = explode(' ', $query);
$command = array_shift($pieces);
$args = implode('+', $pieces);
$url = "http://www.explainshell.com/explain/{$command}";
if ($args) {
    $url .= "?args={$args}";
}
$wf->result(array('uid' => time(), 'arg' => $url, 'title' => "Explain command for `{$command}`", 'subtitle' => '$ ' . $query, 'icon' => __DIR__ . '/icon.png', 'valid' => 'yes'));
echo $wf->toXML();
<?php

require_once __DIR__ . '/vendor/autoload.php';
use Alfred\Workflow;
use Skyzyx\Components\Mimetypes\Mimetypes;
$bundle_id = 'com.ryanparman.workflow.mime';
$wf = new Workflow($bundle_id);
$int = 0;
$query = $argv[1];
$mimes = Mimetypes::getInstance()->getMimeTypes();
$extensions = array_keys($mimes);
$matching_extensions = array_filter($extensions, function ($extension) use($query, $mimes, $wf, &$int) {
    $int++;
    if (preg_match('/' . $query . '/i', $extension)) {
        $wf->result(array('uid' => "{$extension}-{$query}-{$int}", 'arg' => $mimes[$extension], 'title' => $extension . ': ' . $mimes[$extension], 'subtitle' => 'Copy to clipboard', 'icon' => __DIR__ . '/icon.png'));
        return true;
    }
    return false;
});
if (count($matching_extensions) === 0) {
    $wf->result(array('uid' => 'none', 'arg' => $query, 'title' => 'No results', 'subtitle' => 'No results found.', 'icon' => __DIR__ . '/icon.png', 'valid' => 'no'));
}
echo $wf->toXML();
<?php

require_once __DIR__ . '/vendor/autoload.php';
use Alfred\Workflow;
use Guzzle\Http\Client;
$wf = new Workflow('com.ryanparman.workflow.redbox');
$http = new Client();
$query = stripslashes($argv[1]);
$request = $http->get("https://content.atomz.com/autocomplete/sp10/04/a1/b7/?query=" . urlencode($query));
$r = $request->send()->getBody(true);
// Do some cleanup
$r = trim(substr(trim($r), 1, -1));
$r = preg_replace('/\\s+/i', ' ', $r);
$r = json_decode($r, true);
function uc($s)
{
    $s = ucwords($s);
    $s = str_ireplace('blu-ray', 'Blu-ray', $s);
    return $s;
}
if (count($r) > 0) {
    foreach ($r as $i => $result) {
        $result = uc($result);
        $wf->result(array('uid' => sha1($query . $i . time()), 'arg' => rawurlencode($result), 'title' => $result, 'subtitle' => "Search Redbox for \"{$result}\"", 'icon' => __DIR__ . '/icon.png', 'valid' => 'yes'));
    }
} else {
    $wf->result(array('uid' => sha1($query . $i . time()), 'arg' => rawurlencode($query), 'title' => 'No results found for "' . uc($query) . '"', 'subtitle' => "Try using fewer keywords.", 'icon' => __DIR__ . '/icon.png', 'valid' => 'no'));
}
echo $wf->toXML();
<?php

require_once __DIR__ . "/vendor/autoload.php";
use Alfred\Workflow;
if ($argc < 2) {
    exit;
}
$w = new Workflow();
$query = $argv[1];
//find files
$find = $w->mdfind($query);
//remove empty last one
array_pop($find);
//check results
if (count($find)) {
    //build result array
    $results = array_map(function ($file) {
        return ['uid' => $file, 'arg' => $file, 'title' => basename($file), 'subtitle' => $file, 'icon' => false, 'valid' => 'yes', 'autocomplete' => 'autocomplete'];
    }, $find);
} else {
    //build not found msg
    $results = [['uid' => '', 'arg' => '', 'title' => '404 Not Found', 'subtitle' => '', 'icon' => false, 'valid' => 'yes', 'autocomplete' => '']];
}
//set results
$w->results = $results;
//return alfred's xml
echo $w->toXML();
<?php

require_once __DIR__ . '/vendor/autoload.php';
use Alfred\Workflow;
use Guzzle\Http\Client;
$wf = new Workflow('com.ryanparman.workflow.geo');
$http = new Client();
$int = 0;
$query = $argv[1];
$request = $http->get("http://freegeoip.net/json/" . urlencode($query));
$r = $request->send()->json();
extract($r);
$wf->result(array('uid' => 'geocity' . time(), 'arg' => "{$city}, {$region_name}, {$country_name}", 'title' => "{$city}, {$region_name}, {$country_name}", 'subtitle' => "Data for {$ip}", 'icon' => __DIR__ . '/icon.png', 'valid' => 'no', 'autocomplete' => $ip));
$wf->result(array('uid' => 'geolatlong' . time(), 'arg' => "{$latitude},{$longitude}", 'title' => "Lat/Long: {$latitude},{$longitude}", 'subtitle' => "Copy to clipboard", 'icon' => __DIR__ . '/icon.png', 'valid' => 'yes'));
$wf->result(array('uid' => 'geoip' . time(), 'arg' => $ip, 'title' => "Public IP: {$ip}", 'subtitle' => "Copy to clipboard", 'icon' => __DIR__ . '/icon.png', 'valid' => 'yes'));
$wf->result(array('uid' => 'geomap' . time(), 'arg' => "https://maps.google.com/maps?t=v&z=12&ll={$latitude},{$longitude}", 'title' => "Show map of {$latitude},{$longitude}", 'subtitle' => "Open in Google Maps", 'icon' => __DIR__ . '/icon.png', 'valid' => 'yes'));
echo $wf->toXML();