GameController notification when connect/disconnect

Example show how to use Notification for connect/disconnect of game controller.
The boolean value for variable gameControllerConnect could be true or false.

Have been tested on DualSense Game Controller

use framework "Foundation"
use framework "GameController"
use scripting additions

set theController to current application's GCController's controllers()'s firstObject()

-- Set true or false for testing the notification for connect/disconnect of game controller
set gameControllerConnect to true

if gameControllerConnect then
	set {notificationName, theSelector} to {"GCControllerDidConnectNotification", "controllerDidConnect:"}
	display dialog "Connect your Game Controller,... wait 10 seconds" giving up after 10
	set {notificationName, theSelector} to {"GCControllerDidDisconnectNotification", "controllerDidDisconnect:"}
	display dialog "Disconnect your Game Controller,..." & return & "Press PS + Circle-button in 10 seconds or more" giving up after 20
end if

set notificationCenter to current application's NSNotificationCenter's defaultCenter()
notificationCenter's addObserver:me selector:theSelector |name|:notificationName object:theController

on controllerDidConnect:sender
	log "Game Controller Connected..."
	my (notificationCenter's removeObserver:me |name|:notificationName object:theController)
end controllerDidConnect:

on controllerDidDisconnect:sender
	log "Game Controller Disconnect..."
	my (notificationCenter's removeObserver:me |name|:notificationName object:theController)
end controllerDidDisconnect:
1 Like