Use the cursorTarget at the time the gear dialog was opened instead of the cursorTarget at the time the script runs. The script can be delayed by several seconds (spawn is scheduled).