How can I check if the Address Bar is visible in Safari?

Hi there,

Can anyone help me with this one please.

Is it possible to detect whether or not the Address Bar is visible in Safari?
Would some sort of ‘if exists’ statement work.

I’ve written a script for opening some windows and what I want to do is switch the Adress Bar off, but I need to check it’s visible before I do this.

Thanks in advance,

Nick

i was feeling real nice this morning

set AddessBarVisible to false
tell application "System Events"
	tell application process "Safari"
		tell window 1
			tell tool bar 1
				repeat with gr in every group
					tell gr
						try
							-- get value needed to throw try error
							get value of text field 1 of splitter group 1
							set AddessBarVisible to true
						end try
					end tell
				end repeat
			end tell
		end tell
	end tell
end tell
display dialog AddessBarVisible

Hi Kim,

Thanks for the help with this query, glad I caught you on a good day :slight_smile:

I’ll put it into my script and see if it does the trick.

Thanks again,

Nick

Hi Kim;

When I tried your script it refused to compile the line “repeat with gr in every group” under OS X 10.4.7 unless Safari was running, a window was showing, and the Address Bar was there. I got this to work for me:

tell application "System Events"
	tell application process "Safari"
		tell window 1
			if not (exists tool bar 1) then beep 3
		end tell
	end tell
end tell

Or, munching it down a bit: (bearing in mind that the answer is false if a Safari window is not frontmost)

set AB to true
tell application "System Events" to tell process "Safari" to tell window 1 to if not (exists tool bar 1) then set AB to false
AB

Even with UIBrowser at hand, I’m hopeless at UI scripting (clearly having missed an “aha” somewhere along the way), so I’ll be interested to see the solution for turning the tool bar off.

Perhaps something like this might do the trick:

activate application "Safari"
tell application "System Events" to tell application process "Safari"
	set w to not (exists front window)
	if w then click menu item "New Window" of menu "File" of menu bar item "File" of menu bar 1
	repeat until exists front window
		delay 0.1
	end repeat
	tell menu item "Hide Address Bar" of menu "View" of menu bar item "View" of menu bar 1 to if exists then click
	if w then click menu item "Close Window" of menu "File" of menu bar item "File" of menu bar 1
end tell

Thank you, Kai, although this last line “if w [not exists front window] then click menu item “Close Window” of menu “File” of menu bar item “File” of menu bar 1” confuses me.

That’s probably because, at that point, the boolean value represented by the variable w no longer means [not exists front window], Adam. :wink:

The routine takes account of the fact that, if no Safari window is currently open, the status of the menu item Show/Hide Address Bar is not a reliable indicator of whether the address bar will show when a new window is opened. So the script first checks for an open window:

set w to not (exists front window)

Then, if w [no front window exists], a window is opened temporarily - just to determine the accurate status of the menu item:

if w then click menu item "New Window" of menu "File" of menu bar item "File" of menu bar 1

Later, once the appropriate actions have been taken, the temporary window is no longer required - so it can be closed:

if w then click menu item "Close Window" of menu "File" of menu bar item "File" of menu bar 1

So what w really means here is: [no front window existed before the script opened one]. :slight_smile:

Merci, beaucoup. It didn’t seem to work that way, but I’ll test some more tomorrow. Now I’m going to enjoy a Bass Ale.

It certainly works as described here, Adam. Since you seem to be questioning either what the script does or the above description, could you perhaps enlarge on that statement?

I’m sorry I didn’t confirm that it works perfectly, Kai. No problem at all.