flowlauncher python插件开发
- requestments.txt
添加flowlauncher或者flox-lib
- 继承FlowLauncher 或者flox
使用flox的话,需要在Flow Launcher子目录下进行调试,不然会报错,找不到Launcher
- query(self, query)返回json数组
代码中不用要print调用,会导致解析异常
点击回调使用
"JsonRPCAction": {
"method": "copy_to_clipboard",
"parameters": [data]
}
- 添加lib到路径
import sys,os
parent_folder_path = os.path.abspath(os.path.dirname(__file__))
sys.path.append(parent_folder_path)
sys.path.append(os.path.join(parent_folder_path, 'lib'))
sys.path.append(os.path.join(parent_folder_path, 'plugin'))
- setting支持
import json
from functools import cached_property
class UUID(FlowLauncher):
@cached_property
def plugindir(self):
potential_paths = [
os.path.abspath(os.getcwd()),
os.path.dirname(os.path.abspath(os.path.dirname(__file__)))
]
for path in potential_paths:
while True:
if os.path.exists(os.path.join(path, 'plugin.json')):
return path
elif os.path.ismount(path):
return os.getcwd()
path = os.path.dirname(path)
@cached_property
def settings(self):
app_data=os.path.dirname(os.path.dirname(self.plugindir))
app_setting=os.path.join(app_data, 'Settings',"Plugins", self.__class__.__name__,'Settings.json')
with open(app_setting, 'r') as f:
return json.load(f)
# self.site_url = self.settings.get("site_url")
- 插件设置
SettingsTemplate.yaml
支持的类型参考:
body:
- type: input
attributes:
name: site_url
label: 'URL:'
description: Url of CyberChef
- 右键菜单
query返回的数据里面包含ContextData,数组
"ContextData": [model.fullname, model.model],
然后context_menu的参数data就是ContextData的值
def context_menu(self, data):
return [
{
"Title": "拷贝对外机种名",
"SubTitle": "拷贝对外机种名{}".format(data[0]),
"IcoPath": "Images/app.png",
"JsonRPCAction": {
"method": "copy_to_clipboard",
"parameters": [data[0]]
}
},
{
"Title": "拷贝编译机种名",
"SubTitle": "拷贝编译机种名{}".format(data[1]),
"IcoPath": "Images/app.png",
"JsonRPCAction": {
"method": "copy_to_clipboard",
"parameters": [data[1]]
}
}
]
参考:
- 搜索关键词高亮
返回的json结果包含TitleHighlightData,为int 数组类型,包含需要高亮的字符的index列表,如上,flowLauncher 高亮,则index_list=[0,1,2,3,4,5,6,7,8,9,10,11,12]
flow Launcher使用StringMatcher.FuzzySearch(query.Search, title).MatchData
flox-lib 中有一个对应的实现string_matcher函数生成matchdata
self.add_item(
title=title,
subtitle=subtitle,
icon="Images/app.ico",
method=self.open_url,
parameters=[guid],
TitleHighlightData=string_matcher.string_matcher(query,title).index_list
)
- 使用flox,启动异常
报异常
System.Text.Json.JsonException: The input does not contain any JSON tokens. Expected the input to start with a valid JSON token, when isFinalBlock is true. Path: $ | LineNumber: 0 | BytePositionInLine: 0.
对策:main函数加run()
if __name__ == "__main__":
your_class = YourClass()
your_class.run()
示例代码:https://github.com/minyoad/Flow-UUID/
# -*- coding: utf-8 -*-
import sys,os
parent_folder_path = os.path.abspath(os.path.dirname(__file__))
sys.path.append(parent_folder_path)
sys.path.append(os.path.join(parent_folder_path, 'lib'))
sys.path.append(os.path.join(parent_folder_path, 'plugin'))
from uuid import uuid4
import pyperclip
from flowlauncher import FlowLauncher,FlowLauncherAPI
class UUID(FlowLauncher):
def __init__(self):
self.generated_uuid = str(uuid4())
super().__init__()
def query(self, query):
items=[]
# lower case
items.append({
"Title":"小写:{}".format(self.generated_uuid),
"SubTitle": "Click to copy",
"IcoPath": "icon.png",
"JsonRPCAction": {
"method": "copy_to_clipboard",
"parameters": [self.generated_uuid]
}
})
# upper case
data=self.generated_uuid.upper()
items.append({
"Title":"大写:{}".format(data),
"SubTitle": "Click to copy",
"IcoPath": "icon.png",
"JsonRPCAction": {
"method": "copy_to_clipboard",
"parameters": [data]
}
})
# lower case
data=self.generated_uuid.replace('-','')
items.append({
"Title":"去掉[-]小写:{}".format(data),
"SubTitle": "Click to copy",
"IcoPath": "icon.png",
"JsonRPCAction": {
"method": "copy_to_clipboard",
"parameters": [data]
}
})
data=self.generated_uuid.upper().replace('-','')
items.append({
"Title":"去掉[-]大写:{}".format(data),
"SubTitle": "Click to copy",
"IcoPath": "icon.png",
"JsonRPCAction": {
"method": "copy_to_clipboard",
"parameters": [data]
}
})
return items
def copy_to_clipboard(self, data):
pyperclip.copy(data)
FlowLauncherAPI.show_msg(
title="UUID",
sub_title=f"uuid copied to clipboard",
ico_path="icon.png"
)
if __name__ == "__main__":
UUID()
本作品采用 知识共享署名-相同方式共享 4.0 国际许可协议 进行许可。