Skip to content

Commit 8b728fc

Browse files
authored
Merge pull request #4 from es-kumagai/support_file_selection
Support file selection
2 parents 812c131 + c450071 commit 8b728fc

File tree

3 files changed

+52
-18
lines changed

3 files changed

+52
-18
lines changed

Sources/OpenTerminal/Info.plist

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
<key>CFBundlePackageType</key>
1818
<string>APPL</string>
1919
<key>CFBundleShortVersionString</key>
20-
<string>1.1.0</string>
20+
<string>1.2.0</string>
2121
<key>CFBundleSignature</key>
2222
<string>????</string>
2323
<key>CFBundleVersion</key>
24-
<string>5</string>
24+
<string>6</string>
2525
<key>LSApplicationCategoryType</key>
2626
<string>public.app-category.utilities</string>
2727
<key>LSMinimumSystemVersion</key>

Sources/OpenTerminal/URL.swift

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,42 @@
88

99
import Foundation
1010

11+
private let fileManager = FileManager()
12+
1113
extension URL {
12-
14+
1315
var pathForAppleScriptWithEscapedSpace: String {
1416

1517
return "\(path.replacingOccurrences(of: " ", with: "\\\\ "))"
1618
}
19+
20+
var truncatedToDirectoryName: URL {
21+
22+
var isDirectory: ObjCBool = false
23+
var isFile: Bool {
24+
25+
return !isDirectory.boolValue
26+
}
27+
28+
guard fileManager.fileExists(atPath: path, isDirectory: &isDirectory) else {
29+
30+
// Return self if the url is not exists. This is an unexpected case.
31+
return self
32+
}
33+
34+
guard isFile else {
35+
36+
return self
37+
}
38+
39+
return deletingLastPathComponent()
40+
}
41+
}
42+
43+
extension Sequence where Element == URL {
44+
45+
var uniquelySet: Set<Element> {
46+
47+
return Set(self)
48+
}
1749
}

Sources/OpenTerminal/main.swift

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ let finder = SBApplication(bundleIdentifier: "com.apple.Finder")! as FinderAppli
1414
let selection = finder.selection!
1515
let selectionItems = selection.get() as! Array<AnyObject>
1616

17-
let fileUrls: Array<String>
17+
let filePaths: Array<String>
1818

1919
if selectionItems.isEmpty {
2020

@@ -23,31 +23,33 @@ if selectionItems.isEmpty {
2323
let container = window.target!
2424
let item = container.get() as! FinderItemProtocol
2525

26-
fileUrls = [item.url!]
26+
filePaths = [item.url!]
2727
}
2828
else {
2929

3030
// This case is for launch from Finder directly.
31-
fileUrls = selectionItems
31+
filePaths = selectionItems
3232
.compactMap { $0 as? FinderApplicationFileProtocol }
3333
.compactMap { $0.url }
3434
}
3535

36-
fileUrls
37-
.compactMap { URL(string: $0) }
36+
filePaths
37+
.compactMap { URL.init(string: $0) }
38+
.compactMap { $0.truncatedToDirectoryName }
39+
.uniquelySet
3840
.forEach { url in
41+
42+
do {
3943

40-
do {
41-
42-
guard let terminal = settings.terminal else {
43-
44-
throw OpenError.cannotSpecifyTargetTerminal
45-
}
44+
guard let terminal = settings.terminal else {
4645

47-
try terminal.open(url: url)
46+
throw OpenError.cannotSpecifyTargetTerminal
4847
}
49-
catch {
5048

51-
alert(message: "\(error)")
52-
}
49+
try terminal.open(url: url)
50+
}
51+
catch {
52+
53+
alert(message: "\(error)")
54+
}
5355
}

0 commit comments

Comments
 (0)