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 that is marked with the [Search] attribute.
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 an ExtendedQuery parameter to your method.
ExtendedQuery has two properties:
string Search — the text that the user entered after the action keyword
string[] SearchTerms — the text that the user entered after the action keyword, split into an array of words
By default, the [Search] attribute is case-insensitive.
If you want to make it case-sensitive, you can set the stringComparison property to something else, like this:
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.
To do this,
give your method parameters appropriate types (int and string in this case)
and name them _N where N is the index of the capturing group.
The first capturing group is available as the parameter called _1, the second group as _2, etc.
While this does work, it’s not the most readable code when you have to use capturing group indexes as variable names.
Let’s improve this by using named capturing groups.
Functionally, it works exactly the same way, but now the code is much more readable.
Regex Options
Just like the [Search] attribute with text matching,
regular expressions in this attribute are case-insensitive by default.
You can change this behavior by specifying regexOptions parameter.
Often, when the user selects a result, you want to perform an action.
You can do that by returning an ExtendedResult with the Action or ActionAsync property set.
This property must be a function that returns bool or an async function that returns bool.
If the return value is true, Flow Launcher will close the search window.
If it’s false, the search window will stay open after performing the action.
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.
If you want to use context menus, you need to do four things:
Mark your class as partial.
Define a method that returns your context menu. Its return type is similar to the return type of the [Search]
method, i.e., you can return a string, a list of strings, an array, or anything else described above on this page.
Mark this method with the [ContextMenu] attribute.
Use this method prefixed with Create. If this method’s name doesn’t include the words “context” or “menu”,
you will also have to add ContextMenu to the end of the method name.
Let’s go over the code now.
Context Menu Example
Let’s create a plugin that can show a context menu with two options: with the search text uppercased and lowercased.