Screen capture with LaunchAgent

I made a screen capture command line tool LaunchAgent. When launched, the screen capture policy alert did not pop up, so I couldn’t capture the right screenshots. I have no idea how to correct this. Could you help me with it? Thank you.

the command line tool path: /bin/capture

code follows:

#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;

}

the LaunchAgent plist file path: /Library/LaunchAgent/com.test.launchagent.screencapture.plist

and the LaunchAgent plist follows:


<!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>

For this to work the TCC subsystem has to be able to identify some app as the responsible code for your agent. For example, if your agent were a helper tool embedded in an app, TCC would recognise that and treat the app as the responsible code, using its name in the permission alert and so on.

So, the best way to fix this depends on your intended deployment strategy for this code. Is it something that you’re hacking together for your own use? Or do you intend to ship it as a product?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

When I added UserName: User on the launch agent plist

Do you mean that you literally set the UserName property User? If so, that’s nonsensical. A launchd agent is always run by the user associated with the context in which it’s loaded. The UserName property only makes sense in the context of a launchd daemon.

the permission alert correctly popped out

Well, that’s weird. However, it doesn’t change things. I’m still looking for answers to these questions:

The best way to fix this depends on your intended deployment strategy for this code. Is it something that you’re hacking together for your own use? Or do you intend to ship it as a product?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Screen capture with LaunchAgent
 
 
Q