app.finder - Finder 操作 API
与 macOS Finder 交互。
注意: 所有 Finder 操作都是异步执行的,立即返回
true,不保证操作成功。
方法
app.finder.open(path)
用默认应用打开文件或目录。
参数:
path(string) - 文件或目录路径
返回值: boolean - 始终返回 true(异步执行)
-- 打开文件
app.finder.open("/path/to/document.pdf")
-- 打开目录
app.finder.open("/path/to/folder")
app.finder.openWith(path, appName)
用指定应用打开文件。
参数:
path(string) - 文件路径appName(string) - 应用名称
返回值: boolean - 始终返回 true(异步执行)
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 - 始终返回 true(异步执行)
app.finder.select("/path/to/file.txt")
app.finder.reveal(path)
同 select,在 Finder 中显示文件。
参数:
path(string) - 文件路径
返回值: boolean - 始终返回 true(异步执行)
app.finder.reveal("/path/to/file.txt")
示例
处理完成后显示文件
function MyPlugin:handleConvert(context)
for _, file in ipairs(context.selectedFiles) do
local outputPath = app.path.removeExtension(file) .. ".png"
if app.image.convert(file, outputPath) then
-- 转换成功,在 Finder 中显示
app.finder.reveal(outputPath)
end
end
end
用指定应用打开
function MyPlugin:handleOpenWithEditor(context)
for _, file in ipairs(context.selectedFiles) do
app.finder.openWith(file, "Visual Studio Code")
end
end