app.context - 上下文 API
访问 Finder 选择上下文和插件目录信息。
方法
app.context.selectedFiles()
获取 Finder 中选中的文件路径。
返回值: table - 绝对路径数组
local files = app.context.selectedFiles()
for _, path in ipairs(files) do
app.log.info(path)
end
app.context.currentDirectory()
获取当前 Finder 窗口的目录。
返回值: string - 目录路径
local dir = app.context.currentDirectory()
-- "/Users/username/Documents"
app.context.pluginsDirectory()
获取插件安装目录。
返回值: string - 目录路径
local dir = app.context.pluginsDirectory()
-- "~/Library/Group Containers/group.net.ymlab.iRightMenu.pro/Plugins"
app.context.pluginDirectory()
获取当前插件的目录。
返回值: string - 目录路径
local dir = app.context.pluginDirectory()
-- "~/Library/Group Containers/.../Plugins/com.example.my-plugin"
-- 读取插件资源文件
local configPath = app.path.join(dir, "config.json")
示例
处理选中文件
function MyPlugin:handleAction(context)
local files = app.context.selectedFiles()
local dir = app.context.currentDirectory()
app.log.info("当前目录: " .. dir)
app.log.info("选中文件数: " .. #files)
for i, file in ipairs(files) do
app.log.info("文件 " .. i .. ": " .. file)
end
end
读取插件配置
function MyPlugin:loadConfig()
local pluginDir = app.context.pluginDirectory()
local configPath = app.path.join(pluginDir, "config.json")
if app.file.exists(configPath) then
local content = app.file.read(configPath)
return app.json.parse(content)
end
return {}
end