Skip to content

Commit 00356b5

Browse files
committed
fix(core): use url instead of path
1 parent 2415cea commit 00356b5

File tree

4 files changed

+52
-45
lines changed

4 files changed

+52
-45
lines changed

OpenInTerminal/AppDelegate.swift

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -192,19 +192,14 @@ extension AppDelegate {
192192

193193
@objc func copyPathToClipboard() {
194194
do {
195-
var paths = try FinderManager.shared.getFullPathsToFrontFinderWindowOrSelectedFile()
196-
if paths.count == 0 {
195+
var urls = try FinderManager.shared.getFullUrlsToFrontFinderWindowOrSelectedFile()
196+
if urls.count == 0 {
197197
// No Finder window and no file selected.
198198
let homePath = NSHomeDirectory()
199199
guard let homeUrl = URL(string: homePath) else { return }
200-
paths.append(homeUrl.appendingPathComponent("Desktop").path)
201-
} else {
202-
paths = paths.compactMap {
203-
URL(string: $0)
204-
}.map {
205-
$0.path
206-
}
200+
urls.append(homeUrl.appendingPathComponent("Desktop"))
207201
}
202+
let paths = urls.map { $0.path }
208203
let pathString = paths.joined(separator: "\n")
209204
// Set string
210205
NSPasteboard.general.clearContents()

OpenInTerminalCore/App.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ extension App: Equatable {
3636

3737
public protocol Openable {
3838
func openOutsideSandbox() throws
39-
func openInSandbox(_ paths: [String]) throws
39+
func openInSandbox(_ urls: [URL]) throws
4040
}
4141

4242
extension App: Openable {
@@ -117,29 +117,27 @@ extension App: Openable {
117117
let source = """
118118
do shell script "\(openCommand)"
119119
"""
120+
print(source)
120121
try excute(source)
121122
}
122123
}
123124

124-
public func openInSandbox(_ paths: [String]) throws {
125+
public func openInSandbox(_ urls: [URL]) throws {
125126
switch self.type {
126127
case .terminal:
127-
guard var path = paths.first else { return }
128-
guard let url = URL(string: path) else {
129-
throw OITError.wrongUrl
130-
}
128+
guard var url = urls.first else { return }
131129
// check the path is directory or not
132130
var isDirectory: ObjCBool = false
133131
guard FileManager.default.fileExists(atPath: url.path, isDirectory: &isDirectory) else {
134132
return
135133
}
136134
// if the selected is a file, then delete last path component
137135
if isDirectory.boolValue == false {
138-
path = url.deletingLastPathComponent().path
136+
url.deleteLastPathComponent()
139137
}
140138
// get open command, e.g. "open -a Terminal /Users/user/Desktop/test\ folder"
141139
var openCommand = ScriptManager.shared.getOpenCommand(self)
142-
openCommand += " " + path.specialCharEscaped()
140+
openCommand += " " + url.path.specialCharEscaped()
143141
// script
144142
guard let scriptURL = ScriptManager.shared.getScriptURL(with: Constants.generalScript) else { return }
145143
// // handle exceptional case
@@ -163,7 +161,9 @@ extension App: Openable {
163161
case .editor:
164162
// get open command, e.g. "open -a TextEdit /Users/user/Desktop/test\ folder /Users/user/Documents"
165163
var openCommand = ScriptManager.shared.getOpenCommand(self)
166-
paths.forEach {
164+
urls.map {
165+
$0.path
166+
}.forEach {
167167
openCommand += " " + $0.specialCharEscaped()
168168
}
169169
// script

OpenInTerminalCore/FinderManager.swift

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ public class FinderManager {
1313

1414
public static var shared = FinderManager()
1515

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

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

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

4141
guard let targetUrl = target.URL,
4242
let url = URL(string: targetUrl) else {
4343
print("target url nil")
44-
return ""
44+
return nil
4545
}
4646

47+
return url
48+
}
49+
50+
/// Get full path to front Finder window or selected file
51+
public func getFullPathToFrontFinderWindowOrSelectedFile() throws -> String {
52+
guard let url = try getFullUrlToFrontFinderWindowOrSelectedFile() else {
53+
return ""
54+
}
4755
return url.path
4856
}
4957

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

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

@@ -78,38 +86,41 @@ public class FinderManager {
7886
$0.URL
7987
}.compactMap {
8088
URL(string: $0)
81-
}.map {
82-
$0.path
8389
}
8490

8591
return paths
8692
}
8793

94+
/// Get full paths to front Finder windows or selected files
95+
public func getFullPathsToFrontFinderWindowOrSelectedFile() throws -> [String] {
96+
let urls = try getFullUrlsToFrontFinderWindowOrSelectedFile()
97+
let paths = urls.map {
98+
$0.path
99+
}
100+
return paths
101+
}
102+
88103
/// Get path to front Finder window or selected file.
89104
/// If the selected one is file, return it's parent path.
90105
public func getPathToFrontFinderWindowOrSelectedFile() throws -> String {
91106

92-
let fullPath = try getFullPathToFrontFinderWindowOrSelectedFile()
93-
94-
guard fullPath != "" else { return "" }
95-
96-
guard let url = URL(string: fullPath) else {
97-
throw OITError.wrongUrl
107+
guard let fullUrl = try getFullUrlToFrontFinderWindowOrSelectedFile() else {
108+
return ""
98109
}
99110

100111
var isDirectory: ObjCBool = false
101112

102-
guard FileManager.default.fileExists(atPath: url.path, isDirectory: &isDirectory) else {
113+
guard FileManager.default.fileExists(atPath: fullUrl.path, isDirectory: &isDirectory) else {
103114
print("file does not exist")
104115
return ""
105116
}
106117

107118
// if the selected is a file, then delete last path component
108119
guard isDirectory.boolValue else {
109-
return url.deletingLastPathComponent().path
120+
return fullUrl.deletingLastPathComponent().path
110121
}
111122

112-
return url.path
123+
return fullUrl.path
113124
}
114125

115126
public func getDesktopPath() -> String? {

OpenInTerminalFinderExtension/FinderSync.swift

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -154,24 +154,24 @@ class FinderSync: FIFinderSync {
154154

155155
// MARK: - Actions
156156

157-
func getSelectedPathsFromFinder() -> [String] {
158-
var paths = [String]()
157+
func getSelectedPathsFromFinder() -> [URL] {
158+
var urls = [URL]()
159159
if let items = FIFinderSyncController.default().selectedItemURLs(), items.count > 0 {
160-
items.forEach { (url) in
161-
paths.append(url.path)
160+
items.forEach {
161+
urls.append($0)
162162
}
163163
} else if let url = FIFinderSyncController.default().targetedURL() {
164-
paths.append(url.path)
164+
urls.append(url)
165165
}
166-
return paths
166+
return urls
167167
}
168168

169169
func open(_ app: App) {
170-
let paths = getSelectedPathsFromFinder()
170+
let urls = getSelectedPathsFromFinder()
171171
do {
172-
try app.openInSandbox(paths)
172+
try app.openInSandbox(urls)
173173
} catch {
174-
logw("Failed to open \(app.name) with \(paths)")
174+
logw("Failed to open \(app.name) with \(urls)")
175175
}
176176
}
177177

@@ -241,7 +241,8 @@ class FinderSync: FIFinderSync {
241241
}
242242

243243
@objc func copyPathToClipboard() {
244-
let paths = getSelectedPathsFromFinder()
244+
let urls = getSelectedPathsFromFinder()
245+
let paths = urls.map { $0.path }
245246
let pathString = paths.joined(separator: "\n")
246247
// Set string
247248
NSPasteboard.general.clearContents()

0 commit comments

Comments
 (0)