Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add root level variables option #122

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ alfy.alfred = {

alfy.input = process.argv[2];

alfy.output = (items, {rerunInterval} = {}) => {
console.log(JSON.stringify({items, rerun: rerunInterval}, null, '\t'));
alfy.output = (items, {rerunInterval, variables} = {}) => {
console.log(JSON.stringify({items, rerun: rerunInterval, variables}, null, '\t'));
};

alfy.matches = (input, list, item) => {
Expand Down
Binary file added media/screenshot-var-desc-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/screenshot-var-desc-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/screenshot-var-desc-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
70 changes: 70 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,76 @@ alfy.output(

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

###### variables

Type: `object`

###### 1. Root level variable

variables can be useful when passing variables in `script filter` to Alfred.

In alfred, variable includes `item-level variable` and `root-level variable`.

You can set a `root-level variable` by putting variables in this options object in the following ways:

Here's a `script filter` example using `root-level variable`.

```js
const alfy = require('alfy')
const [userID, serviceProvider] = alfy.input.split('@', 2)
const isValidEmail = /* some boolean value indicating input is valid. */

alfy.output(
[
{
title: `${alfy.input} is ${isValidEmail ? 'valid' : 'invalid'} email`,
arg: `${isValidEmail}`
}
],
{
variables: {
email: `${alfy.input}`,
userID,
serviceProvider
}
}
)
```

We can fetch these variables from this script by using `{var:email}`, `{var:userID}` like below screenshot.

<img src="media/screenshot-var-desc-1.png" width="694">

When we type [email protected], it gives us below notification.

<img src="media/screenshot-var-desc-2.png" width="694">

And this is the result of entering only abc (not valid email).

<img src="media/screenshot-var-desc-3.png" width="694">

###### 2. Item level variable

If the value of variables must be different depending on the item, you must use item-level variables like this way.

```js
const items = [];
items.push({
title: `${someInfo}`,
variables: {
title: `${someInfo}`
}
});
....

alfy.output(items);
```

Passed variables can be used like `{var:apiKey}` in both ways.

You can also use `{allVars}` to communicate all variables. [More info.](https://www.alfredapp.com/help/workflows/inputs/script-filter/json/)


#### log(value)

Log `value` to the [Alfred workflow debugger](https://www.alfredapp.com/help/workflows/advanced/debugger/).
Expand Down