Workflows Class

Workflows

A PHP utility 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, requesting remote data, and more.

Download

Initialization

To initialize the class object it, include it in your php file, and the instantiate the new object.

Example

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

Methods

Bundle

Returns the current workflow bundle.

Example

1
$bundle = $w->bundle();
$bundle = $w->bundle();

Result

1
com.jdfwarrior.rdio
com.jdfwarrior.rdio

Cache

Returns the path to the cache folder for the current workflow.

Example

1
$cache = $w->cache();
$cache = $w->cache();

Result

1
/Users/ferg/Library/Caches/com.runningwithcrayons.Alfred-2/Workflow Data/com.jdfwarrior.rdio
/Users/ferg/Library/Caches/com.runningwithcrayons.Alfred-2/Workflow Data/com.jdfwarrior.rdio

Data

Returns the path to the data folder for the current workflow.

Example

1
$data = $w->data();
$data = $w->data();

Result

1
/Users/ferg/Library/Application Support/Alfred 2/Workflow Data/com.jdfwarrior.rdio
/Users/ferg/Library/Application Support/Alfred 2/Workflow Data/com.jdfwarrior.rdio

Path

Returns the path to the workflow.

Example

1
$path = $w->path();
$path = $w->path();

Result

1
/Users/ferg/Library/Application Support/Alfred 2/Alfred.alfredpreferences/workflows/com.jdfwarrior.rdio
/Users/ferg/Library/Application Support/Alfred 2/Alfred.alfredpreferences/workflows/com.jdfwarrior.rdio

Home

Returns the path to the current users home directory

Example

1
$home = $w->home();
$home = $w->home();

Example

1
/Users/ferg
/Users/ferg

Toxml

Accepts a properly formatted array or json object and converts it to XML for creating Alfred feedback results. Both parameters are optional. If results have been created using the result() function, then passing no arguments will use the array of results created using the result() function. Arrays passed in must be an associative array with array key values for the following required values: uid, arg, title, subtitle and icon. You may also pass array key=>value pairs for the following optional keys: valid and autocomplete. 

Example using result function

1
2
$w->result( 'itemuid', 'itemarg', 'Some Item Title', 'Some item subtitle', 'icon.png', 'yes', 'autocomplete' );
echo $w->toxml();
$w->result( 'itemuid', 'itemarg', 'Some Item Title', 'Some item subtitle', 'icon.png', 'yes', 'autocomplete' );
echo $w->toxml();

Example using array

1
2
3
4
5
6
7
8
9
10
11
12
$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 );
$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

1
2
3
4
5
6
7
8
<?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>
<?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>

Set

The set function will allow you to set/save values to a specified plist file allowing you to store easily store data for your workflow.

This function can be passed data in two different ways. Either as a key string, value, and filename to save to or, as an array and the filename to save to. If you are setting a single value, the first method is slightly easier. In the first method, $a would be the key name that you wish to set, $b would be the value of the key, and $c would be the filename to write to.

In the second method, $a would be an associative array of key=>value pairs, and $b would be the filename to save the data to.

Example passing key, value, and file

1
$w->set( 'username', 'jdfwarrior', 'settings.plist' );
$w->set( 'username', 'jdfwarrior', 'settings.plist' );

Example passing array and file

1
2
$temp = array( 'username' => 'jdfwarrior', 'password' => 'mypassword' );
$w->set( $temp, 'settings.plist' );
$temp = array( 'username' => 'jdfwarrior', 'password' => 'mypassword' );
$w->set( $temp, 'settings.plist' );

Get

Returns the value of the specified field ($a) from the specified plist ($b).

If only a filename is provided, the function will check the data path, then cache path, then local path. If the file is not found, the function will return false.

Example

1
$w->get( 'username', 'settings.plist' );
$w->get( 'username', 'settings.plist' );

Result

1
jdfwarrior
jdfwarrior

Request

Performs a curl request on the url specified ($url). cURL options can be passed as an associative array in the $options argument. See here for a list of available cURL options.

Example

1
$data = $w->request( 'http://google.com' );
$data = $w->request( 'http://google.com' );

Mdfind

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

Example

1
2
3
$results = $w->mdfind('"kMDItemContentType == com.apple.mail.emlx"');
or
$results = $w->mdfind('Alfred 2.app');
$results = $w->mdfind('"kMDItemContentType == com.apple.mail.emlx"');
or
$results = $w->mdfind('Alfred 2.app');

Result

1
2
3
array (
  0 => '/Applications/Alfred 2.app',
)
array (
  0 => '/Applications/Alfred 2.app',
)

Write

Similar to set() except data is dumped as text to a file. If the passed in data is an array, the data is json_encoded and written to the output file.

Example

1
$w->write( 'This is a sample string', 'savedData.txt' );
$w->write( 'This is a sample string', 'savedData.txt' );

Read

Opposite of write(). This function reads data from the specified file and returns it. If the data read is json, a json object is returned. Otherwise, it will return string data.

Example

1
$data = $w->read( 'savedData.txt' );
$data = $w->read( 'savedData.txt' );

Result

1
This is a sample string
This is a sample string

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.

Autocomplete value is optional. If no value is specified, it will take the value of the result title. Possible values for $valid are ‘yes’ and ‘no’ to set the validity of the result item.

Example

1
2
$w->result( 'alfred', 'alfredapp', 'Alfred', '/Applications/Alfred.app', 'fileicon:/Applications/Alfred.app', 'yes', 'Alfredapp' );
echo $w->toxml();
$w->result( 'alfred', 'alfredapp', 'Alfred', '/Applications/Alfred.app', 'fileicon:/Applications/Alfred.app', 'yes', 'Alfredapp' );
echo $w->toxml();

Result

1
2
3
4
5
6
7
8
<?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>
<?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>

Results

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.

Autocomplete value is optional. If no value is specified, it will take the value of the result title. Possible values for $valid are ‘yes’ and ‘no’ to set the validity of the result item.

Example

1
2
$w->result( 'alfred', 'alfredapp', 'Alfred', '/Applications/Alfred.app', 'fileicon:/Applications/Alfred.app', 'yes', 'Alfredapp' );
echo var_export( $w->results() );
$w->result( 'alfred', 'alfredapp', 'Alfred', '/Applications/Alfred.app', 'fileicon:/Applications/Alfred.app', 'yes', 'Alfredapp' );
echo var_export( $w->results() );

Result

1
2
3
4
5
6
7
8
9
10
11
12
array (
  0 => 
  array (
    'uid' => 'alfred',
    'arg' => 'alfredapp',
    'title' => 'Alfred',
    'subtitle' => '/Applications/Alfred.app',
    'icon' => 'fileicon:/Applications/Alfred.app',
    'valid' => 'yes',
    'autocomplete' => 'Alfredapp',
  ),
)
array (
  0 => 
  array (
    'uid' => 'alfred',
    'arg' => 'alfredapp',
    'title' => 'Alfred',
    'subtitle' => '/Applications/Alfred.app',
    'icon' => 'fileicon:/Applications/Alfred.app',
    'valid' => 'yes',
    'autocomplete' => 'Alfredapp',
  ),
)

As XML

1
2
3
4
5
6
7
8
<?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>
<?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>