Skip to content

macos/screencapturekit: add support with TCC error handling and example #279

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

tmc
Copy link
Collaborator

@tmc tmc commented Mar 2, 2025

This PR adds support for Apple's ScreenCaptureKit framework to DarwinKit with comprehensive TCC (Transparency, Consent, and Control) permission handling.

Features

Core Screenshot Functionality

  • SCScreenshotManager for taking screenshots
  • Proper error handling via SCError type
  • Modern DarwinKit API design using objc.Call pattern

TCC Permission Handling

  • Comprehensive error detection for TCC-related issues
  • Helper methods to identify and handle permission errors
  • User-friendly guidance for resolving permission problems
  • System Preferences integration for easier permission granting
  • Automatic retry mechanism for temporary permission issues

Additional Types

  • SCStreamConfiguration for screen capture configuration
  • SCContentFilter for filtering capture content
  • SCDisplay and SCWindow type definitions for future expansion

Example Application

  • Added screenshot example demonstrating proper usage
  • Shows permission handling with retry logic
  • Provides user guidance for resolving permission issues
  • Saves screenshots to the desktop

Usage Example

// Basic usage
manager := screencapturekit.SCScreenshotManager_SharedManager()
manager.CaptureScreenshotWithCompletion(func(image appkit.Image, err screencapturekit.SCError) {
    if \!err.IsNil() {
        fmt.Printf("Error: %v\n", err)
        return
    }
    // Process the image...
})

// With retry logic for TCC permission issues
manager.CaptureScreenshotWithRetry(3, time.Second, func(image appkit.Image, err screencapturekit.SCError, success bool) {
    if \!success {
        if err.IsTCCError() {
            fmt.Println(err.GetPermissionInstructions())
            screencapturekit.OpenSystemPreferencesTCC()
        }
        return
    }
    // Process the image...
})

@tmc tmc mentioned this pull request Mar 2, 2025

// GetPermissionInstructions returns instructions for how to grant screen recording permissions
func (e SCError) GetPermissionInstructions() string {
if \!e.IsTCCError() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is going on here?

@progrium
Copy link
Owner

progrium commented Mar 8, 2025

claude generated? if we ran clobber and this package was deleted, how would it be regenerated?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants