app.application - 应用程序 API
与 macOS 应用程序交互。
方法
app.application.list()
获取所有运行中的应用(仅包含有 UI 的常规应用)。
返回值: table - 应用信息数组
local apps = app.application.list()
for _, a in ipairs(apps) do
app.log.info(a.name .. " - " .. (a.bundleId or ""))
end
-- 返回格式:
-- {
-- name = "Safari",
-- bundleId = "com.apple.Safari",
-- path = "/Applications/Safari.app",
-- pid = 1234,
-- isActive = true,
-- isHidden = false
-- }
app.application.frontmost()
获取当前最前端的应用。
返回值: table|nil - 应用信息
local front = app.application.frontmost()
if front then
app.log.info("当前应用: " .. front.name)
end
-- 返回格式:
-- {
-- name = "Finder",
-- bundleId = "com.apple.finder",
-- path = "/System/Library/CoreServices/Finder.app",
-- pid = 1234
-- }
app.application.info(bundleId)
获取应用详细信息。
参数:
bundleId(string) - 应用的 Bundle ID
返回值: table|nil - 应用信息
local info = app.application.info("com.apple.Safari")
if info then
app.log.info("版本: " .. (info.version or ""))
end
-- 运行中的应用返回:
-- {
-- name = "Safari",
-- bundleId = "com.apple.Safari",
-- path = "/Applications/Safari.app",
-- pid = 1234,
-- version = "17.0",
-- build = "17612.1.29",
-- isRunning = true,
-- isActive = false,
-- isHidden = false
-- }
-- 未运行的应用返回:
-- {
-- name = "Safari",
-- bundleId = "com.apple.Safari",
-- path = "/Applications/Safari.app",
-- version = "17.0",
-- build = "17612.1.29",
-- isRunning = false
-- }
app.application.path(bundleId)
获取应用的安装路径。
参数:
bundleId(string) - 应用的 Bundle ID
返回值: string|nil - 应用路径
local path = app.application.path("com.apple.Safari")
-- "/Applications/Safari.app"
app.application.isInstalled(bundleId)
检查应用是否已安装。
参数:
bundleId(string) - 应用的 Bundle ID
返回值: boolean
if app.application.isInstalled("com.microsoft.VSCode") then
app.log.info("VS Code 已安装")
end
app.application.isRunning(bundleId)
检查应用是否正在运行。
参数:
bundleId(string) - 应用的 Bundle ID
返回值: boolean
if app.application.isRunning("com.apple.Safari") then
app.log.info("Safari 正在运行")
end
app.application.launch(bundleId, options?)
启动应用程序。
参数:
bundleId(string) - 应用的 Bundle IDoptions(table, 可选):hide(boolean) - 启动后隐藏background(boolean) - 后台启动(不激活)arguments(table) - 启动参数
返回值: boolean - 是否成功
-- 普通启动
app.application.launch("com.apple.Safari")
-- 后台启动
app.application.launch("com.apple.Safari", {background = true})
-- 带参数启动
app.application.launch("com.apple.Terminal", {
arguments = {"-e", "ls -la"}
})
app.application.open(path)
用默认应用打开文件或 URL。
参数:
path(string) - 文件路径或 URL
返回值: boolean - 是否成功
-- 打开文件
app.application.open("/Users/test/document.pdf")
-- 打开 URL
app.application.open("https://www.apple.com")
-- 打开 mailto 链接(需以 URL scheme 格式传入)
-- 注意:仅支持 http://, https://, file:// 前缀的 URL 会被解析为 URL,
-- 其他 scheme(如 mailto:)会被当作文件路径处理,可能无法正常工作。
-- 建议使用 app.shell.execute('open "mailto:test@example.com"') 代替。
app.application.open("mailto:test@example.com")
app.application.quit(bundleId)
退出应用程序。
参数:
bundleId(string) - 应用的 Bundle ID
返回值: boolean - 是否成功
app.application.quit("com.apple.Safari")
app.application.forceQuit(bundleId)
强制退出应用程序。
参数:
bundleId(string) - 应用的 Bundle ID
返回值: boolean - 是否成功
app.application.forceQuit("com.apple.Safari")
app.application.hide(bundleId)
隐藏应用程序。
参数:
bundleId(string) - 应用的 Bundle ID
返回值: boolean - 是否成功
app.application.hide("com.apple.Safari")
app.application.unhide(bundleId)
取消隐藏应用程序。
参数:
bundleId(string) - 应用的 Bundle ID
返回值: boolean - 是否成功
app.application.unhide("com.apple.Safari")
app.application.activate(bundleId)
激活应用程序(置于最前)。
参数:
bundleId(string) - 应用的 Bundle ID
返回值: boolean - 是否成功
app.application.activate("com.apple.Safari")
app.application.find(name)
按名称查找应用(搜索运行中的应用和 /Applications 目录)。
参数:
name(string) - 应用名称(支持部分匹配)
返回值: table - 匹配的应用数组
local results = app.application.find("Safari")
for _, a in ipairs(results) do
app.log.info(a.name .. " - " .. (a.isRunning and "运行中" or "未运行"))
end
-- 返回格式:
-- {
-- name = "Safari",
-- bundleId = "com.apple.Safari",
-- path = "/Applications/Safari.app",
-- isRunning = true
-- }
app.application.defaultApp(extension)
获取文件扩展名的默认打开应用。
参数:
extension(string) - 文件扩展名(可带或不带点)
返回值: string|nil - 应用路径
local app_path = app.application.defaultApp("pdf")
-- "/System/Applications/Preview.app"
local app_path = app.application.defaultApp(".txt")
-- "/System/Applications/TextEdit.app"
事件监听
app.application.watch(event, callback)
监听应用程序生命周期事件。
参数:
event(string) - 事件名称:"launch"- 应用启动时触发"terminate"- 应用退出时触发"activate"- 应用激活(切换到前台)时触发
callback(function) - 回调函数,接收事件表:type(string) - 事件类型(与event参数一致)name(string) - 应用名称bundleId(string) - 应用 Bundle IDpid(number) - 进程 ID
返回值: string, error - watcher 句柄 ID
app.application.watch("launch", function(e)
app.log.info("应用启动: " .. e.name .. " (" .. e.bundleId .. ")")
end)
app.application.watch("terminate", function(e)
app.log.info("应用退出: " .. e.name)
end)
app.application.watch("activate", function(e)
app.log.info("切换到: " .. e.name)
end)
app.application.stopAllWatchers()
停止当前插件注册的所有应用事件监听器。
返回值: boolean
app.application.stopAllWatchers()
app.application.listWatchers()
列出当前插件已注册的应用事件监听器。
返回值: array<table> - 每项包含:
id(string) - watcher 句柄 IDevent(string) - 监听的事件名称
local watchers = app.application.listWatchers()
for _, w in ipairs(watchers) do
app.log.info(w.id .. " -> " .. w.event)
end
示例
用指定应用打开文件
function MyPlugin:handleOpenWithVSCode(context)
local vscode = "com.microsoft.VSCode"
if not app.application.isInstalled(vscode) then
app.dialog.alert("错误", "VS Code 未安装")
return
end
-- 先启动 VS Code
app.application.launch(vscode)
-- 然后打开文件
for _, file in ipairs(context.selectedFiles) do
app.shell.execute('open -a "Visual Studio Code" "' .. file .. '"')
end
end
退出所有第三方应用
function MyPlugin:handleQuitThirdParty(context)
local running = app.application.list()
local count = 0
for _, a in ipairs(running) do
-- 排除 Apple 应用
if a.bundleId and not a.bundleId:match("^com%.apple%.") then
if app.application.quit(a.bundleId) then
count = count + 1
end
end
end
app.notification.show("完成", "已退出 " .. count .. " 个应用")
end