Help needed for simple Applescript

Discussion in 'Mac Programming' started by duckdealer, Apr 7, 2014.

  1. duckdealer macrumors newbie

    Apr 7, 2014
    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.

  2. kryten2, Apr 7, 2014
    Last edited: Apr 7, 2014

    kryten2 macrumors 6502a

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

    Examples :

    tell application "System Events"
    	if (exists process "Terminal") then
    		display dialog "Terminal is running"
    		-- Make two new tabs in window 1 or current window
    		--  Activate Firefox
    		tell application "Terminal" to activate
    		-- Open two new tabs
    	end if
    end tell
    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.
  3. kaibob, Apr 7, 2014
    Last edited: Apr 7, 2014

    kaibob macrumors regular

    Jun 21, 2010
    Prescott, Arizona
    I think a third option is to use the reopen command, which is described as follows:

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

    tell application "Firefox"
       --commands to open new tabs
    end tell

Share This Page