app.plist - Plist 操作 API

读写 macOS Property List 文件。

方法

app.plist.read(path)

读取 plist 文件。

参数:

  • path (string) - plist 文件路径

返回值: table|nil - Lua 表或 nil

local info = app.plist.read("/Applications/Safari.app/Contents/Info.plist")
if info then
    app.log.info("版本: " .. info.CFBundleShortVersionString)
    app.log.info("Bundle ID: " .. info.CFBundleIdentifier)
end

app.plist.write(path, data)

写入 plist 文件(XML 格式)。

参数:

  • path (string) - 文件路径
  • data (table) - 要写入的数据

返回值: boolean - 是否成功

app.plist.write("/path/to/config.plist", {
    AppName = "MyApp",
    Version = "1.0.0",
    Settings = {
        Theme = "dark",
        AutoSave = true
    }
})

app.plist.getValue(path, keyPath)

从 plist 文件读取单个值。

参数:

  • path (string) - plist 文件路径
  • keyPath (string) - 键路径(支持点号分隔的嵌套路径)

返回值: any|nil - 值或 nil

local version = app.plist.getValue(
    "/Applications/Safari.app/Contents/Info.plist",
    "CFBundleShortVersionString"
)

-- 嵌套路径
local setting = app.plist.getValue("/path/to/config.plist", "Settings.Theme")

-- 数组索引(从 0 开始)
local firstItem = app.plist.getValue("/path/to/config.plist", "Items.0")

app.plist.setValue(path, keyPath, value)

在 plist 文件中设置单个值。

参数:

  • path (string) - plist 文件路径
  • keyPath (string) - 键路径
  • value (any) - 值(nil 表示删除该键)

返回值: boolean - 是否成功

app.plist.setValue("/path/to/config.plist", "LastOpened", os.time())

-- 设置嵌套值
app.plist.setValue("/path/to/config.plist", "Settings.Theme", "light")

-- 删除键
app.plist.setValue("/path/to/config.plist", "OldKey", nil)

类型映射

Plist 类型 Lua 类型
dict table(哈希表)
array table(数组)
string string
integer/real number
true/false boolean
data string(Base64 编码)
date string(ISO 8601 格式)

示例

读取应用信息

function MyPlugin:getAppInfo(appPath)
    local infoPath = app.path.join(appPath, "Contents/Info.plist")

    if not app.path.exists(infoPath) then
        return nil
    end

    local info = app.plist.read(infoPath)
    if not info then
        return nil
    end

    return {
        name = info.CFBundleName or info.CFBundleDisplayName,
        version = info.CFBundleShortVersionString,
        build = info.CFBundleVersion,
        bundleId = info.CFBundleIdentifier,
        minOS = info.LSMinimumSystemVersion
    }
end

function MyPlugin:handleShowAppInfo(context)
    for _, file in ipairs(context.selectedFiles) do
        if app.path.extension(file) == "app" then
            local info = self:getAppInfo(file)
            if info then
                local message = string.format(
                    "名称: %s\n版本: %s (%s)\nBundle ID: %s",
                    info.name or "未知",
                    info.version or "未知",
                    info.build or "未知",
                    info.bundleId or "未知"
                )
                app.dialog.alert({title = info.name, message = message})
            end
        end
    end
end

修改配置文件

function MyPlugin:handleToggleSetting(context)
    local configPath = "/path/to/config.plist"

    -- 读取当前值
    local enabled = app.plist.getValue(configPath, "Settings.Enabled")

    -- 切换并保存
    app.plist.setValue(configPath, "Settings.Enabled", not enabled)

    app.notification.show("设置", enabled and "已禁用" or "已启用")
end
开发者文档
使用帮助
使用说明 脚本菜单 常见问题
脚本开发
开发指南
插件开发
快速开始 开发指南 示例插件
API 参考
概览 API 查询 插件信息 日志 Finder 上下文 插件设置 国际化
UI 与交互
对话框 进度条 系统通知 选择器 WebView 状态栏 Dock
文件与路径
文件操作 路径工具 Finder 操作 废纸篓 扩展属性 元数据 文件监听
数据格式
JSON Plist CSV XML PDF 图片
文本与编码
字符串 正则表达式 日期时间 颜色 加密编码
系统
Shell 命令 进程管理 应用管理 系统信息 AppleScript 快捷指令
系统信息
网络信息 电源/电池 屏幕/外观 音频控制 蓝牙设备 位置服务
网络
HTTP 请求 WebSocket URL 工具
输入与剪贴板
键盘模拟 鼠标模拟 全局热键 剪贴板 窗口管理
存储
SQLite Keychain UserDefaults
媒体
文字识别 二维码
工具
归档 类型标识 分享 定时器 防休眠 并发/协程