Skip to content
PHP helper class for Alfred Workflows
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.

README.md

Alfred 3 Workflows PHP Helper

Latest Version Software License Build Status Total Downloads

This package simplifies PHP development for Alfred 3 workflows.

Installation

composer require joetannenbaum/alfred-workflow

Usage

To understand the following properties, please reference the official Alfred 3 documentation.

The library is not doing any validation for required properties, so all of the following are optional. Please refer to the documentation above for required properties. All of the properties will default to the official defaults if excluded.

use Alfred\Workflows\Workflow;

require 'vendor/autoload.php';

$workflow = new Workflow;

// add variables
$workflow->variable('fruit','apple')
         ->variable('vegetables','carrots');

$workflow->result()
            ->uid('bob-belcher')
            ->title('Bob')
            ->subtitle('Head Burger Chef')
            ->quicklookurl('http://www.bobsburgers.com')
            ->type('default')
            ->arg('bob')
            ->valid(true)
            ->icon('bob.png')
            ->mod('cmd', 'Search for Bob', 'search')
            ->text('copy', 'Bob is the best!')
            ->autocomplete('Bob Belcher');

$workflow->result()
            ->uid('linda-belcher')
            ->title('Linda')
            ->subtitle('Wife')
            ->quicklookurl('http://www.bobsburgers.com')
            ->type('default')
            ->arg('linda')
            ->valid(true)
            ->icon('linda.png')
            ->mod('cmd', 'Search for Linda', 'search')
            ->text('largetype', 'Linda is the best!')
            ->autocomplete('Linda Belcher');

echo $workflow->output();

Results in:

{
  "items": [
    {
      "arg": "bob",
      "autocomplete": "Bob Belcher",
      "icon": {
        "path": "bob.png"
      },
      "mods": {
        "cmd": {
          "subtitle": "Search for Bob",
          "arg": "search",
          "valid": true
        }
      },
      "quicklookurl": "http://www.bobsburgers.com",
      "subtitle": "Head Burger Chef",
      "text": {
        "copy": "Bob is the best!"
      },
      "title": "Bob",
      "type": "default",
      "uid": "bob-belcher",
      "valid": true
    },
    {
      "arg": "linda",
      "autocomplete": "Linda Belcher",
      "icon": {
        "path": "linda.png"
      },
      "mods": {
        "cmd": {
          "subtitle": "Search for Linda",
          "arg": "search",
          "valid": true
        }
      },
      "quicklookurl": "http://www.bobsburgers.com",
      "subtitle": "Wife",
      "text": {
        "largetype": "LINDA IS THE BEST!"
      },
      "title": "Linda",
      "type": "default",
      "uid": "linda-belcher",
      "valid": true
    }
  ],
  "variables": {
    "fruit": "apple",
    "vegetables": "carrots"
  }
}

Helper Methods

Just for clarity, some helper methods exist.

// This...
$workflow->result()->mod('cmd', 'Search for Bob', 'search');
// ...is the same as this.
$workflow->result()->cmd('Search for Bob', 'search');
// And these are all available as well:
$workflow->result()->shift('Search for Bob', 'search');
$workflow->result()->fn('Search for Bob', 'search');
$workflow->result()->ctrl('Search for Bob', 'search');
$workflow->result()->alt('Search for Bob', 'search');
// This...
$workflow->result()->text('largetype', 'Linda is the best!')
// ...is the same as this.
$workflow->result()->largetype('Linda is the best!');
// Also works:
$workflow->result()->copy('Linda is the best!');
// This...
$workflow->result()->icon('bob.png', 'fileicon')
// ...is the same as this.
$workflow->result()->fileiconIcon('bob.png')
// Also works:
$workflow->result()->filetypeIcon('bob.png')

Sorting

If you'd like to sort your results:

// Default is by title asc:
$workflow->sortResults();
// Title desc:
$workflow->sortResults('desc');
// By property asc:
$workflow->sortResults('asc', 'subtitle');

Filtering

You can filter your results as well if Alfred isn't doing it for you:

Please note this is a very simple filtering, literally looking for the string within the string. For anything more complex filter before creating results.

// Default is searching in title:
$workflow->filterResults('bob');
// By property:
$workflow->filterResults('bob', 'subtitle');
You can’t perform that action at this time.