app.finder - Finder 操作 API
与 macOS Finder 交互。
返回值:成功返回
true;失败返回false和错误(路径不存在、找不到应用、打不开)。iRightMenu Pro 2.7.1 之前的版本一律返回true,不保证操作成功。权限:L1 标准 — 需在
permissions中声明("finder")
方法
app.finder.open(path)
用默认应用打开文件或目录。
参数:
path(string) - 文件或目录路径
返回值: boolean, error - 成功返回 true;路径不存在或打不开时返回 false 和错误
-- 打开文件
app.finder.open("/path/to/document.pdf")
-- 打开目录
app.finder.open("/path/to/folder")
app.finder.openWith(path, appName)
用指定应用打开文件。
参数:
path(string) - 文件路径appName(string) - 应用名称
返回值: boolean, error - 成功返回 true;文件不存在或找不到应用时返回 false 和错误
app.finder.openWith("/path/to/file.txt", "Visual Studio Code")
app.finder.openWith("/path/to/file.txt", "Sublime Text")
app.finder.select(path)
在 Finder 中选中文件(打开所在目录并选中)。
参数:
path(string) - 文件路径
返回值: boolean, error - 成功返回 true;文件不存在时返回 false 和错误
app.finder.select("/path/to/file.txt")
app.finder.reveal(path)
同 select,在 Finder 中显示文件。
参数:
path(string) - 文件路径
返回值: boolean, error - 成功返回 true;文件不存在时返回 false 和错误
app.finder.reveal("/path/to/file.txt")
示例
处理完成后显示文件
function MyPlugin:handleConvert(context)
for _, file in ipairs(context.selectedFiles) do
local outputPath = app.path.withExtension(file, "png")
if app.image.convert(file, outputPath) then
-- 转换成功,在 Finder 中显示
local ok, err = app.finder.reveal(outputPath)
if not ok then
app.log.warning("无法在 Finder 中显示:" .. err.message)
end
end
end
end
用指定应用打开
function MyPlugin:handleOpenWithEditor(context)
for _, file in ipairs(context.selectedFiles) do
app.finder.openWith(file, "Visual Studio Code")
end
end