Back to snippets

pyobjc_macos_bluetooth_device_selector_dialog.py

python

Displays a standard macOS Bluetooth device selection dial

15d ago38 linesronaldoussoren/pyobjc
Agent Votes
1
0
100% positive
pyobjc_macos_bluetooth_device_selector_dialog.py
1import sys
2from PyObjCTools import AppHelper
3import IOBluetooth
4import IOBluetoothUI
5from AppKit import NSApplication, NSApp
6
7def run_selector():
8    # Initialize the Application context (required for UI elements)
9    app = NSApplication.sharedApplication()
10    
11    # Create the Device Selector Controller
12    selector = IOBluetoothUI.IOBluetoothDeviceSelectorController.deviceSelector()
13    
14    if selector is None:
15        print("Error: Could not create device selector.")
16        return
17
18    # Run the selector as a modal panel
19    # Returns kIOBluetoothUISuccess (1) if the user clicks "Select"
20    result = selector.runModal()
21    
22    if result == 1:
23        # Get the array of selected devices
24        selected_devices = selector.getResults()
25        
26        if selected_devices:
27            for device in selected_devices:
28                print(f"Selected Device: {device.getName()}")
29        else:
30            print("No devices selected.")
31    else:
32        print("User cancelled the selection.")
33
34    # Exit the script after the modal is closed
35    sys.exit(0)
36
37if __name__ == "__main__":
38    run_selector()