[英]Detect screenshots on macOS swift 5
在網上搜索有關通過文件元數據檢測屏幕截圖時,我發現了以下方法來檢測使用NSMetadataQuery
拍攝的屏幕截圖:
import SwiftUI
@main
struct ShareZApp: App {
@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
class AppDelegate: NSObject, NSApplicationDelegate, NSMetadataQueryDelegate {
let query = NSMetadataQuery()
func applicationDidFinishLaunching(_ notification: Notification) {
let center = NotificationCenter.default
center.addObserver(self, selector: #selector(queryUpdated(notification:)), name: NSNotification.Name.NSMetadataQueryDidStartGathering, object: query)
center.addObserver(self, selector: #selector(queryUpdated(notification:)), name: NSNotification.Name.NSMetadataQueryDidUpdate, object: query)
center.addObserver(self, selector: #selector(queryUpdated(notification:)), name: NSNotification.Name.NSMetadataQueryDidFinishGathering, object: query)
query.delegate = self
query.searchScopes = []
query.enableUpdates()
query.predicate = NSPredicate(format: "kMDItemIsScreenCapture = 1")
query.start()
}
@objc func queryUpdated(notification: NSNotification) {
print(notification)
}
deinit {
print("deinit")
}
}
不幸的是,這種方法不起作用,從網上的消息來源看來,macOS 10.15 做了一些更改,檢測到類似的事件。 這里也是這種情況嗎? 在屏幕截圖上運行xattr
命令也會顯示元標記,因此它沒有被刪除:
還是有另一種方法來檢測這個元標記? 我正在運行 macOS Big Sur 11.2.3。
所以似乎有一種更簡單的方法來實現我想要做的事情,我可以調用/usr/sbin/screencapture
可執行文件來創建屏幕截圖,並使用-i -r </path/to/target>
標志我可以指定一個 output 路徑。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.