From 12ca4043b7b1d02d0f407ef85f6a49ee0ea4106f Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Tue, 25 Jan 2022 20:52:53 +0800 Subject: [PATCH] Installer // Use FileManager to trash old bundle. --- Source/Installer/AppDelegate.swift | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/Source/Installer/AppDelegate.swift b/Source/Installer/AppDelegate.swift index cd4e2daf..f4b08791 100644 --- a/Source/Installer/AppDelegate.swift +++ b/Source/Installer/AppDelegate.swift @@ -117,12 +117,25 @@ class AppDelegate: NSWindowController, NSApplicationDelegate { let shouldWaitForTranslocationRemoval = appBundleChronoshiftedToARandomizedPath(kTargetPartialPath) && (window?.responds(to: #selector(NSWindow.beginSheet(_:completionHandler:))) ?? false) - // http://www.cocoadev.com/index.pl?MoveToTrash - let sourceDir = (kDestinationPartial as NSString).expandingTildeInPath - let trashDir = (NSHomeDirectory() as NSString).appendingPathComponent(".Trash") - var tag = 0 - - NSWorkspace.shared.performFileOperation(.recycleOperation, source: sourceDir, destination: trashDir, files: [kTargetBundle], tag: &tag) + // 將既存輸入法扔到垃圾桶內 + do { + let sourceDir = (kDestinationPartial as NSString).expandingTildeInPath + let fileManager = FileManager.default + let fileURLString = String(format: "%@/%@", sourceDir, kTargetBundle) + let fileURL = URL(fileURLWithPath: fileURLString) + + // 檢查檔案是否存在 + if fileManager.fileExists(atPath: fileURLString) { + // 塞入垃圾桶 + try fileManager.trashItem(at: fileURL, resultingItemURL: nil) + } else { + NSLog("File does not exist") + } + + } + catch let error as NSError { + NSLog("An error took place: \(error)") + } let killTask = Process() killTask.launchPath = "/usr/bin/killall"