3

While using Google Chrome I often drag website links from the address bar into a folder on the desktop for later reference. This creates a .desktop file that is essentially a link to a webpage.

These desktop files can be opened easily from Nautilus & PCmanFM by simply by double-clicking to open them, however, when I open them using the SpaceFM file manager, Google Chrome starts downloading the file instead of opening it like it does with the other mentioned file managers.

I am able to open these files through SpaceFM if I right click them and open with Firefox, but not Google Chrome.

An example of the desktop file would be as follows:

[Desktop Entry]
Encoding=UTF-8
Name=Link to The Hidden Fortress (1958) - IMDb
Type=Link
URL=http://www.imdb.com/title/tt0051808/?ref_=nv_sr_2
Icon=text-html

Is there a way to make these .desktop files open in Google Chrome normally when launched from the SpaceFM browser?

  • can anyone please help ? – Kalamalka Kid Apr 20 '16 at 7:12
  • ha! nice of you to come to the rescue @JacobVlijm. The links are always dragged from CHrome browser to a folder on my desktop, then moved to other folders. – Kalamalka Kid May 6 '16 at 7:53
  • observation * if you drag & drop a .dektop file from any file manager into chrome it will download the file instead of opening the link, so it seems as though SpaceFM is treating the action as though it were Drag & Drop. – Kalamalka Kid May 6 '16 at 8:06
1
+50

1. Convert link files into launchers

The solution below offers the option to right- click the link (.desktop file) and choose > "Execute", which will run Google-Chrome to open the link. By editing SpaceFm's settings, you can run the link by double-click as well (see notes [3]).

enter image description here

Note that the solution automatically edits (only) new links on your desktop, to run the link specifically with Google-Chrome

What it is

A small background script checks once per two seconds for new .desktop files on your Desktop. If a relevant file is found, the file is edited from a Link file into an Application file. This is done by editing two lines inside the file:

  • The line:

    Type=Link
    

    is changed into:

    Type=Application
    
  • The line:

    URL=<link>
    

    is changed into:

    Exec=/usr/bin/google-chrome-stable <link>
    

In the test(s) I ran, this makes the link "openable" by right- click in SpaceFm: right-click > Open > Execute

The script

#!/usr/bin/env python3
import os
import time

# --- define the (absolute) path to your desktop below
dr = "/absolute/path/to/your/desktop"
# edit (if necessary) the command to launch Google-Chrome
application = "/usr/bin/google-chrome-stable"

def find_relevant():
    return [f for f in os.listdir(dr) if f.endswith(".desktop")]

relevant1 = []

while True:
    time.sleep(2)
    relevant2 = [f for f in os.listdir(dr) if f.endswith(".desktop")]
    new = [f for f in relevant2 if not f in relevant1]
    if new:
        for f in new:
            f = dr+"/"+f
            rewrite = False
            lines = [l.strip() for l in open(f).readlines()]
            for i, l in enumerate(lines):
                if l.startswith("Type=Link"):
                    rewrite = True
                    lines[i] = "Type=Application"
                elif l.startswith("URL="):
                    lines[i] = l.replace("URL=", "Exec="+application+" ")
            if rewrite == True:
                print("rewrite")
                open(f, "wt").write(("\n").join(lines))            
    relevant1 = relevant2

How to use

  1. Copy the script into an empty file, save it as edit_links.py
  2. In the head- section of your script, edit the path to your Desktop (as mentioned, use absolute path here)
  3. Check the command to run Google-Chrome (also set in the head- section) by checking the first Exec= line in the google-chrome.desktop file: run

    gedit /usr/share/applications/google-chrome.desktop
    

    to read the file.

  4. Test-run the script by the command:

    python3 /path/to/edit_links.py
    

    Open Google-Chrome, drag links on to your Desktop, test after a few seconds if it works fine.

  5. If all works fine, add it to Startup Applications: Dash > Startup Applications > Add. Add the command:

    python3 /path/to/edit_links.py
    

Note

  1. The dragged .desktop file (link) needs to be on your Desktop at least one-two seconds to be found and edited, so if you move the links, at least leave them for a few seconds on your Desktop. :)
  2. ll the script does is check for new .desktop files on your Desktop, it only reads/edits the file(s) if there are new files. This means nothing to your system.
  3. By editing SpaceFm's settings, you can alsu run the "link" by double-click:

    enter image description here

Convert your existing link files recursively

Additionally, as discussed in chat, a script to run a single time to convert the links in a directory recursively:

#!/usr/bin/env python3
import os
import sys

# --- define the (absolute) path to your desktop below
dr = sys.argv[1]
# edit (if necessary) the command to launch Google-Chrome
application = "/usr/bin/google-chrome-stable"

for root, dirs, files in os.walk(dr):
    for f in files:
        if f.endswith(".desktop"):
            f = root+"/"+f
            rewrite = False
            lines = [l.strip() for l in open(f).readlines()]
            for i, l in enumerate(lines):
                if l.startswith("Type=Link"):
                    rewrite = True
                    lines[i] = "Type=Application"
                elif l.startswith("URL="):
                    lines[i] = l.replace("URL=", "Exec="+application+" ")
            if rewrite == True:
                open(f, "wt").write(("\n").join(lines)) 

To use it, save it as convert_links.py, run it with the targeted directory as an argument:

python3 /path/to/convert_links.py <directory>

2. Convert links into cross-platform usable link files

As requested by OP, below a version of the (first) (background) script, converting the link files, created by dragging a link from the browser to the desktop, into cross-platform links. Usage is exactly as explained in the first section.

The script

#!/usr/bin/env python3
import os
import time

# --- define the (absolute) path to your desktop below
dr = "/absolute/path/to/your/desktop"

out1 = ["<html>", "<body>", '<script type="text/javascript">']
out2 = ["</script>", "</body>", "</html>"]

def find_relevant():
    return [f for f in os.listdir(dr) if f.endswith(".desktop")]

relevant1 = []

while True:
    time.sleep(2)
    relevant2 = [f for f in os.listdir(dr) if f.endswith(".desktop")]
    new = [f for f in relevant2 if not f in relevant1]
    if new:
        for f in new:
            f = dr+"/"+f
            rewrite = False
            lines = [l.strip() for l in open(f).readlines()]
            for i, l in enumerate(lines):
                if l.startswith("Type=Link"):
                    rewrite = True
                elif l.startswith("URL="): 
                    url = 'window.location.href = "'+l.replace("URL=", "")+'"'
                    out1.append(url)
                elif l.startswith("Name="):
                    name = l.replace("Name=", "")
            if rewrite == True:
                open(f.replace(".desktop", ".html"), "wt").write(("\n").join(out1+out2))
                os.remove(f)
    relevant1 = relevant2

Version to convert your existing links in a recursive directory (single run)

#!/usr/bin/env python3
import os
import sys

dr = sys.argv[1]

out1 = ["<html>", "<body>", '<script type="text/javascript">']
out2 = ["</script>", "</body>", "</html>"]

for root, dirs, files in os.walk(dr):
    for f in files:
        if f.endswith(".desktop"):
            f = root+"/"+f
            rewrite = False
            lines = [l.strip() for l in open(f).readlines()]
            for i, l in enumerate(lines):
                if l.startswith("Type=Link"):
                    rewrite = True
                elif l.startswith("URL="): 
                    url = 'window.location.href = "'+l.replace("URL=", "")+'"'
                    out1.append(url)
                elif l.startswith("Name="):
                    name = l.replace("Name=", "")
            if rewrite == True:
                open(f.replace(".desktop", ".html"), "wt").write(("\n").join(out1+out2))
                os.remove(f)

To use it, save it as convert_links.py, run it with the targeted directory as an argument:

python3 /path/to/convert_links.py <directory>

Note

This version was based on this nice answer on Super User to create cross-platform links.

  • Excellent work, but something isn't quite working. The files have changed to - Type=Application with http://www.imdb.com/title/tt0449467/?ref_=fn_al_tt_1 but still are being downloaded by CHrome instead of opened – Kalamalka Kid May 6 '16 at 9:16
  • Oh wait, yes, execute works fine, but not double click! Ok, got it. Looks like you're getting some more of my $$ tonight ha-ha. – Kalamalka Kid May 6 '16 at 9:19
  • while your script may have solved my SpaceFM problem on my Ubuntu machine, it creates another problem for the same files on my OSX machine. Simply put, these .desktop files will be shared on computers with different operating systems, and OSX does not know how to open this kind of modified .desktop file, so I am going to need an actual solution instead of a workaround. – Kalamalka Kid May 6 '16 at 10:43
  • It would be good to see a solution that would work so that users don't have to modify the files, as links are often shared over different platforms (like OSX and Windows), both of which would not be able to read the modified .desktop files described in this (brilliant) workaround. =) – Kalamalka Kid May 6 '16 at 10:52
  • agreed. I will mark your answer as the solution. I am in touch with developers, and they are looking into it. Thanks again. – Kalamalka Kid May 6 '16 at 11:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.