Jump to content
JvdL

Open Firefox Window with several sites opened

Recommended Posts

Hello,

I searched this forum and also the web to find a workflow that can be triggered by a hotkey that opens a new Firefox window with several sites in several tabs opened at once.

All the scripts or snippets I found where buggy or not working at all. What would be the right approach to do that.

 

E.g. I would like to press a hotkey that opens:

 

1. A new Firefox window

2. First tab "www.nba.com"

3. Second tab "www.sbnation.com"

 

I would appreciate your comments

Jürgen

Share this post


Link to post

What I have till now is this:

 

delay 1

tell application "System Events"
    
    keystroke "n" using {shift down, command down}

    delay 5

    keystroke "l" using {command down}
    keystroke "https://www.nba.com" & return
    delay 1

    keystroke "t" using {command down} 
    keystroke "l" using {command down} 
    keystroke "https://www.wikipedia.org" & return
    delay 1

    keystroke "t" using {command down} 
    keystroke "l" using {command down} 
    keystroke "https://www.cnn.com" & return
    delay 1

end tell

end alfred_script

 

But it does not work consistently . Sometimes it works as it should be, sometimes it just adds additional tabs to the Firefox Window in focus, sometimes it opens a new window, opening the previous set of tabs, ...

 

Is this a problem with the script of with Firefox?

 

Thanks

Jürgen

Share this post


Link to post

Hi @JvdL and welcome to the forum :)

 

There's a good built-in workflow you can use as example to teach you how to do what you describe; Open Alfred's preferences to the Workflows tab, and click the [+] button at the bottom of the sidebar. In Examples, choose "Should I watch this movie?"

 

This workflow shows you how to use a keyword to launch three websites (YouTube, IMDB and Rotten Tomatoes).

 

movie-workflow-example.png

 

Now you can create your Blank Workflow to create your own in a similar way; Instead of a keyword, you'll start your workflow with a hotkey, and connect it to two "Open URL" objects. Set the URL for each site you listed above, and choose Firefox as the browser for each one.

 

Let me know if you need any further help :)

 

Cheers,
Vero

Share this post


Link to post

Thanks Vero, I will try that, too.

 

I found a bug in my script and now it seems to work:

 

on alfred_script(q)

tell application "Firefox" to activate

    delay 1

tell application "System Events"
    
    keystroke "n" using {command down}

    delay 2 -- shorter delay causes the second key stroke to end up in the search

    keystroke "l" using {command down}
    keystroke "https://www.nba.com" & return
    delay 1

    keystroke "t" using {command down} 
    keystroke "l" using {command down} 
    keystroke "https://www.wikipedia.org" & return
    delay 1

    keystroke "t" using {command down} 
    keystroke "l" using {command down} 
    keystroke "https://www.cnn.com" & return
    delay 1

end tell

end alfred_script

 

The keystroke for the new window was wrong.

 

Thanks again

Jürgen

 

Edited by JvdL

Share this post


Link to post

@JvdL As much as possible, avoid simulating keystrokes as this isn't a reliable way to perform actions.

 

Wherever possible, your best bet is to use Alfred's built-in objects to do the majority of what you want, and use AppleScript or another appropriate solution where scripting is required and it can't be done using objects. Simulating keystrokes should be a last resort, as it can be really quirky.

Share this post


Link to post

Hello Vero,

I checked the workflow you mentioned, but it does open three sites in three different windows.

I would like to open three sites in different tabs ...

 

Is that possible?

Share this post


Link to post

@JvdL Take a look at Firefox's own preferences, as this behaviour is set by the app opening the URLs and not by Alfred.

 

E.g. in Safari, it's in the Tabs preferences:

 

Screen Shot 2018-01-24 at 17.42.02.png

 

Cheers,
Vero

Share this post


Link to post

I'm with @Vero about avoiding simulated keystrokes and delay using AppleScript, but in the case of Firefox and interacting with different window and tab, it is hard to get around since there's no applescript dictionary to open a new window or a new tab directly with Firefox. The things that is more reliable is to set the Firefox preference "Open links in tabs instead of new windows"  to what you prefer and then it will open links how this is defined (so in your case, since you say that it opens the URL in new window with the Alfred action, then I suppose it is not ticked and set to open in a new window). So, what I recommend is to check this checkbox in the Firefox preferences (Preferences -> Tabs -> [ ] Open links in tabs instead of new windows) and then press Command+N  before running a workflow like the one from @Vero or try this script:

 

In this one, I try to limit the bad behaviour of delay and keystrokes by checking if the action was done and if not repeat and check again. So, this should work quite reliably.

set urlList to {"https://www.nba.com", "https://www.wikipedia.org", "https://www.cnn.com"}

tell application "Firefox"
	activate
	
	repeat 40 times -- gives about 4 seconds to open Firefox if not opened
		if frontmost then exit repeat
		delay 0.1
	end repeat
	
	tell application "System Events" to tell application process "Firefox"
		set oldNbrOfWindow to (count of windows)
		keystroke "n" using command down
		repeat 20 times
			if ((count of windows) > oldNbrOfWindow) then exit repeat
			delay 0.1
		end repeat
	end tell
	
	repeat with the_url in urlList
		open location the_url
	end repeat
	
	-- close the first default tab
	tell application "System Events"
		keystroke tab using control down
		delay 0.2
		keystroke "w" using command down
	end tell
end tell

 

Edited by GuiB

Share this post


Link to post

Hello Guib,

I checked the Firefox Preferences and "Open new windows in a new tab instead" is checked. But the template workflow opens several windows anyway.

But in general I agree with you both reg. keystrokes and delays. I will try your script now.

 

Thanks for the quick answers!

Jürgen

Share this post


Link to post

Hi GuiB,

 

I tried using your script replacing the three websites the original poster requested with the two websites I would like Firefox to open at the same time in one window (in two separate tabs). I am a complete newbie to applescript, so I do not completely understand what it is doing yet, however, it is successfully opening the two websites I want it to open in two separate tabs. However, there is an unwanted behavior of it opening a second blank firefox window in addition to the two tabs. So, I am wondering if there is a way to modify the script so that it does not open the extra firefox window. I'm wondering if this is happening because the original script was for three websites and I only need two? Thanks for reading and for your consideration. Here is the script I am running, based on the one you wrote.

 

Thanks,

Chris

 

set urlList to {"https://voice.google.com", "https://riot.im/app/"}

tell application "Firefox"
    activate
    
    repeat 40 times -- gives about 4 seconds to open Firefox if not opened
        if frontmost then exit repeat
        delay 0.1
    end repeat
    
    tell application "System Events" to tell application process "Firefox"
        set oldNbrOfWindow to (count of windows)
        keystroke "n" using command down
        repeat 20 times
            if ((count of windows) > oldNbrOfWindow) then exit repeat
            delay 0.1
        end repeat
    end tell
    
    repeat with the_url in urlList
        open location the_url
    end repeat
    
    -- close the first default tab
    tell application "System Events"
        keystroke tab using control down
        delay 0.2
        keystroke "w" using command down
    end tell
end tell

Share this post


Link to post
5 hours ago, cebroski said:

However, there is an unwanted behavior of it opening a second blank firefox window in addition to the two tabs.

 

Perhaps I've misunderstood your problem. The script is designed to open the URLs in tabs in a new window.

 

If you want to open the URLs in an existing window, delete this section of the script:

tell application "System Events" to tell application process "Firefox"
    set oldNbrOfWindow to (count of windows)
    keystroke "n" using command down
    repeat 20 times
        if ((count of windows) > oldNbrOfWindow) then exit repeat
        delay 0.1
    end repeat
end tell
Edited by deanishe

Share this post


Link to post

@cebroski in addition to what @deanishe said, I would recommand to remove also the last part of the scipt so it doesn't close an already opened tab.

 

I mean, remove:

-- close the first default tab
tell application "System Events"
	keystroke tab using control down
	delay 0.2
	keystroke "w" using command down
end tell

 

However, if I understand well what you want, I think it would be better/easier/faster to use the Alfred's builtin 'Open URL' object. The idea is to have a Keyword object (or another trigger) that connect to 2 'Open URL' objects that send your specific URLs to Firefox.

 

Here is an example workflow that should do what you want: https://d.pr/f/jsvmIY

Make sure to check the "Open links in tabs instead of new windows" in the Firefox Preferences.

Share this post


Link to post

@KA_, you can use a "Random" node that outputs a number in the range of 1 to the number of URLs that you have and then you use a "Conditional" node to select the "Open URL" node that is linked to the selected random number.

 

Here is an example: https://d.pr/f/yKRv5w

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...