[英]macOS : programmatic check if process runs as a launchDaemon or launchAgent or from command-line
[英]Macos Screen capture with LaunchAgent
我做了一個截屏命令行工具 LaunchAgent。 啟動時,屏幕捕獲策略警報沒有彈出,所以我無法捕獲正確的屏幕截圖。 我不知道如何糾正這個問題。 你能幫我嗎? 謝謝你。
命令行工具路徑: /bin/capture
代碼如下:
#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
NSLog(@"capture screen...");
CGRect mainRect = CGDisplayBounds(CGMainDisplayID());
CGImageRef desktopImage = CGWindowListCreateImage(mainRect, kCGWindowListOptionOnScreenOnly, kCGNullWindowID, kCGWindowImageBestResolution | kCGWindowImageShouldBeOpaque);
NSBitmapImageRep *bmpImgRef = [[NSBitmapImageRep alloc] initWithCGImage:desktopImage];
NSData *data = [bmpImgRef representationUsingType:NSBitmapImageFileTypeJPEG properties:@{NSImageCompressionFactor: @(1)}];
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
[fmt setDateFormat:@"yyyyMMdd_hh:mm:ss"];
[data writeToURL:[NSURL fileURLWithPath:[NSString stringWithFormat:@"/tmp/%@.jpg", [fmt stringFromDate:[NSDate date]]]] atomically:true];
}
return 0;
}
LaunchAgent plist 文件路徑: /Library/LaunchAgent/com.test.launchagent.screencapture.plist
LaunchAgent plist 如下:
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.test.launchagent.screencapture</string>
<key>ProgramArguments</key>
<array>
<string>/bin/capture</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
你確實需要幾件事來完成這項工作。
首先,您需要為您的二進制文件提供一個 Info.plist。 如果您關心僅保留單個 unix 文件而不是創建捆綁包,則可以使用嵌入式 Info.plist(可以在 Xcode 構建設置中設置)。
其次,您必須更改啟動 plist 以在Aqua session 中運行您的程序。
通過這些步驟,您應該能夠運行它並查看策略彈出窗口。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.