System Events GUI Applescript for Safari - error

Howdy, all. :smiley:

I recently made a little System Events script that installs a custom CSS file in Safari. The script works fine for me, but some other people are having problems with it - specifically, it throws an ā€œNSReceiverEvaluationScriptError:4ā€ error. I think this might have to do with the layout in different versions of Safari. Here’s the code:


tell application "System Events" to activate
tell application "Safari" to activate
tell application "System Events"
	click button "Safari" of application process "Dock" of application "System Events"
	keystroke "," using command down --"Preferences", in Safari 1.3.2
	tell me to delay 3 --this should give enough time for the preferences window to become the front window.
	click button "Advanced" of tool bar 1 of front window of application process "Safari" of application "System Events"
	click pop up button 1 of group 1 of group 1 of front window of application process "Safari" of application "System Events"
	pick menu item "Other." of menu 1 of pop up button 1 of group 1 of group 1 of front window of application process "Safari" of application "System Events"
	keystroke "d" using command down
	keystroke "ad.block.css"
	keystroke return
	keystroke "w" using command down
	keystroke "w" using command down
end tell

provided there’s a file called ā€œad.block.cssā€ on the desktop, the script should work. :confused:

–edited to add:
Incidentally, increasing the amount of delay does not fix the problem.

Hi Alex

:lol:, what the hell is this? ;);):wink:

The common syntax for UI scripting an application is

activate application "theApp"
tell application "System Events"
	tell application process "theApp"
...
	end tell
end tell

Try this (I’ve tested it only in Safari 2.0.4):

activate application "Safari"
tell application "System Events"
	tell application process "Safari"
		keystroke "," using command down --"Preferences", in Safari 1.3.2
		delay 1 -- one second might be sufficient
		tell window 1
			click button "Advanced" of tool bar 1
			set Stylesheet to pop up button 1 of group 1 of group 1
			click Stylesheet
			pick menu item "Other." of menu 1 of Stylesheet
		end tell
		keystroke "d" using command down
		keystroke "ad.block.css"
		keystroke return
		keystroke "w" using command down
		keystroke "w" using command down
	end tell
end tell

Well. It works. But where does ā€˜pick’ come from? :confused:

Here’s a version that’s (hopefully) user-language independent and covers a couple of other eventualities:

activate application "Safari"
tell application "System Events"
	tell application process "Safari"
		keystroke "," using command down --"Preferences", in Safari 1.3.2
		repeat until ((count window 1 each button) is 4)
			delay 0.2
		end repeat
		tell window 1
			if not (tool bar 1 exists) then click button 4
			click button -1 of tool bar 1
			tell pop up button 1 of group 1 of group 1
				click
				pick menu item -1 of menu 1
			end tell
			repeat until (sheet 1) exists
				delay 0.2
			end repeat
			keystroke "d" using command down
			set cssFileFound to (static text "ad.block.css" of list 1 of scroll area 1 of scroll area 1 of browser 1 of splitter group 1 of group 1 of sheet 1 exists)
			if (cssFileFound) then
				keystroke "ad.block.css" & return
			else
				keystroke "." using {command down}
			end if
		end tell
		keystroke "w" using {command down}
	end tell
end tell
if not (cssFileFound) then
	tell application "Safari"
		display dialog "No file called \"ad.block.css\" was found on the desktop" buttons {"OK"} default button 1 with icon stop
	end tell
end if

I was using ā€œUIElementInspectorā€, and the action it gave for selecting a menu was ā€œpickā€

I guess I have a lot to learn about GUI scripting.:o

Thanks for your ā€œrobustā€ version, Nigel.
I was also wondering about the ā€œpickā€ keyword,
there is no documentation at all, but it works.
Strange.