diff --git a/index.js b/index.js index bfd5a7c..f24e64f 100644 --- a/index.js +++ b/index.js @@ -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) => { diff --git a/media/screenshot-var-desc-1.png b/media/screenshot-var-desc-1.png new file mode 100644 index 0000000..af46fdb Binary files /dev/null and b/media/screenshot-var-desc-1.png differ diff --git a/media/screenshot-var-desc-2.png b/media/screenshot-var-desc-2.png new file mode 100644 index 0000000..f3f6d02 Binary files /dev/null and b/media/screenshot-var-desc-2.png differ diff --git a/media/screenshot-var-desc-3.png b/media/screenshot-var-desc-3.png new file mode 100644 index 0000000..52390f6 Binary files /dev/null and b/media/screenshot-var-desc-3.png differ diff --git a/readme.md b/readme.md index cd3d70f..0dced6c 100644 --- a/readme.md +++ b/readme.md @@ -234,6 +234,76 @@ alfy.output( +###### 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. + + + +When we type abc@gmail.com, it gives us below notification. + + + +And this is the result of entering only abc (not valid email). + + + +###### 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/).