on enabledGUIScripting(switch)
-- Call this handler and pass 'true' in the switch parameter to enable GUI Scripting before your script executes any GUI Scripting commands, or pass 'false' to disable GUI Scripting. You need not test the 'UI elements enabled' setting before calling this handler, because authorization is required only if 'UI elements enabled' will be changed. Returns the final setting of 'UI elements enabled', even if unchanged.
tell application "System Events"
activate -- brings System Events authentication dialog to front
set UI elements enabled to switch
return UI elements enabled
end tell
end enabledGUIScripting
That's for use in an AppleScript script, obviously.
But you can also embed an AppleScript command setting the 'UI elements enabled' property in a Cocoa application, and call it from a button. menu item, or whatever. I do that in several of my products.