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