I modified a script from this hint of phow4rd to toggle Safari's Accept Cookies setting from 'Never' to 'Only from sites I visit.'
Here is the modified script. You can save it to ~/Library/Scripts/Applications/Safari to make it available from the AppleScript menu. You may need to create the Safari folder.
tell application "Safari" to activate
tell application "System Events"
tell application process "Safari"
click menu item "Preferences…" of menu "Safari" of ¬
menu bar item "Safari" of menu bar 1
end tell
tell application "System Events"
click button "Security" of tool bar 1 of window 1 of ¬
application process "Safari"
delay 1
if the value of radio button "Never" of radio group 1 of group 1 of group 1 of window "Security" of application process "Safari" is 0 then
click radio button "Never" of ¬
radio group 1 of group 1 of group 1 of window "Security" of application process "Safari"
else
click radio button "Only from sites I visit" of ¬
radio group 1 of group 1 of group 1 of window "Security" of application process "Safari"
end if
get value of radio button "Never" of ¬
radio group 1 of group 1 of group 1 of window "Security" of application process "Safari"
copy the result as number to varResult
if varResult = 1 then
set varResult to "'Never'"
else
set varResult to "'Only from sites I visit'"
end if
click button 1 of window "Security" of application process "Safari"
display dialog ("\"Toggle Cookies On/Off \" script Confirmation:" & ¬
return & return & "'Accept Cookies' is set now to " & varResult & ".") ¬
buttons {"OK"} default button "OK" giving up after 3 with icon 1
end tell
end tellMac OS X Hints
http://hints.macworld.com/article.php?story=20100818114906805