Sublime Forum

Open http / www files at once?

#1

Is it possible to open dozens or hundreds of http or www links from sublime in the browser at the same time ?

I’m slowly beginning to like sublime, a slow start to using the program :slightly_smiling:

0 Likes

#2

Since you can write arbitrary python code in the ST console, it is pretty easy:

  1. select all links you want to open, e.g.
    i. open the search panel (ctrl+f)
    ii. enable regex search (alt+r)
    iii. search for (https?://|www\.)\S+
    iv. find all (alt+enter)
  2. open the ST console (ctrl+`)
  3. paste import webbrowser; [webbrowser.open(view.substr(sel)) for sel in view.sel()] and execute
2 Likes

#3

That worked except it went almost in a infinite loop, I had to force my browser to close. Can I open from top to bottom of the document, ten http links at once, when I’m ready, open another 10 links etc, etc ?

0 Likes

#4

Yes, this is also possible. For example you could use an generator.

First create the generator:

import webbrowser; gen = (webbrowser.open(link) for link in list(view.substr(sel) for sel in view.sel()))

The generator is now in the variable gen and the links will be opened, when fetching elements.

To open a single link:

next(gen)

To open 10 links:

list(next(gen) for _ in range(10))
2 Likes

#5

Depending where I am based on the mini-map when running the command; next(gen). The first link from the top doesn’t open ?

0 Likes

#6

I don’t understand your question, you still need to select all links (1.). Afterwards it will just iterate over all selections from the top to the bottom.
If you want to get more insights you can change the generator to also return the link:

import webbrowser; gen = (("opening '%s'" % link, webbrowser.open(link)) for link in list(view.substr(sel) for sel in view.sel()))
0 Likes

#7

The RegEx is including, not the opening quotation, rather the closing quotation and comma in the file which I want to open the links.

0 Likes

#8

Install package Open-Include
Select all
ALT+D

1 Like

#9

Currently the regex matches everything until the first whitepace character, you can change it to match anything until the first whitespace, comma, quote, brace, …

Just change it to this: (https?://|www\.)[^\s,"'\}]+

0 Likes

#10

This is almost working, the JSON file has a string which contains favorite icons and that too is a http file, how can I exclude this from the selection ?

0 Likes

#11

To exclude string ending with .ico you could either add a negative look-ahead to the regex:
(https?://|www\.)(?!\S+\.ico\b)[^\s,"'\}]+
or change the generator to filter out links, which ends with .ico

import webbrowser; gen = (("opening '%s'" % link, webbrowser.open(link)) for link in list(view.substr(sel) for sel in view.sel()) if not link.endswith(".ico"))
0 Likes

#12

Thank you, everything is working great; what is the command to open all the http links at the same time when fetching links after the generator ?

RegEx powerful stuff; what is the purpose of a generator ?

0 Likes

#13

You can just turn the generator into a list:

list(gen)

We created list/generator elements as the result of webbrowser.open(link), because of the desired side effect of opening the link.

Using a list all list elements will directly created and hence all links open at the same time.

Generators use lazy evaluation to retrieve each element only when it is requested, hence each link was only opened, when accessing the element. We just abused the lazy evaluation to get a lazy side-effect. In general generators are used to improve performance, because they require less memory space, are usually faster, and you can cancel them early without creating all elements.

2 Likes

#14

Can it stop loading the pages when it reaches the end of the document; rather then a continuous loop as does list(gen) ?

0 Likes

#15

The generator only goes one time from top to bottom of the document. So list(gen) won’t go into an infinite loop, but open all remaining links. Your JSON just might contain more links, than you thought.

0 Likes

#16

That is originally what I thought as well, although when watching the links open in the browser, I noticed the same links opening two or three times, that’s where I though it must be looping.

0 Likes

#17

You can replace list(view.substr(sel) for sel in view.sel()) with set(view.substr(sel) for sel in view.sel()), which will remove all duplicates before opening the links. It will mess up the order they appear in though.

0 Likes

#18

I prefer to keep the order :slightly_smiling:

0 Likes

#19

You can remove duplicates and preserve order using list(collections.OrderedDict.fromkeys(your_list).keys()).

This would result in the generator:

import collections, webbrowser; gen = (("opening '%s'" % link, webbrowser.open(link)) for link in list(collections.OrderedDict.fromkeys(view.substr(sel) for sel in view.sel()).keys()))
0 Likes

#20

I’m getting an error with the generator;

AttributeError: ‘module’ object has no attribute ‘OrderedDict’

0 Likes