import subprocess def get_calculator_result(): """Captures the result from the macOS Calculator app and returns it as a string.""" applescript = ''' tell application "Calculator" activate delay 0.5 tell application "System Events" to keystroke "3" tell application "System Events" to keystroke "+" tell application "System Events" to keystroke "1" tell application "System Events" to keystroke "=" set the clipboard to (get the result as string) return the clipboard as string end tell ''' result = subprocess.run(['osascript', '-e', applescript], capture_output=True, text=True) return result.stdout.strip() # Get the calculator result and print it result = get_calculator_result() print("Calculator result:", result)