Firefox GUI menu bar script sometimes fails

Part of a script I’m trying to write hides the Bookmark Bar in Firefox, but the trouble is sometimes it works, sometimes it fails.

This is the original version I wrote:

tell application "Firefox" to activate
(* Hide Bookmark Bar *)
tell application "System Events"

	try
		click menu item "Never Show" of menu "Bookmarks Toolbar" of menu item "Bookmarks Toolbar" of menu "Toolbars" of menu item "Toolbars" of menu "View" of menu bar item "View" of menu bar 1 of process "Firefox"
	end try
	
end tell

As it failed sometimes I tweaked it slightly:

tell application "Firefox" to activate
(* Hide Bookmark Bar *)
tell application "System Events"
	
	tell menu "Bookmarks Toolbar" of menu item "Bookmarks Toolbar" of menu "Toolbars" of menu item "Toolbars" of menu "View" of menu bar item "View" of menu bar 1 of process "Firefox"
		
		if exists menu item "Never Show" then
			tell menu item "Never Show"
				click it
			end tell
		end if
		
	end tell
end tell

When running this version it sometimes shows that the “Never Show” menu bar item is false (doesn’t exist).

If I keep running the script a few times the “Never Show” menu bar item ‘becomes available’ again, and will work.

Does anyone know why it sometimes works, but other times fails?

Waiting for each menu to appear is the same as waiting for windows to appear. Because they don’t show up right away. Therefore, for stable operation, you should check the appearance of each menu and submenu.

No need checking for menu items and submenu items, because they are not windows, like menus itself.

I don’t have Firefox so I can’t test the following script. But, in theory, it should work stably:


tell application "Firefox" to activate

(* Hide Bookmark Bar *)
tell application "System Events" to tell process "Firefox" to tell menu bar 1
	tell menu bar item "View"
		click it
		repeat until menu "View" exists -- wait for menu "View"
			delay 0.1
		end repeat
		tell menu item "Toolbars" of menu "View"
			click it
			repeat until menu "Toolbars" exists -- wait for menu "Toolbars"
				delay 0.1
			end repeat
			tell menu item "Bookmarks Toolbar" of menu "Toolbars" -- EDITED
				click it
				repeat until menu "Bookmarks Toolbar" exists -- wait for menu "Bookmarks Toolbar"
					delay 0.1
				end repeat
				tell menu "Bookmarks Toolbar" of menu item "Bookmarks Toolbar"
					click menu item "Never Show"
				end tell
			end tell
		end tell
	end tell
end tell

Thanks a lot. I kept getting errors at the part

		tell menu item "Bookmarks Toolbar"
				click it

But for some strange reason my script has started working OK now :smiley:

tell application "Firefox" to activate

(* Hide Bookmark Bar *)
tell application "System Events"
	
	tell menu "Bookmarks Toolbar" of menu item "Bookmarks Toolbar" of menu "Toolbars" of menu item "Toolbars" of menu "View" of menu bar item "View" of menu bar 1 of process "Firefox"
		
		try
			tell menu item "Never Show"
				click it
			end tell
		end try
		
	end tell
end tell

It was one mistake. I edited the script.

Ah, great. Thanks again!