6 Replies Latest reply: Apr 2, 2013 9:59 AM by Derick Fay
SilentMountain Level 1 Level 1 (0 points)

UI Element Scripting can be accomplished using AppleScript.

 

I needed a script that would delete my Netflix cookie from Safari and sign me back in.

 

I used AppleScript to create the following "Reset Cookie" application.

 

1. Open AppleScript Editor in ~\Applications\Utilities

2. Paste in the following code, then click "Compile" then "Run"

3. Save as an Application (Run Only)

 

set theWebsite to "netflix"

tell application "Safari" to activate

delay 2

tell application "System Events" to tell process "Safari"

  keystroke "," using command down

          tell window 1

  click button -3 of tool bar 1

  delay 1

  click button "Details…" of group 2 of UI element 2 of row 1 of table 1 of UI element 1 of scroll area 1 of group 1 of group 1

  delay 1

  keystroke theWebsite

  keystroke tab

  delay 1

  keystroke "a" using command down

  delay 1

                    click button "Remove" of sheet 1

                    click button "Done" of sheet 1

          end tell

  keystroke "w" using command down

          tell application "Safari" to set the URL of the front document to the "https://signup.netflix.com/Login"

  delay 2

  keystroke return

end tell

 

Notes:

- there is a variable called theWebsite which specifies the name of Cookie I want to look for in Safari's Privacy settings

- the script opens Safari, opens Preferences, and selects the Privacy Tab

- the trick is to target the "Details..." button, which is buried deep in the UI hierarchy

- once that's open, theWebsite value is entered into the search field, the results are selected and removed.

- finally, close the dialog, and tell Safari to open the new webpage (for me, the Netflix sign in page)

- since keychain has my login info, I just tell the script to hit "Return" (the delay gives the page a chance to load, which is important - set that higher for slower connections

 

Make sure to read this first to make sure your Mac is configured to accept UI Scripting via the Accessability System Preferences and to learn more about UI Scripting.

 

http://www.macosxautomation.com/applescript/uiscripting/index.html

  • twtwtw Level 5 Level 5 (4,900 points)

    Just as an observation, Safari cookies are stored in a plist file at ~/Library/Cookies/Cookies.plist.  It might be easier to quit Safari, then find and edit the cookie info in that file directly.

  • SilentMountain Level 1 Level 1 (0 points)

    You've clearly missed the point. This is to create a 1-click automated process. What you describe requires locating a file in an invisible directory, opening the plist file in a text editor, and locating the correct text in the file and modifying it correclly. None of which you should do while Safari is open, and which is much more complicated from a script perspective.

  • SilentMountain Level 1 Level 1 (0 points)

    Update

     

    I was having unreliable results with this line:

     

    click button -3 of tool bar 1

     

    so I changed it to:

     

    click button "Privacy" of tool bar 1

     

    Other Notes:

     

    • Increased the first delay to 4, since if Safari was closed it didn't have time to spawn a new window before kicking off the Preference Window process. This would throw an error for the last line to navigate to said URL. If you don't use the last URL line, the delay should be ok. Depends on how long it takes your machine to load Safari.
    • The (Run Only) suggestion makes it so you can't edit the script later, so ignore that one :/
    • Many of the delays in the middle of the code are just for user benefit - it seems to run fine without them, but I like being able to see it happening.


  • twtwtw Level 5 Level 5 (4,900 points)

    You've misunderstood.  all of what I suggested can be done in a script, with no need for GUI scripting.  When I get a chance I'll write it up.

  • fasgf354qvt2b5 Level 1 Level 1 (5 points)

    Thanks for this script, works nicely for me except for the Details... button line.  In my version of Safari (6.0.1), I had to change it to:

    click button "Details…" of group 1 of group 1

     

    For the future reference of other people who get an error on that line (the error is simply that it is unable to retrieve the element as described), I identified the correct reference to the button by opening Safari, opening the preferences page, going to the Privacy tab, then creating and running this one-line script:

    tell application "System Events" to get entire contents of window 1 of process "Safari"

     

    The Result (and the Replies) should list all of the elements of the Window (it is possible to narrow this down by modifying the script, but this approach will give you everything and you can just scroll until you see "Details...").  Find "Details...", it will look something like this:

    button "Details…" of group 1 of group 1 of window "Privacy" of application process "Safari" of application "System Events"

     

    Because the OP's script already has us in in a "tell window 1" block, we can (and must) omit the latter part of the line starting with "of window".

  • Derick Fay Level 1 Level 1 (10 points)

    I had to tweak this with some delays for Safari 6.0.3, and eliminated the auto-load at the end (since I'm not using it for Netflix ).  This version seems to work consistently:

     

    set theWebsite to "wiley"

    tell application "Safari" to activate

    delay 2

    tell application "System Events" to tell process "Safari"

      keystroke "," using command down

      delay 2

              tell window 1

                        click button "Privacy" of tool bar 1

      delay 1

                        click button "Details…" of group 1 of group 1

      delay 1

      keystroke theWebsite

      keystroke tab

      delay 1

      keystroke "a" using command down

      delay 1

                        click button "Remove" of sheet 1

                        click button "Done" of sheet 1

              end tell

      keystroke "w" using command down

    end tell