Command line app doesn't prompt for a permission when it runs from from a terminal

I've made a simple command line app that requires Screen recording permission.

When I ran it from Xcode, it prompts for a permission and once I allowed it from the settings, it runs well.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <CoreGraphics/CGDisplayStream.h>

int main() {
	printf("# Start #\n");
	
	if (CGPreflightScreenCaptureAccess()) {
		printf("# Permitted.\n");
	} else {
		printf("# Not permitted.\n");
		if (CGRequestScreenCaptureAccess() == false) {
			printf("# CGRequestScreenCaptureAccess() returning false\n");
		}
	}
	
	size_t output_width = 1280;
	size_t output_height = 720;
	
	dispatch_queue_t dq = dispatch_queue_create("com.domain.screengrabber", DISPATCH_QUEUE_SERIAL);
	
	CGError err;
	
	CGDisplayStreamRef sref = CGDisplayStreamCreateWithDispatchQueue(
		1,
		output_width,
		output_height,
		'BGRA',
		NULL,
		dq,
		^(
		    CGDisplayStreamFrameStatus status,
		    uint64_t time,
		    IOSurfaceRef frame,
		    CGDisplayStreamUpdateRef ref
		) {
		    printf("Got frame: %llu, FrameStatus:%d \n", time, status);
	    }
	);
	
	err = CGDisplayStreamStart(sref);
	
	if (kCGErrorSuccess != err) {
		printf("Error: failed to start streaming the display. %d\n", err);
		exit(EXIT_FAILURE);
	}
	
	while (true) {
		usleep(1e5);
	}
	
	CGDisplayStreamStop(sref);
	
	printf("\n\n");
	return 0;
}

Now I want to execute this from terminal, so I went to the build folder and typed the app name.

cd /Users/klee/Library/Developer/Xcode/DerivedData/ScreenStreamTest-ezddqbkzhndhakadslymnvpowtig/Build/Products/Debug

./ScreenStreamTest

But I am getting following output without any prompt for permission.

# Start #
# Not permitted.
# CGRequestScreenCaptureAccess() returning false
Error: failed to start streaming the display. 1001

Is there a something I need to consider for this type of command line app?

The fact that you’re seeing different behaviour when running from Xcode isn’t a huge surprise. Xcode’s debugging infrastructure has smarts to separate the responsible code chain that would otherwise lead from your tool to the Xcode app.

It’s not entirely clear why this code doesn’t prompt when you run it from Terminal. Before I spend time on that, I’d like to ask about your final goal here. Do you plan to build a tool that users run from Terminal? If not, what are you building?

Also, is there a reason you’re using this legacy API rather than ScreenCaptureKit? The ScreenCaptureKit section of the macOS Sequoia 15 Release Notes make it clear that this is not going to yield the best user experience.

Share and Enjoy

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

Command line app doesn't prompt for a permission when it runs from from a terminal
 
 
Q