Autorun on Safari window open

Hiya,

OK, so I wrote a simple script to click “View > Text Encoding > Unicode (UTF-8)”.

What I need, if possible, is have it run automatically every time a new Safari window is opened.

So, possible?

Thanks!

Wouldn’t it be easier, to change the default text encoding in the Preferences > Appearance?

It would be, sure. Theoretically. But for some reason, changing the default in Preferences > Appearance to Unicode (UTF-8) doesn’t work.

You can set that preference as such, and it looks like it’s working, but then for whatever reason it doesn’t. It’s kind of awkward to explain.

What happens is that some punctuation comes in funky if the Text Encoding isn’t set to Unicode. Accented/umlaut characters come in as random characters or simply drop out. So even if the default is set to Unicode in Prefs > Appearance, and the “Default” option is selected in the View menu, the characters are still messed up. Therefore, the only solution I can come up with is setting the Text Encoding to Unicode, which I’m trying now to automate.

(And yes, I’ve tried all of the suggestions I could find to fix the problem - delete font cache, trash/recreate com.apple.Safari.plist, et cetera.)

Here’s what I can think of. It delves into a bit of GIU scripting, but will hopefully get the job done.

tell application "System Events"
	if exists process "Safari" then
		tell process "Safari"
			set windowNum to 0
			repeat
				if (count windows) is greater than windowNum then
					activate
					click menu item "Unicode (UTF-8)" of menu 1 of menu item "Text Encoding" of menu 1 of menu bar item "View" of menu bar 1
					set windowNum to (count windows)
				else if (count windows) is less than windowNum then
					set windowNum to (count windows)
				end if
				delay 1
			end repeat
		end tell
	end if
end tell