You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Trying to type into a field with AppleScript

I'm trying to write a script that will remove a set of cookies from Safari. I can get the Preferences window open and setting the focus in the search field. At that point, the script fails (see "<<<" below). Any thoughts as to why the strings aren't being placed into the search field?


TIA,

David


===================


set deCookie to {"newsweek.com", "newyorker.com", "cnn.com"}

tell application "System Events"

tell application process "Safari"

set frontmost to true

keystroke "," using command down

delay 1

tell window 1

click button "Privacy" of toolbar 1 of it

try

click button "Manage Website Data…" of group 1 of group 1

-- give cookies time to load in their scrollable window

delay 3

repeat with d in deCookie

click text field "Search"

try

keystroke d -- <<< this is where this script is failing <<<

delay 1

select row 1 of table 1 of scroll area 1 of sheet 1

click button "Remove" of sheet 1

end try

end repeat

click button "Done" of sheet 1

on error errmsg number errnbr

keystroke "w" using command down

display alert "No cookies to remove" giving up after 5

return

end try

end tell

keystroke "w" using command down

display alert "All cookies removed" giving up after 5

end tell

end tell

return

MacBook Pro 13″, macOS 11.1

Posted on Jan 5, 2021 6:14 AM

Reply
Question marked as Top-ranking reply

Posted on Jan 5, 2021 10:05 AM

It is working now with selected list items.


I encountered a problem with Safari 14.0.2 on macOS 11.1 after the script runs, if it is run again, the Manage Website Data panel opens and is frozen, requiring one to quit Safari, so I have added that as a last step, to work around that issue.


Revised script:

use scripting additions

set deCookie to {"2mdn.net", "adnxs.com", "adsrvr.org", "agkn.com"}

tell application "System Events"
	tell application process "Safari"
		set frontmost to true
		keystroke "," using command down
		delay 1
		tell window 1
			click button "Privacy" of toolbar 1 of it
			try
				repeat with d in deCookie
					click button "Manage Website Data…" of group 1 of group 1
					-- give cookies time to load in their scrollable window
					delay 2
					select -- focuses on the search window
					try
						keystroke d
						delay 2
						select row 1 of table 1 of scroll area 1 of sheet 1
						delay 1
						click button "Remove" of sheet 1
					end try
				end repeat
				click button "Done" of sheet 1
			on error errmsg number errnbr
				click button "Done"
				keystroke "w" using command down
				display alert "No cookies to remove" giving up after 5
				return
			end try
		end tell
		keystroke "w" using command down
		display alert "Selected cookies removed" giving up after 5
	end tell
end tell
# because if you don't do this, the next time you open Preferences it will be hung
tell application "Safari" to if it is running then quit
return


Similar questions

5 replies
Question marked as Top-ranking reply

Jan 5, 2021 10:05 AM in response to drschwartz

It is working now with selected list items.


I encountered a problem with Safari 14.0.2 on macOS 11.1 after the script runs, if it is run again, the Manage Website Data panel opens and is frozen, requiring one to quit Safari, so I have added that as a last step, to work around that issue.


Revised script:

use scripting additions

set deCookie to {"2mdn.net", "adnxs.com", "adsrvr.org", "agkn.com"}

tell application "System Events"
	tell application process "Safari"
		set frontmost to true
		keystroke "," using command down
		delay 1
		tell window 1
			click button "Privacy" of toolbar 1 of it
			try
				repeat with d in deCookie
					click button "Manage Website Data…" of group 1 of group 1
					-- give cookies time to load in their scrollable window
					delay 2
					select -- focuses on the search window
					try
						keystroke d
						delay 2
						select row 1 of table 1 of scroll area 1 of sheet 1
						delay 1
						click button "Remove" of sheet 1
					end try
				end repeat
				click button "Done" of sheet 1
			on error errmsg number errnbr
				click button "Done"
				keystroke "w" using command down
				display alert "No cookies to remove" giving up after 5
				return
			end try
		end tell
		keystroke "w" using command down
		display alert "Selected cookies removed" giving up after 5
	end tell
end tell
# because if you don't do this, the next time you open Preferences it will be hung
tell application "Safari" to if it is running then quit
return


Jan 5, 2021 1:26 PM in response to drschwartz

You are welcome. Took some time to beat it into submission though…


If you wanted to automatically create a list of cookies that you want to remove without the laborious list typing, you could create a text file with one, unquoted, cookie name per line. Then let AppleScript read that into your deCookie list.


Cookie.txt (arbitrary name)

youtube.com
foobar.net
notthiscookie.org
googleadscontent.com


AppleScript:

# select the file containing the cookies to remove

set cookie_doc to (choose file of {"public.text"} default location (path to desktop))
set deCookie to (read cookie_doc as text using delimiter linefeed) as list

--> {"youtube.com", "foobar.net", "notthiscookie.org", "googleadscontent.com"}


Trying to type into a field with AppleScript

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.