Queries are a way to search for information in your plugin.
They are the main way users interact with your plugin,
and they are the main way your plugin can provide information to the user.
Responding to Queries
To respond to a query, you need to define a method in your plugin class that is decorated with the @FlowPlugin.Search
decorator. That method must return data to display.
For now, we’ll just return a string to ensure that everything’s working.
With this code your plugin will always return one result with the title “Hello,
world!” and the icon that is specified in your plugin.json file.
Let’s say you want to return a different result based on the search text.
You can do that by adding a parameter of type Query to your method.
Query has several properties:
raw: string — the full text that the user entered
isReQuery: boolean — whether the query is a re-query
search: string — the text that the user entered after the action keyword and after the startsWith string from the
@FlowPlugin.Search decorator.
terms: string[] — same as search, but split into an array of words
actionKeyword: string — the action keyword that the user entered
regexpMatches: RegExpMatchArray — the result of the regular expression match. If you didn’t use the regex
parameter in the @FlowPlugin.Search decorator, this will be undefined
By default, the @FlowPlugin.Search decorator is case-insensitive.
If you want to make it case-sensitive, you can set the caseSensitive property to true:
Sometimes simple text matching is not enough. In this case, you can use regular expressions to match the search text.
Let’s write a plugin that will accept two integers and perform a simple operation on them.
You can use the matched groups to extract the numbers from the search text.
If the user input matches your regular expression, the query object will have a regexpMatches property that contains
the result of the calling userInput.match(yourRegex). Please note that if you’re using regular expressions, the type
of the first argument your method accepts must be RegexpQuery instead of Query.
Perfect! The result is the same, but now we can refer to the matched groups by their names.
Often, when the user selects a result, you want to perform an action.
You can do that by returning an object with the action property set.
The value of the property must be either the return value of one of the methods in this.actions,
or the result of running a custom action defined by you. Let’s take a look at both:
When selecting this result, the user will have example.com opened in the browser they have
specified in Flow Launcher’s settings. If you don’t want Flow Launcher to hide after invoking one of the built-in
actions, you can call the .dontHide() method on the return result of this method:
Sometimes you need to perform an action that is not built into Flow Launcher or several built-in actions in a row.
To do this, you can define your own method in your class and decorate it with the @FlowPlugin.Action decorator.
Inside this method, you can also call the built-in actions using this.api. They’re always async, even if they don’t
return any value, so you need to mark your method as async and await them. Your custom action must always return
a boolean value indicating whether Flow Launcher window should be hidden after the action is performed. true will
hide it, false will keep it open.
Context menus are a way to provide additional actions for a result.
In Flow Launcher, you can call a context menu on a result by pressing Shift + Enter.
To define a context menu, you need to define a method in your plugin class and decorate it with the
@FlowPlugin.ContextMenu decorator. This method must return the same type of data as methods decorated with the
@FlowPlugin.Search decorator do: a string, a number, an object, or an array of strings, numbers, or objects.