
    lKjk                         d Z ddlZddlZddlZddlmZ  ej                  e      Zde	fdZ
de	fdZdee	e	f   fdZd	dZy)
zB
macOS permission checker for Accessibility and Screen Recording.
    N)Tuplereturnc                  z    	 ddl m}   |        S # t        $ r"}t        j	                  d|        Y d}~yd}~ww xY w)z}
    Check if Accessibility permission is granted.

    Returns:
        True if permission is granted, False otherwise.
    r   )AXIsProcessTrustedz*Failed to check Accessibility permission: NF)ApplicationServicesr   	Exceptionloggererror)r   es     f/Users/ahmed/devFolder/Ultron/claude-voice/.venv/lib/python3.12/site-packages/macos_mcp/permissions.pycheck_accessibility_permissionr      s:    :!## A!EFs    	:5:c                      	 t        j                  g dddd      } | j                  dk(  S # t        $ r"}t        j                  d|        Y d}~yd}~ww xY w)	z
    Check if Screen Recording permission is granted.

    Returns:
        True if permission is granted, False otherwise.
    )	osascriptz-ez/tell application "System Events" to get versionT   )capture_outputtimeouttextr   z.Could not verify Screen Recording permission: NF)
subprocessrun
returncoder   r	   warning)resultr   s     r   !check_screen_recording_permissionr      s[    
R	
   A%% GsKLs   ), 	AAAc                     t               } t               }| r|sig }| s|j                  d       |s|j                  d       t        j	                  ddj                  |       d       t        j                  ddgd	       | |fS )
z
    Request missing permissions by opening System Preferences.

    Returns:
        Tuple of (accessibility_granted, screen_recording_granted)
    AccessibilityScreen RecordingzMissing permissions: , zC. Opening System Preferences. Please grant permissions and restart.openzMx-apple.systempreferences:com.apple.preference.security?Privacy_Accessibility   )r   )r   r   appendr	   r   joinr   r   )accessibility_okscreen_recording_okmissings      r   request_permissionsr%   1   s     67;=#6NN?+"NN-.#DIIg$6#7 8P P	
 	_ 	
 000    c                  r   ddl } | j                  j                  dd      dk(  }t               \  }}|r|sg }|s|j	                  d       |s|j	                  d       dd	j                  |       d
}|rt        j                  |dz          yt        j                  |       t        j                  d       yy)a  
    Validate that all required permissions are granted.

    When launched as a subprocess by a parent that already holds TCC grants
    (e.g. Claude Desktop), AXIsProcessTrusted() returns False for the child
    binary even though AX calls succeed via inheritance.  Setting
    MACOS_MCP_SKIP_PERMISSION_CHECK=1 downgrades the failure to a warning so
    the server starts instead of exiting.
    r   NMACOS_MCP_SKIP_PERMISSION_CHECK01r   r   z"Required permissions not granted: r   z[. Please enable them in System Preferences > Privacy & Security and restart the MCP server.u:    (continuing anyway — MACOS_MCP_SKIP_PERMISSION_CHECK=1)   )osenvirongetr%   r    r!   r	   r   r
   sysexit)r,   skipr"   r#   r$   msgs         r   validate_permissionsr3   S   s     ::>>;SASHD,?,A))#6NN?+"NN-. 171C0D Eh h 	 NN3!]]^LLHHQK $7r&   )r   N)__doc__r   loggingr/   typingr   	getLogger__name__r	   boolr   r   r%   r3    r&   r   <module>r;      s]      
 			8	$  4 (1U4:. 1Dr&   