skyzyx/alfred-workflow-builder

Workflow Builder for Alfred 2

1.0.5 2013-07-04 23:56 UTC

This package is auto-updated.

Last update: 2024-03-06 14:39:57 UTC


README

Alfred Workflow Builder is a PHP class for creating workflows with Alfred 2. This class provides functions for working with plist settings files, reading and writing data to files, generating Alfred feedback results, and more.

Installation

Composer is the recommended way to install is package. Composer is dependency management tool for PHP that allows you to declare the dependencies your project needs and installs them into your project.

  1. Add skyzyx/alfred-workflow-builder as a dependency in your project's composer.json file.

    {
        "require": {
            "skyzyx/alfred-workflow-builder": "1.0.*"
        }
    }
  2. Download and install Composer.

    curl -s "http://getcomposer.org/installer" | php
  3. Install your dependencies.

    php composer.phar install --optimize-autoloader
  4. Require Composer's autoloader. Composer also prepares an autoload file that's capable of autoloading all of the classes in any of the libraries that it downloads. To use it, just add the following line to your code's bootstrap process.

    require 'vendor/autoload.php';

The original version of this class (written by David Ferguson) had methods for things like caching data to local files and fetching remote data over HTTP. Instead, we recommend you use Guzzle, Requests or Buzz for HTTP requests and Doctrine Cache for local file system caching. If you'd also like logging, we recommend Monolog.

Alfred\Workflow

use Alfred\Workflow;

// Pass a Bundle ID
$w = new Workflow('com.ryanparman.my-workflow');
#=> <Alfred\Workflow>

string toXML()

Accepts a properly formatted array or json object and converts it to XML for creating Alfred feedback results. If results have been created using the result() function, then passing no arguments will use the array of results created using the result() function.

Example using result function

$w->result(array(
    'uid'          => 'itemuid',
    'arg'          => 'itemarg',
    'title'        => 'Some Item Title',
    'subtitle'     => 'Some item subtitle',
    'icon'         => 'icon.png',
    'valid'        => 'yes',
    'autocomplete' => 'autocomplete'
));
echo $w->toXML();

Example using array

$results = array();
$temp = array(
    'uid'          => 'itemuid',
    'arg'          => 'itemarg',
    'title'        => 'Some Item Title',
    'subtitle'     => 'Some item subtitle',
    'icon'         => 'icon.png',
    'valid'        => 'yes',
    'autocomplete' => 'autocomplete'
);
array_push($results, $temp);
echo $w->toXML($results);

Result

<?xml version="1.0"?>
<items>
    <item uid="itemuid" arg="itemarg" autocomplete="autocomplete">
        <title>Some Item Title</title>
        <subtitle>Some item subtitle</subtitle>
        <icon>icon.png</icon>
    </item>
</items>

array mdfind()

Executes an mdfind command and returns results as an array of matching files.

$results = $w->mdfind('"kMDItemContentType == com.apple.mail.emlx"');
/* or */
$results = $w->mdfind('Alfred 2.app');
#=> (array) ['/Applications/Alfred 2.app']

You can learn more about querying the OS X metadata service by checking out:

array result()

Creates a new result item that is cached within the class object. This set of results is available via the results() functions, or, can be formatted and returned as XML via the toXML() function.

Key Usage
uid

Unique ID for the search result. (Required)

arg

Argument for this result. This will get fed into any downstream actions. (Required)

title

The main title for the result. (Required)

subtitle

The subtitle for the result. (Required)

icon

The icon that this result should have. This should typically be icon.png. (Required)

valid

If you press enter with this result selected, should it trigger downstream actions? Valid values are "yes", "no", true and false. The default value is "yes".

autocomplete

If you press enter with this result selected, what value should pop up as an autocomplete value? (Movies is a good usage example.)

Example

$w->result(array (
    'uid'          => 'alfred',
    'arg'          => 'alfredapp',
    'title'        => 'Alfred',
    'subtitle'     => '/Applications/Alfred.app',
    'icon'         => 'fileicon:/Applications/Alfred.app',
    'valid'        => 'yes',
    'autocomplete' => 'Alfredapp',
));
echo $w->toXML();

Result

<?xml version="1.0"?>
<items>
    <item uid="alfred" arg="alfredapp" autocomplete="Alfredapp">
        <title>Alfred</title>
        <subtitle>/Applications/Alfred.app</subtitle>
        <icon type="fileicon">/Applications/Alfred.app</icon>
    </item>
</items>

Alfred\Storage\Plist

use Alfred\Storage\Plist;

// Pass a Bundle ID and Plist name
$plist = new Plist('com.ryanparman.my-workflow', 'info');
#=> <Alfred\Storage\Plist>

string setValue()

Stores a key-value pair.

$plist->setValue('username', 'rparman');

string setValues()

Stores a series of key-value pairs.

$plist->setValues(array(
    'username' => 'rparman',
    'password' => 'abc123',
    'zipcode'  => '90210',
));

string getValue()

Retrieves the value of a key.

$username = $plist->getValue('username');
#=> (string) rparman

More!

You can learn more about Alfred 2 Workflows by checking out http://support.alfredapp.com/workflows.

You can also deconstruct some workflows that are built with Alfred Workflow Builder.