Move to a tab in Firefox

The below script will open Google News in a new tab. Now if I want to do the inverse, the script will continue to open new tabs. But I just want to go back and forth between the two. I do not really care how this is accomplished. I could close the old tab, when the new one is create, or just move to the new tab. I just do not want to open a whole bunch of new tabs every time this script runs. Make sense?

Thank you in advance.


tell application "Firefox" to set theURL to «class curl» of window 1
if theURL is "http://www.google.com/" then
tell application "Firefox" to Get URL "http://news.google.com"
end if
end

Mac Pro/iBook G4, Mac OS X (10.4.7)

Posted on Dec 21, 2006 9:28 AM

Reply
2 replies

Dec 31, 2006 3:23 PM in response to Redshark

Redshark,

I am not sure I fully understand all of what you request but I have been looking at a possible solution to your inquiry.

The AppleScript dictionary of Firefox will not allow you to do what you want as it has no properties or commands related to tabs.

It is also difficult to get that information directly through probing the application using System Events GUI scripting.

Therefore you can not determine anything about the tabs such as their number, titles, size, location and the solution becomes rather involved and somewhat rough.

Using some of the other capabilities of System Events it is possible to piece together a rough solution that will count the number of tabs, move between tabs, and eliminate specified duplicates/empty tabs.

Without knowing the number of tabs it is difficult to provide a solution. There are at least two ways of determining the number of tabs.
1) Ask the user initially and then keep track;
2) Set a marker at the end of the tabs.

I have arbitrarily set a marker tab, which in my case is an empty tab i.e., no URL, at the end of all the other tabs so that I can determine the number of tabs.
NOTE: This means that there should not be any empty tabs in the middle.

The testing is based on the name of the window, determined using AppleScript, which reflects the name of the tab.

The program does the following:
1) checks for a marker tab at the end of the tabs and creates one if it is missing;
2) eliminates all of the duplicated tabs;
2) opens the Google News in a new tab;
3) creates a new marker tab at the end of the tabs.

click here to open this script in your editor <pre style="font-family: 'Monaco', 'Courier New', Courier, monospace; overflow:auto; color: #222; background: #DDD; padding: 0.2em; font-size: 10px; width:400px">property theTabNames : {}
property WantedURL : "http://news.google.com"
property MarkerTabName : "Mozilla FireFox"
property DuplicateNames : {"Google News", MarkerTabName}

on run {}
my SetMarkerTab()
-- Remove duplicate names
repeat with i from 1 to count of items in DuplicateNames
set theTabNames to my DetermineNumberOfTabs(MarkerTabName)
my EliminateTabs(theTabNames, item i of DuplicateNames)
end repeat
-- Open tab with wanted URL
tell application "Firefox"
Get URL WantedURL
end tell
-- Reset the marker
my CreateMarkerTab()
end run

on SetMarkerTab()
tell application "System Events"
tell process "firefox-bin"
set frontmost to true
keystroke "9" using command down -- move to last tab
if (name of window 1) is not MarkerTabName then my CreateMarkerTab()
end tell
end tell
end SetMarkerTab

on DetermineNumberOfTabs(MarkerName)
tell application "System Events"
tell process "firefox-bin"
set frontmost to true
set i to 1
set theNames to {}
set LastTabName to ""
repeat until LastTabName is equal to MarkerName
keystroke (i as string) using command down -- move to a tab
set LastTabName to name of window 1
set theNames to theNames & LastTabName
set i to i + 1
end repeat
end tell
end tell
return theNames
end DetermineNumberOfTabs

on EliminateTabs(TabNames, DuplicateName)
tell application "System Events"
tell process "firefox-bin"
set frontmost to true
repeat with i from (count of TabNames) to 1 by -1
if (item i of TabNames) is equal to DuplicateName then
keystroke (i as string) using command down -- move to a tab
keystroke "w" using command down -- close a tab within a window
end if
end repeat
end tell
end tell
end EliminateTabs

on CreateMarkerTab()
tell application "System Events"
tell process "firefox-bin"
set frontmost to true
keystroke "t" using command down -- new tab
end tell
end tell
end CreateMarkerTab
</pre>



PowerBook 12" Mac OS X (10.4.8)

Jan 2, 2007 6:14 AM in response to lc55

Thank you lc55,

I edited down your script to get just what I needed.

Thank you for your time, and Happy New Year!

tell application "Firefox" to set theURL to «class curl» of window 1
if theURL is "http://www.google.com/" then
tell application "Firefox" to Get URL "http://news.google.com/"
else
if theURL is "http://news.google.com/" then
tell application "System Events"
tell process "firefox-bin"
set frontmost to true
keystroke "w" using command down -- close a tab within a window
end tell
end tell
end if
end if

Mac Pro/iBook G4 Mac OS X (10.4.7)

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Move to a tab in Firefox

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.