forked from raycast/script-commands
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopen-last-download.swift
executable file
·44 lines (32 loc) · 1.09 KB
/
open-last-download.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/swift
// Required parameters:
// @raycast.schemaVersion 1
// @raycast.title Open Last Download
// @raycast.mode silent
// @raycast.packageName System
// Optional parameters:
// @raycast.icon 💁♂️
// Documentation:
// @raycast.description Opens the last downloaded file.
import AppKit
// MARK: - Convenience
extension URL {
var addedToDirectoryDate: Date {
return (try? resourceValues(forKeys: [.addedToDirectoryDateKey]).addedToDirectoryDate) ?? .distantPast
}
}
func failure(_ message: String) -> Never {
print(message)
exit(1)
}
// MARK: - Main
do {
let downloadsDirectory = FileManager.default.urls(for: .downloadsDirectory, in: .userDomainMask).first!
let downloads = try FileManager.default.contentsOfDirectory(at: downloadsDirectory, includingPropertiesForKeys: [.addedToDirectoryDateKey], options: .skipsHiddenFiles)
guard let lastDownload = downloads.sorted(by: { $0.addedToDirectoryDate > $1.addedToDirectoryDate }).first else {
failure("No downloaded files")
}
NSWorkspace.shared.open(lastDownload)
} catch {
failure(error.localizedDescription)
}