Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

duckdealer

macrumors newbie
Original poster
Apr 7, 2014
1
0
Hi I'm currently trying my best to learn and write a simple applescript. I basically want to open firefox with two new tabs, this bit I have managed but the part I am finding tricky is I want there to be a variable that if Firefox is already open then two new tabs are opened in the current window without relaunching the whole program again.

Is Firefox open?
Yes - two new tabs
No - launch Firefox, open two new tabs

I'm sure it's simple but I could really use some help.

Thanks
 

kryten2

macrumors 65816
Mar 17, 2012
1,114
99
Belgium
Hi I'm currently trying my best to learn and write a simple applescript. I basically want to open firefox with two new tabs, this bit I have managed but the part I am finding tricky is I want there to be a variable that if Firefox is already open then two new tabs are opened in the current window without relaunching the whole program again.

Is Firefox open?
Yes - two new tabs
No - launch Firefox, open two new tabs

I'm sure it's simple but I could really use some help.

Thanks

Couple of ways of doing this. You can check if Firefox is running or you can count the windows of Firefox.

Examples :

Code:
tell application "System Events"
	if (exists process "Terminal") then
		display dialog "Terminal is running"
		-- Make two new tabs in window 1 or current window
	else
		--  Activate Firefox
		tell application "Terminal" to activate
		-- Open two new tabs
	end if
end tell

Code:
tell application "Safari"
		if (count of windows) is 1 then
		make new document
	end if
end tell

Note : The examples are with Terminal and Safari but it would get you started. I don't have/use Firefox so I couldn't look into the dictionary of the application if it supports current window. Normally all windows have an index property so you can address them like window 1 window 2 etc.
 
Last edited:

kaibob

macrumors regular
Jun 21, 2010
236
67
Prescott, Arizona
Is Firefox open?
Yes - two new tabs
No - launch Firefox, open two new tabs

I think a third option is to use the reopen command, which is described as follows:

Many applications also support the reopen command, which reactivates a running application or launches it if it isn’t running. If the application is already running, this command has the same effect as double-clicking the application icon in the Finder. Each application determines how it will implement the reopen command—some may perform their usual startup procedures, such as opening a new window, while others perform no additional operations.

I haven't tried this command with Firefox, but it seems to work with most applications. It's easy to check with the following:


Code:
tell application "Firefox"
   reopen
   --commands to open new tabs
   activate
end tell
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.