Turning pictures in Camino (Web Browser) on and off

EDIT: My new script is in the 4th post. (8/24/07)
EDIT2: Final code is in 10th post. (9/2/07)

I needed this because I’m on a much slower computer and killing pictures is a huge help with load times. Enjoy!

(*Please note that this is a GUI Script, so while it is running, you will not want to do anything else.*)

--Get Answer
display dialog "Show pictures in Camino (Yes or No):" default answer "No"
copy the result as list to {Answer}

--Set users file locations as variables
set Userjs to ((path to application support folder from user domain as Unicode text) & "Camino:user.js") as file specification
set Usertxt to ((path to application support folder from user domain as Unicode text) & "Camino:user.txt") as file specification
set caminoFolder to ((path to application support folder from user domain as Unicode text) & "Camino:")

tell application "Finder"
	--Check the existance of file "user.js" and if it does, to change the extention to .txt
	if the Userjs exists then
		set the name of the Userjs to "user.txt"
		set Have to 1
	else
		try
			--Try to make user.txt if user.js doesn't exist
			make new file at caminoFolder with properties {name:"user.txt"}
			set Have to 0
		end try
	end if
end tell

--Open user.txt
tell application "TextEdit"
	open Usertxt
end tell

--Bring TextEdit to the front
activate application "TextEdit"

--GUI Script TextEdit to change the line of javascript so pictures are off
if Answer is "No" then
	tell application "System Events"
		tell process "TextEdit"
			--This is the command for "Find"
			keystroke "f" using command down
			repeat until exists window 1
				delay 0.5
			end repeat
			--Window 1 is the "Find" window
			if Have is 1 then
				try
					tell window 1
						keystroke "user_pref(\"permissions.default.image\", 1);"
						keystroke tab
						keystroke "user_pref(\"permissions.default.image\", 2);"
						delay 1.5
						click button "Replace All"
					end tell
				end try
			else
				--Here's where people who didn't have a user.js file come in.
				try
					tell window 1
						keystroke ""
						keystroke tab
						keystroke "user_pref(\"permissions.default.image\", 2);"
						delay 1.5
						click button "Replace"
					end tell
				end try
			end if
			delay 1
			keystroke "s" using command down
		end tell
	end tell
	delay 1
	tell application "TextEdit"
		quit
	end tell
	tell application "Finder"
		set the name of the Usertxt to "user.js"
	end tell
end if

--GUI Script TextEdit to change the line of javascript so pictures are on
if Answer is "Yes" then
	tell application "System Events"
		tell process "TextEdit"
			--This is the command for "Find"
			keystroke "f" using command down
			repeat until exists window 1
				delay 0.5
			end repeat
			--Window 1 is the "Find" window
			if Have is 1 then
				try
					tell window 1
						keystroke "user_pref(\"permissions.default.image\", 2);"
						keystroke tab
						keystroke "user_pref(\"permissions.default.image\", 1);"
						delay 1.5
						click button "Replace All"
					end tell
				end try
			else
				--Here's where people who didn't have a user.js file come in.
				try
					tell window 1
						keystroke ""
						keystroke tab
						keystroke "user_pref(\"permissions.default.image\", 1);"
						delay 1.5
						click button "Replace"
					end tell
				end try
			end if
			delay 1
			keystroke "s" using command down
		end tell
	end tell
	delay 1
	tell application "TextEdit"
		quit
	end tell
	tell application "Finder"
		set the name of the Usertxt to "user.js"
	end tell
end if

I’d think flipping the number in about:config would be a much less painful way of accomplishing this. :slight_smile:

http://imagesocket.com/view/aboutconfig82f.png

Edit: Also, it’s fail-safe. If something steals focus or otherwise doesn’t match your assumptions of the state of the UI, your script doesn’t work.

Wow… I was not aware of about:config… shoots self >.< lol omg this sucks. Well, I’ll stay positive because writing that script taught me a lot about Applescript. :slight_smile:

Here’s what I was referencing when I wrote this script. http://www.caminobrowser.org/documentation/hiddenprefs/

EDIT: Hehe… I had my “if button_pressed is “x” then” mixed up so on was off and off was on. >.< It’s fixed now.
EDIT#2: When I have multiple pages up, the net was a bit laggier and the final keystroke return was happening too fast, so I just edited it so it’s a repeat until sheet exists line instead. :slight_smile:

Okay, well it took me awhile to get around it because I had other stuff going on, but here’s the new code. :smiley:

display dialog "The state of pictures in Camino" buttons {"On", "Off"}
copy the result as list to {button_pressed}

tell application "Camino"
	open url "about:config"
end tell

activate application "Camino"

if button_pressed is "Off" then
	tell application "System Events"
		try
			keystroke "permissions.default.image"
			delay 0.2
			keystroke tab
			delay 0.2
			key code 125
			delay 0.2
			keystroke return
			repeat until exists sheet 1 of window 1 of process "Camino"
				delay 0.5
			end repeat
			keystroke "2"
			delay 0.2
			click button 1 of sheet 1 of window 1 of process "Camino"
		end try
	end tell
end if

if button_pressed is "On" then
	tell application "System Events"
		try
			keystroke "permissions.default.image"
			delay 0.2
			keystroke tab
			delay 0.2
			key code 125
			delay 0.2
			keystroke return
			repeat until exists sheet 1 of window 1 of process "Camino"
				delay 0.2
			end repeat
			keystroke "1"
			delay 0.5
			click button 1 of sheet 1 of window 1 of process "Camino"
		end try
	end tell
end if

And if the browser or script stutters for a half-second?

Seriously, man, you’re hitting a fly with a rocket launcher.

Well I don’t know what you would do instead, but I made both scripts to the best of my knowledge and they work just fine for me… The repeat until exists loops do a good job of keeping the timing correct and I have no idea how else you would do it. :confused:

One of the problems in this exchange M-S, is that the link you gave at imagesocket.com times out, so no one knows what your point was with it.

Well actually Adam, I managed to get the picture to load at some point and it’s just a partial screenshot of the top half of Camino where M-S is in about:config and has the sheet open for changing the integer i believe. Anyways, that’s exactly what my script edits so I don’t know what he’s complaining about. My script is certainly faster than any person is at modifying it…

I knew what he was talking about anyways as soon as he said about:config because I was aware of about:bookmarks and put two and two together before I ever even saw the picture.

EDIT: (To M-S) If your worried about loosing focus or it stuttering, you could always just throw in “activate aplication "Camino"” before every gui script command and lengthen delays :wink:

P.S. I find when gui scripting keystrokes, you don’t really need to worry too much about delaying because the keystrokes wait for the previous one to finish. So like for example, when it keystrokes “permissions.default.image” I only have a .2 delay until it starts the keystroke tab, but I know for a fact it takes the script longer than .2 seconds to keystroke “permissions.default.image” so it’s obviously waiting until it’s done before it delays .2 and moves on so the only place where a delay is really needed is the “repeat until exists” while the sheet is popping up in Camino which will effectively wait until the sheet is there before doing anything.

Keystrokes don’t need a delay between them as you point out. If you want the entire content of a page for example (in focus) keystroke “ac” using {command down} will do it. It doesn’t need to be two keystrokes. Same goes for entries as you’ve done.

Here’s my final code I’ve been using for awhile now.

display dialog "The state of pictures in Camino" buttons {"On", "Off"} default button "Off"
set {button returned:btn_pressed} to result

tell application "Camino"
	open url "about:config"
end tell

activate application "Camino"

if btn_pressed is "Off" then
	tell application "System Events"
		try
			keystroke "permissions.default.image"
			keystroke tab
			key code 125
			keystroke return
			repeat until exists sheet 1 of window 1 of process "Camino"
				delay 0.2
			end repeat
			keystroke "2"
			delay 0.2
			click button 1 of sheet 1 of window 1 of process "Camino"
		end try
	end tell
end if

if btn_pressed is "On" then
	tell application "System Events"
		try
			keystroke "permissions.default.image"
			keystroke tab
			key code 125
			keystroke return
			repeat until exists sheet 1 of window 1 of process "Camino"
				delay 0.2
			end repeat
			keystroke "1"
			delay 0.2
			click button 1 of sheet 1 of window 1 of process "Camino"
		end try
	end tell
end if

The only possible thing I can think of I would like to do is make it so you could just press “y” or “n” at the dialog, but I wasn’t sure how to set a user defined keystroke to a variable so I just said screw it since it wasn’t that important.

After a cache clear and quite some time since checking this thread, the image link still works for me. But just to play nice, I hosted it myself:

http://www.mikey-san.net/static/bucket/aboutconfig.png