Here’s a Python script that makes Windows open a URL (in your default browser) when you execute (double click) a Linux .desktop file that contains a URL.
If you use both Windows and Linux, and save links to websites from a browser in Linux, the link will become a .desktop file, which can be opened by Linux. .desktop files are a lot like Windows “shortcut” files, but are (of course!) incompatible with them.
This script lets Windows open the website link saved in a .desktop file.
It relies on Python already being installed in your Windows system.
You can use it at the Windows command line like this:
python launch.desktop.py <.desktop file>
To use it at the Windows GUI (to be able to double-click on a .desktop file to launch it), put the following line in a batch file in your executable path somewhere (one place that would work would be C:\WINDOWS\System32) as “launch.desktop.bat”:
python "%~dp0launch.desktop.py" %1
Then put the Python script in the same folder where you put the batch file, as “launch.desktop.py”:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Launches a Linux .desktop file that contains a URL, on Windows.
Tested on Win10, Python 3.11.
Note: Doing this in a Windows batch file is a problem because findstr doesn't want to open files with special characters in them
(which Linux likes to put there). There are ways around that (make temp files), but Python doesn't mind the characters.
"""
__author__ = 'NerdFever.com'
__copyright__ = 'Copyright 2023 NerdFever.com'
__version__ = ''
__email__ = 'dave@nerdfever.com'
__status__ = 'Development'
__license__ = """'Copyright 2023 NerdFever.com
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License."""
import os, sys, subprocess
def main():
if len(sys.argv) != 2:
print(os.path.basename(__file__))
print("Launches a Linux .desktop file that contains a URL, on Windows.")
print("Usage: python", os.path.basename(__file__), "<filepath>")
return
filepath = sys.argv[1]
if not os.path.exists(filepath):
print(f"Error: {filepath} does not exist!")
return
# open file, scan for "URL=" or "URL[anything]=", get URL from remainder of line
url = None
with open(filepath, "r") as f:
for line in f:
if line.startswith("URL="):
url = line[4:].strip()
break
elif line.startswith("URL["):
url = line[line.find("]")+1:].strip()
break
if url is None:
print(f"Error: {filepath} does not contain a URL!")
return
# run "start" on the URL (launch default browser configured in Windows)
subprocess.run(["start", url], shell=True)
if __name__ == "__main__":
main()
The first time you double-click on a .desktop file, Windows will say it doesn’t know how to open it, and offer to let you choose a program on your PC for that. Choose “launch.desktop.bat” and check the box “Always use this app to open .desktop files”. Done.