-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopy-last-screenshot.swift
executable file
·50 lines (36 loc) · 1.31 KB
/
copy-last-screenshot.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
45
46
47
48
49
50
#!/usr/bin/swift
// Required parameters:
// @raycast.schemaVersion 1
// @raycast.title Copy Last Screenshot
// @raycast.mode silent
// @raycast.packageName System
// Optional parameters:
// @raycast.icon 📸
// Documentation:
// @raycast.description Copies the last screenshot to the clipboard.
import Cocoa
// MARK: - Main
let query = NSMetadataQuery()
guard let lastScreenshot = query.searchScreenshots()?.first, let path = lastScreenshot.value(forAttribute: "kMDItemPath") as? String else {
print("Cannot find screenshot")
exit(1)
}
let fileURL = URL(fileURLWithPath: path)
NSPasteboard.general.clearContents()
NSPasteboard.general.writeObjects([fileURL as NSPasteboardWriting])
print("Copied last screenshot")
// MARK: - Convenience
extension NSMetadataQuery {
func searchScreenshots() -> [NSMetadataItem]? {
predicate = NSPredicate(format: "kMDItemIsScreenCapture = 1")
sortDescriptors = [NSSortDescriptor(key: "kMDItemFSCreationDate", ascending: false)]
NotificationCenter.default.addObserver(forName: .NSMetadataQueryDidFinishGathering, object: nil, queue: nil) { [weak self] _ in
self?.disableUpdates()
self?.stop()
CFRunLoopStop(CFRunLoopGetCurrent());
}
guard start() else { return nil }
CFRunLoopRun()
return results.compactMap { $0 as? NSMetadataItem }
}
}