Skip to content

Commit

Permalink
fix(core): use url instead of path
Browse files Browse the repository at this point in the history
  • Loading branch information
Ji4n1ng committed Jan 29, 2021
1 parent 2415cea commit 00356b5
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 45 deletions.
13 changes: 4 additions & 9 deletions OpenInTerminal/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -192,19 +192,14 @@ extension AppDelegate {

@objc func copyPathToClipboard() {
do {
var paths = try FinderManager.shared.getFullPathsToFrontFinderWindowOrSelectedFile()
if paths.count == 0 {
var urls = try FinderManager.shared.getFullUrlsToFrontFinderWindowOrSelectedFile()
if urls.count == 0 {
// No Finder window and no file selected.
let homePath = NSHomeDirectory()
guard let homeUrl = URL(string: homePath) else { return }
paths.append(homeUrl.appendingPathComponent("Desktop").path)
} else {
paths = paths.compactMap {
URL(string: $0)
}.map {
$0.path
}
urls.append(homeUrl.appendingPathComponent("Desktop"))
}
let paths = urls.map { $0.path }
let pathString = paths.joined(separator: "\n")
// Set string
NSPasteboard.general.clearContents()
Expand Down
18 changes: 9 additions & 9 deletions OpenInTerminalCore/App.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ extension App: Equatable {

public protocol Openable {
func openOutsideSandbox() throws
func openInSandbox(_ paths: [String]) throws
func openInSandbox(_ urls: [URL]) throws
}

extension App: Openable {
Expand Down Expand Up @@ -117,29 +117,27 @@ extension App: Openable {
let source = """
do shell script "\(openCommand)"
"""
print(source)
try excute(source)
}
}

public func openInSandbox(_ paths: [String]) throws {
public func openInSandbox(_ urls: [URL]) throws {
switch self.type {
case .terminal:
guard var path = paths.first else { return }
guard let url = URL(string: path) else {
throw OITError.wrongUrl
}
guard var url = urls.first else { return }
// check the path is directory or not
var isDirectory: ObjCBool = false
guard FileManager.default.fileExists(atPath: url.path, isDirectory: &isDirectory) else {
return
}
// if the selected is a file, then delete last path component
if isDirectory.boolValue == false {
path = url.deletingLastPathComponent().path
url.deleteLastPathComponent()
}
// get open command, e.g. "open -a Terminal /Users/user/Desktop/test\ folder"
var openCommand = ScriptManager.shared.getOpenCommand(self)
openCommand += " " + path.specialCharEscaped()
openCommand += " " + url.path.specialCharEscaped()
// script
guard let scriptURL = ScriptManager.shared.getScriptURL(with: Constants.generalScript) else { return }
// // handle exceptional case
Expand All @@ -163,7 +161,9 @@ extension App: Openable {
case .editor:
// get open command, e.g. "open -a TextEdit /Users/user/Desktop/test\ folder /Users/user/Documents"
var openCommand = ScriptManager.shared.getOpenCommand(self)
paths.forEach {
urls.map {
$0.path
}.forEach {
openCommand += " " + $0.specialCharEscaped()
}
// script
Expand Down
45 changes: 28 additions & 17 deletions OpenInTerminalCore/FinderManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public class FinderManager {

public static var shared = FinderManager()

/// Get full path to front Finder window or selected file
public func getFullPathToFrontFinderWindowOrSelectedFile() throws -> String {
/// Get full url to front Finder window or selected file
public func getFullUrlToFrontFinderWindowOrSelectedFile() throws -> URL? {

let finder = SBApplication(bundleIdentifier: Constants.Id.Finder)! as FinderApplication

Expand All @@ -33,22 +33,30 @@ public class FinderManager {
guard let windows = finder.FinderWindows?(),
let firstWindow = windows.firstObject else {
print("No Finder windows are opened or selected")
return ""
return nil
}
target = (firstWindow as! FinderFinderWindow).target?.get() as! FinderItem
}

guard let targetUrl = target.URL,
let url = URL(string: targetUrl) else {
print("target url nil")
return ""
return nil
}

return url
}

/// Get full path to front Finder window or selected file
public func getFullPathToFrontFinderWindowOrSelectedFile() throws -> String {
guard let url = try getFullUrlToFrontFinderWindowOrSelectedFile() else {
return ""
}
return url.path
}

/// Get full paths to front Finder windows or selected files
public func getFullPathsToFrontFinderWindowOrSelectedFile() throws -> [String] {
/// Get full urls to front Finder windows or selected files
public func getFullUrlsToFrontFinderWindowOrSelectedFile() throws -> [URL] {

let finder = SBApplication(bundleIdentifier: Constants.Id.Finder)! as FinderApplication

Expand Down Expand Up @@ -78,38 +86,41 @@ public class FinderManager {
$0.URL
}.compactMap {
URL(string: $0)
}.map {
$0.path
}

return paths
}

/// Get full paths to front Finder windows or selected files
public func getFullPathsToFrontFinderWindowOrSelectedFile() throws -> [String] {
let urls = try getFullUrlsToFrontFinderWindowOrSelectedFile()
let paths = urls.map {
$0.path
}
return paths
}

/// Get path to front Finder window or selected file.
/// If the selected one is file, return it's parent path.
public func getPathToFrontFinderWindowOrSelectedFile() throws -> String {

let fullPath = try getFullPathToFrontFinderWindowOrSelectedFile()

guard fullPath != "" else { return "" }

guard let url = URL(string: fullPath) else {
throw OITError.wrongUrl
guard let fullUrl = try getFullUrlToFrontFinderWindowOrSelectedFile() else {
return ""
}

var isDirectory: ObjCBool = false

guard FileManager.default.fileExists(atPath: url.path, isDirectory: &isDirectory) else {
guard FileManager.default.fileExists(atPath: fullUrl.path, isDirectory: &isDirectory) else {
print("file does not exist")
return ""
}

// if the selected is a file, then delete last path component
guard isDirectory.boolValue else {
return url.deletingLastPathComponent().path
return fullUrl.deletingLastPathComponent().path
}

return url.path
return fullUrl.path
}

public func getDesktopPath() -> String? {
Expand Down
21 changes: 11 additions & 10 deletions OpenInTerminalFinderExtension/FinderSync.swift
Original file line number Diff line number Diff line change
Expand Up @@ -154,24 +154,24 @@ class FinderSync: FIFinderSync {

// MARK: - Actions

func getSelectedPathsFromFinder() -> [String] {
var paths = [String]()
func getSelectedPathsFromFinder() -> [URL] {
var urls = [URL]()
if let items = FIFinderSyncController.default().selectedItemURLs(), items.count > 0 {
items.forEach { (url) in
paths.append(url.path)
items.forEach {
urls.append($0)
}
} else if let url = FIFinderSyncController.default().targetedURL() {
paths.append(url.path)
urls.append(url)
}
return paths
return urls
}

func open(_ app: App) {
let paths = getSelectedPathsFromFinder()
let urls = getSelectedPathsFromFinder()
do {
try app.openInSandbox(paths)
try app.openInSandbox(urls)
} catch {
logw("Failed to open \(app.name) with \(paths)")
logw("Failed to open \(app.name) with \(urls)")
}
}

Expand Down Expand Up @@ -241,7 +241,8 @@ class FinderSync: FIFinderSync {
}

@objc func copyPathToClipboard() {
let paths = getSelectedPathsFromFinder()
let urls = getSelectedPathsFromFinder()
let paths = urls.map { $0.path }
let pathString = paths.joined(separator: "\n")
// Set string
NSPasteboard.general.clearContents()
Expand Down

0 comments on commit 00356b5

Please sign in to comment.