MacOS – How to script Preferences changes in an application

applescriptmacossafari

I want to use applescript to change a checkbox in the preferences box of Safari. I can open the preference window, but how to code the check box change?

Example: I'd like to toggle the check box for "Block pop-up windows" on tab "Security" of the preferences

tell application "Safari" to activate
   tell application "System Events"
      keystroke "," using command down
   end tell
   --some magic goes here
end tell