根据bundleIdentifier获取app的Document路径

这段代码在Objective-C中遍历LSApplicationWorkspace中的应用,查找bundleIdentifier包含'com.my.bundleIdentifier'的应用,并获取其资源目录URL,然后转换为不包含'file://'的路径。

NSMutableArray *defaultWorkspaceApps =[[NSClassFromString(@“LSApplicationWorkspace”) defaultWorkspace] applicationsOfType:0];
for (LSApplicationProxy *app in defaultWorkspaceApps) {

if([app.bundleIdentifier containsString:@“com.my.bundleIdentifier”])
{
NSURL * resourcesDirectoryURL = [app performSelector:NSSelectorFromString(@“containerURL”)];
NSString* path=[resourcesDirectoryURL absoluteString];
path=[str stringByReplacingOccurrencesOfString:@“file://” withString:@“”];//路径

}

}

import Cocoa @main class AppDelegate: NSObject, NSApplicationDelegate { private var writableXMLURL: URL! // 可写的副本路径 private let bundleFileName = "config.xml" // Bundle 中原始文件名 func applicationDidFinishLaunching(_ notification: Notification) { setupMenu() copyXMLFromBundleToWritableLocation() } // MARK: - 菜单设置 func setupMenu() { let menu = NSMenu() let updateItem = NSMenuItem(title: "Update XML Content", action: #selector(updateXML), keyEquivalent: "u") updateItem.keyEquivalentModifierMask = .command let showItem = NSMenuItem(title: "Show Current XML", action: #selector(showXMLContent), keyEquivalent: "s") showItem.keyEquivalentModifierMask = .command menu.addItem(updateItem) menu.addItem(showItem) menu.addItem(NSMenuItem.separator()) menu.addItem(withTitle: "Quit", action: #selector(quit), keyEquivalent: "q") let appMenu = NSMenuItem() appMenu.submenu = menu NSApp.mainMenu = NSMenu() NSApp.mainMenu?.addItem(appMenu) } // MARK: - 从 Bundle 复制 XML 到可写位置 func copyXMLFromBundleToWritableLocation() { // 获取目标路径:~/Library/Application Support/com.yourcompany.AppName/config.xml guard let appSupport = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first else { return } let bundleID = Bundle.main.bundleIdentifier ?? "com.example.DefaultApp" writableXMLURL = appSupport.appendingPathComponent("$bundleID/$bundleFileName)") // 确保目录存在 try? FileManager.default.createDirectory(at: writableXMLURL.deletingLastPathComponent(), withIntermediateDirectories: true) // 如果可写位置还没有该文件,则从 Bundle 复制过去 if !FileManager.default.fileExists(atPath: writableXMLURL.path) { guard let bundledFileURL = Bundle.main.url(forResource: "config", withExtension: "xml") else { print("❌ 未在 App Bundle 中找到 config.xml") return } do { try FileManager.default.copyItem(at: bundledFileURL, to: writableXMLURL) print("✅ 已从 Bundle 复制 config.xml 到: $writableXMLURL.path)") } catch { print("❌ 复制失败: $error)") } } else { print("📄 使用已有可写配置文件: $writableXMLURL.path)") } } // MARK: - 动态更新 XML 内容 @objc func updateXML() { // 1. 读取当前可写 XML 文件 guard var content = try? String(contentsOf: writableXMLURL, encoding: .utf8) else { print("❌ 无法读取可写 XML 文件") return } // 2. 使用正则提取当前 version 并递增 let versionPattern = #"<version>(.*?)</version>"# let regex = try! NSRegularExpression(pattern: versionPattern, options: []) let nsString = content as NSString let range = NSRange(location: 0, length: nsString.length) var newVersion = "1.0" if let match = regex.firstMatch(in: content, range: range) { // 使用 match.range(at: 1) 获取捕获组(即版本号部分)的 NSRange let capturedRange = match.range(at: 1) if capturedRange.location != NSNotFound, capturedRange.length > 0 { // 使用 NSString 的 substring 方法提取文本 let oldVersionStr = nsString.substring(with: capturedRange) if let oldVersion = Double(oldVersionStr) { newVersion = String(format: "%.1f", oldVersion + 0.1) } else { print("⚠️ 无法解析版本号: $oldVersionStr)") } } } print("🔧 新版本号: $newVersion)") // 3. 生成新时间 let formatter = DateFormatter() formatter.timeStyle = .medium formatter.dateStyle = .short let nowStr = formatter.string(from: Date()) // 4. 替换 version 和 lastModified 字段 content = content .replacingOccurrences(of: #"<version>.*?</version>"#, with: "<version>$newVersion)</version>", options: .regularExpression) .replacingOccurrences(of: #"<lastModified>.*?</lastModified>"#, with: "<lastModified>$nowStr)</lastModified>", options: .regularExpression) // 5. 写回可写文件 do { try content.write(to: writableXMLURL, atomically: true, encoding: .utf8) print("✅ 成功更新并保存 XML:\n$content)") } catch { print("❌ 写入失败: $error)") } } // MARK: - 显示当前 XML 内容 @objc func showXMLContent() { guard let content = try? String(contentsOf: writableXMLURL, encoding: .utf8) else { print("❌ 无法读取文件") return } let alert = NSAlert() alert.messageText = "Current Writable XML" alert.informativeText = content alert.alertStyle = .informational alert.runModal() } @objc func quit() { NSApp.terminate(nil) } } 我这个是不是没写界面啊?运行出来什么也没有,能不能再额外加点可视化或者打印,让我知道具体修改到了文件没有
12-03
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小黑猫成仙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值