Commanding Firefox to close tabs by AppleScript

Hi all!

Just wondering if it's possible at all: I'd like to write an AppleScript that would let me close all tabs except my homepage in Firefox when I quit the application.

I'm not sure about the syntax or the commands I can invoke in AppleScript, because I know Firefox's AppleScripting abilities are quite poor.

Do you think it's possible at all? Feel free to ask for more information if that I provide is not sufficient... 😉

MBP Core Duo 1,83GHz 15", Mac OS X (10.4.11), Me love you cookies lo-oong time!

Posted on Jun 18, 2009 6:28 AM

Reply
8 replies

Jun 19, 2009 4:39 AM in response to OneCatFamily


on run

my activateGUIscripting()

tell application "Firefox"
activate
end tell
tell application "System Events" to tell process "Firefox"
tell menu bar item 9 of menu bar 1 to tell menu 1
set onglets to (get name of every menu item)
set nbItems to count of onglets
if nbItems > 4 then
repeat with i from nbItems to 5 by -1
set wName to item i of onglets
click menu item wName
my shortcut("Firefox", "w", "c")
end repeat
end if
end tell
my shortcut("Firefox", "q", "c")
end tell
end run
--=====
on activateGUIscripting()
tell application "System Events"
if not (UI elements enabled) then set (UI elements enabled) to true (* to be sure than GUI scripting will be active *)
end tell
end activateGUIscripting
--=====
(*
==== Uses GUIscripting ====
*)
on shortcut(a, t, d)
local k
tell application a to activate
tell application "System Events" to tell process a
repeat with k in t
if d = "c" then
keystroke (k as text) using {command down}
else if d contains "c" then
if d contains "s" then
if d contains "k" then
keystroke (k as text) using {command down, shift down, control down}
else
keystroke (k as text) using {command down, shift down}
end if
else if d contains "k" then
keystroke (k as text) using {command down, control down}
end if
end if
end repeat
end tell
end shortcut
--=====


Yvan KOENIG (from FRANCE vendredi 19 juin 2009 13:38:58)

Jun 19, 2009 11:38 AM in response to OneCatFamily

KOENIG Yvan has an interesting solution.

---------
rccharles, you think it can be done without the help of a third-party software or not?


I know what your were asking was ( likely ) possible, but didn't know how to do what you requested.

UI Browser makes figuring out what to code easier, but there is a free apple tool.

You might like iKey/youpi key. I have my frequently used applications set to f keys. The same f key starts an application and switches to the application.

You may like youpi key. It is free. It worked for me in MAC OS 10.4 although not officially supported. There is a commercial version you can buy, called iKey.

http://www.versiontracker.com/dyn/moreinfo/macosx/11485&vid=75326

Here is my script for listing my application folder. I have it assigned to function-key 6.

tell application "Finder"
activate
select window of desktop
make new Finder window to startup disk
select Finder window 1
set target of Finder window 1 to folder "Applications" of startup disk
select Finder window 1
set position of Finder window 1 to {60, 45}
end tell

This script was automatically generated in script editor record mode. After I recorded the script and did some minor editing, I copy the script to youpi key.

I have all of my frequently used applications set to function keys.

---------------


"iKey is an automation utility, a program that creates shortcuts to accomplish repetitive tasks. In essence, an iKey shortcut is a little program in its own right, but you don't need to know the first thing about programming to create an iKey shortcut. All you have to do is put together three necessary parts of a shortcut: One or more commands that give the shortcut its functionality, a context in which it runs, and a launcher that defines how the shortcut is activated."
http://www.scriptsoftware.com/ikey/

Robert

Jun 20, 2009 4:58 AM in response to Noaks

If I understand well, your proposal is;

speak in Safari language to an application which knows only Firefox language.

The result will be:
I don't understand what you are asking me.

I'm not fond of GUIscripting.
If I used it, it's because Firefox's AppleScript support is restricted to a really short vocabulary.

Yvan KOENIG (from FRANCE samedi 20 juin 2009 13:58:13)

Jun 20, 2009 11:34 AM in response to KOENIG Yvan

I understand what you mean, but I'm not fluent in XUL, far from it... 😟

I've asked a few questions on MozillaZine, but the guys out there are so self-centered and elitist that asking a question like this on their forum would have me excommunicated... Again...

So, in a nutshell, do you think it can be done or not. I'm asking this because, contrarily to Opera, when you use Firefox as default and launch it by clicking on a link in your email client (Thunderbird in my case), the link you've clicked on overwrites your homepage, and I don't like it; in Opera, the link opens a new tab in the foreground, with my homepage in the background and to the left. It makes more sense to me that way, because I often go to my homepage (Google News), while after reading the page the link in Thunderbird sent me to, I usually close it right away.

Again, implying that Firefox's behavior is somewhat faulty in this respect (it's a mere trait, after all) would send me to the stake on MozillaZine, so I thought I'd look for answers here since I'm on a Mac and still trying to get my hands around AppleScript, but I caught your drift: Firefox doesn't speak AppleScript, so aside from GUI-scripting my way until my fingers fall off my hands, I'm in a hopeless situation... 😟

Message was edited by: Didier Caizergues

Jun 20, 2009 1:08 PM in response to OneCatFamily

I don't know what is XUL.

(1) my late message was not for you. The addressee was Noaks whose own response was odd !

(2) Firefox is a very good program but its AppleScript support is really short.

(3) I gave you a script which uses GUIscripting and closes the windows minus the HomePage one.

Upon the tabs, the French Firefox Help states:

+Fermeture et restauration des onglets+

+Pour fermer l'onglet courant, appuyez sur Ctrl Cmd+ W, cliquez sur le bouton de fermeture d'onglet , cliquez avec le bouton central de la souris sur l'onglet ou sélectionnez Fichier > Fermer l'onglet. Pour fermer tous les autres onglets sauf l'onglet actif, cliquez sur l'onglet avec le bouton droit de la souris cliquez sur l'onglet en appuyant sur Ctrl et choisissez Fermer les autres onglets.+

I may code that but as there is no way to grab the localized name of the item "starting page" I am forced to build what I hate:
s script dedicated to a single language.

Here it is:


--=====
on run
my activateGUIscripting()
(*
your code
*)
my fermeOnglets()
my shortcut("Firefox", "q", "c") (* Quit the app *)

end run
--=====
on fermeOnglets()
repeat 20 times
set {nbItems, windowName} to my getNomOnglet()
if (nbItems > 3) and windowName is not "Page de démarrage Mozilla Firefox" then
my shortcut("Firefox", "w", "c") (* Close the active tab *)
else
exit repeat
end if
end repeat
end fermeOnglets
--=====
on getNomOnglet()
set theApp to "Firefox"
tell application theApp
activate
tell application "System Events" to tell process theApp to tell menu bar 1
tell menu bar item 9 to tell menu 1
set bb to name of every menu item
end tell -- to menu bar item mt
end tell -- application System Events & process theApp & menu bar 1
end tell -- application theApp

return {count of bb, item -1 of bb}
end getNomOnglet
--=====
on activateGUIscripting()
tell application "System Events"
if not (UI elements enabled) then set (UI elements enabled) to true (* to be sure than GUI scripting will be active *)
end tell
end activateGUIscripting
--=====
on shortcut(a, t, d)
local k
tell application a to activate
tell application "System Events" to tell process a
repeat with k in t
if d = "c" then
keystroke (k as text) using {command down}
else if d contains "c" then
if d contains "s" then
if d contains "k" then
keystroke (k as text) using {command down, shift down, control down}
else
keystroke (k as text) using {command down, shift down}
end if
else if d contains "k" then
keystroke (k as text) using {command down, control down}
end if
end if
end repeat
end tell
end shortcut
--=====


Yvan KOENIG (from FRANCE samedi 20 juin 2009 22:08:44)

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.

Commanding Firefox to close tabs by AppleScript

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