|
| 1 | +# Custom Scripts |
| 2 | + |
| 3 | +> [!INFO] |
| 4 | +> This page is a modified version of the [Boop project](https://github.com/IvanMathy/Boop/blob/main/Boop/Documentation/CustomScripts.md) documentation |
| 5 | +
|
| 6 | +## Setup |
| 7 | + |
| 8 | +To use custom scripts, you need to go to the `Scripts` folder located in `...\flow-launcher\UserData\Plugins\Formatter-x.x.x`. |
| 9 | + |
| 10 | +## Writing Custom Scripts |
| 11 | + |
| 12 | +You can easily extend **Formatter** with custom scripts to add your own functionality. Each script is a self-contained Javascript file that is loaded at app launch. If you make something cool or useful, feel free to submit a PR and share it with everyone else! |
| 13 | + |
| 14 | +### Meta info |
| 15 | + |
| 16 | +Each script starts with a declarative JSON document, describing the contents of that file, a title, a description, and search tags. All that stuff is contained within a top level comment (with some extra asterisks) just like so: |
| 17 | + |
| 18 | +```js |
| 19 | +/** |
| 20 | + { |
| 21 | + "api":1, |
| 22 | + "name":"Add Slashes", |
| 23 | + "description":"Escapes your text", |
| 24 | + "author":"Ivan", |
| 25 | + "icon":"quote", |
| 26 | + "tags":"add,slashes,escape" |
| 27 | + } |
| 28 | +**/ |
| 29 | +``` |
| 30 | + |
| 31 | +- `api` is not currently used, but is strongly recommended for potential backwards compatibility. You should set it to 1. |
| 32 | +- `name`, `description` and `author` are exactly what you think they are. |
| 33 | +- `icon` is a visual representation of your scripts' actions. (is not currently used, but will can be used soon) |
| 34 | +- `tags` are used to filter and sort results. |
| 35 | + |
| 36 | +### The Main Function |
| 37 | + |
| 38 | +Your script must declare a top level `main()` function, that takes in a single argument of type `ScriptExecution`. This is where all of the magic happens. |
| 39 | + |
| 40 | +Your script will only be executed once, just like a web browser would at page load. Anytime your script's services are requested, `main()` will be invoked with a new execution object. |
| 41 | + |
| 42 | +Your script will be kept alive in its own virtual machine, retaining context and any potential global variables/functions you define. This means you need to be mindful of potentially generated garbage. |
| 43 | + |
| 44 | +```js |
| 45 | +function main(state) { |
| 46 | + // Do something useful here (or not) |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +### ScriptExecution |
| 51 | + |
| 52 | +The script execution object is a representation of the current state of the editor. This is how you communicate with the main app. A new execution object will be created and passed down to your script every time the user needs it. Once your `main()` returns, values are extracted from the execution and put back in the editor. |
| 53 | + |
| 54 | +Script executions are not exactly full Javascript objects, instead they're a proxy to the native `ScriptExecution` Plugin class that communicates with FLow Launcher. Property are actually dynamic getters and setters rather than stored values. Therefore, try to only use the values you need and store them in variables to avoid calling native code too often. |
| 55 | + |
| 56 | +#### Property |
| 57 | + |
| 58 | +At the moment, Script execution object have only one property to deal with text: `text`. |
| 59 | + |
| 60 | +- `text` will contain or set the entire string from the clipboard. |
| 61 | + |
| 62 | +```js |
| 63 | +let text = state.text; // get |
| 64 | +state.text = 'Replace the whole text in your clipboard'; // set |
| 65 | +``` |
0 commit comments