Skip to content

Commit

Permalink
Support setting Alfred variables with alfy.output (sindresorhus#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
C. Thomas Bailey committed Jan 12, 2017
1 parent c21377f commit 979febc
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 3 deletions.
18 changes: 16 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,22 @@ alfy.alfred = {

alfy.input = process.argv[2];

alfy.output = arr => {
console.log(JSON.stringify({items: arr}, null, '\t'));
const isDefined = x => x !== null && x !== undefined;

const formatItemArg = item => {
if (isDefined(item) && isDefined(item.variables)) {
const alfredworkflow = {arg: item.arg, variables: item.variables};
item.arg = JSON.stringify({alfredworkflow});
delete item.variables;
}
};

alfy.output = items => {
if (isDefined(items) && items.forEach) {
items.forEach(formatItemArg);
}

console.log(JSON.stringify({items}, null, '\t'));
};

alfy.matches = (input, list, item) => {
Expand Down
Binary file added media/screenshot-variable.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 28 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ Return output to Alfred.

Type: `Array`

List of `Object` with any of the [supported properties](https://www.alfredapp.com/help/workflows/inputs/script-filter/json/).
List of `Object` with any of the [supported properties](https://www.alfredapp.com/help/workflows/inputs/script-filter/json/). In addition, if a list item has a `variables` property, it will be used to set Alfred's [Workflow Environment Variables](https://www.alfredapp.com/help/workflows/advanced/variables/) if the user selects the item.

Example:

Expand All @@ -179,6 +179,33 @@ alfy.output([{

<img src="media/screenshot-output.png" width="694">

Using the `variables` property:

```js
alfy.output([{
title: 'Unicorn',
arg: '🦄',
variables: {color: 'white'}
}, {
title: 'Rainbow',
arg: '🌈',
variables: {color: 'myriad'}
}]);
```

You can access Alfred Workflow Variables through `process.env`:

```js
// After a user selects "Unicorn" or "Rainbow"
process.env.color
//=> 'white' if they selected Unicorn
//=> 'myriad' if they selected Rainbow
```

Alfred Workflow Variables are also available in the workflow editor using the form `{var:varname}`.

<img src="media/screenshot-variable.png" width="694">

#### matches(input, list, [item])

Returns an `Array` of items in `list` that case-insensitively contains `input`.
Expand Down
76 changes: 76 additions & 0 deletions test/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import test from 'ava';
import hookStd from 'hook-std';
import {alfy} from './_utils';

const m = alfy();

test.cb('.output() properly wraps item.variables', t => {
const unhook = hookStd.stdout({silent: true}, output => {
unhook();
const item = JSON.parse(output).items[0];
const arg = JSON.parse(item.arg);
t.deepEqual(arg, {
alfredworkflow: {
arg: '🦄',
variables: {fabulous: true}
}
});
t.end();
});
m.output([{
title: 'unicorn',
arg: '🦄',
variables: {fabulous: true}
}]);
});

test.cb('.output() wraps item.variables even if item.arg is not defined', t => {
const unhook = hookStd.stdout({silent: true}, output => {
unhook();
const item = JSON.parse(output).items[0];
const arg = JSON.parse(item.arg);
t.deepEqual(arg, {
alfredworkflow: {
variables: {fabulous: true}
}
});
t.end();
});
m.output([{
title: 'unicorn',
variables: {fabulous: true}
}]);
});

test.cb('.output() does not wrap item.arg if item.variables is not defined', t => {
const unhook = hookStd.stdout({silent: true}, output => {
unhook();
const item = JSON.parse(output).items[0];
t.is(item.arg, '🦄');
t.is(item.variables, undefined);
t.end();
});
m.output([{
title: 'unicorn',
arg: '🦄'
}]);
});

test('.output() accepts null and undefined items', t => {
const unhook = hookStd.stdout({silent: true}, () => unhook());
t.notThrows(() => m.output([undefined, null]));
});

test('.output() accepts non-arrays', t => {
let done = false;
const unhook = hookStd.stdout({silent: true}, () => {
if (done) {
unhook();
}
});
t.notThrows(() => m.output({}));
t.notThrows(() => m.output('unicorn'));
t.notThrows(() => m.output(null));
done = true;
t.notThrows(() => m.output(undefined));
});

0 comments on commit 979febc

Please sign in to comment.