You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何获取Mac上所有已安装应用及其详细信息(不限于运行中应用)

获取Mac上已安装但未运行应用的详细信息

需求说明

需要获取Mac上所有已安装但未运行的应用的详细信息,包括图标、Bundle路径、可执行文件路径等(与NSRunningApplication提供的同类信息),但system_profiler SPApplicationsDataType命令无法满足该需求。

可行解决方案

1. 遍历应用目录读取Bundle信息

Mac应用通常存放于以下目录:

  • /Applications(系统级应用)
  • ~/Applications(用户级应用)
  • /Library/Application Support(部分应用)
  • ~/Library/Application Support(部分用户应用)

通过遍历这些目录下的.app包,使用NSBundle读取对应信息:

  • 图标:通过NSBundle的icon属性,或读取Contents/Resources下的图标文件
  • Bundle路径:bundleURL属性
  • 可执行文件路径:executableURL属性

示例Swift代码:

import AppKit

func scanInstalledApps() {
    let appDirectories = [
        URL(fileURLWithPath: "/Applications"),
        URL(fileURLWithPath: NSHomeDirectory()).appendingPathComponent("Applications")
    ]
    
    for dir in appDirectories {
        guard let enumerator = FileManager.default.enumerator(at: dir, includingPropertiesForKeys: [.isDirectoryKey], options: [.skipsHiddenFiles]) else { continue }
        
        for case let fileURL as URL in enumerator {
            guard fileURL.pathExtension == "app" else { continue }
            
            // 检查应用是否正在运行
            let bundleID = Bundle(url: fileURL)?.bundleIdentifier
            let isRunning = NSWorkspace.shared.runningApplications.contains { $0.bundleIdentifier == bundleID }
            
            if !isRunning {
                if let bundle = Bundle(url: fileURL) {
                    print("应用名称: \(bundle.infoDictionary?["CFBundleName"] as? String ?? fileURL.lastPathComponent)")
                    print("Bundle路径: \(bundle.bundleURL.path)")
                    print("可执行文件路径: \(bundle.executableURL?.path ?? "未知")")
                    print("图标: \(bundle.icon ?? NSImage())")
                    print("---")
                }
            }
        }
    }
}

scanInstalledApps()

2. 借助Launch Services数据库

Mac的Launch Services维护了已注册应用的数据库,通过LSApplicationWorkspace获取所有已注册应用后,排除正在运行的即可:

示例Swift代码:

import AppKit
import CoreServices

func getInstalledAppsFromLaunchServices() {
    let workspace = LSApplicationWorkspace.default()
    guard let allApps = workspace.allApplications() as? [LSApplicationProxy] else { return }
    
    let runningAppBundleIDs = Set(NSWorkspace.shared.runningApplications.compactMap { $0.bundleIdentifier })
    
    for app in allApps {
        guard !runningAppBundleIDs.contains(app.bundleIdentifier!) else { continue }
        
        print("应用名称: \(app.localizedName ?? app.bundleURL.lastPathComponent)")
        print("Bundle路径: \(app.bundleURL.path)")
        print("可执行文件路径: \(app.executableURL?.path ?? "未知")")
        print("图标: \(app.icon ?? NSImage())")
        print("---")
    }
}

getInstalledAppsFromLaunchServices()

补充说明

  • LSApplicationProxy提供的信息与NSRunningApplication高度匹配,能直接获取所需的图标、路径等属性
  • 目录遍历方式更直接,但可能遗漏Launch Services注册的其他位置应用;Launch Services方式则能覆盖所有已注册应用,更全面

内容的提问来源于stack exchange,提问作者Huakun Shen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 09:30:44