Matthew B
Matthew B

Reputation: 398

How to use python to create an app that you can use to open other files?

I am working on a project that allows you to create your own key shortcuts. The program needs to import a list of settings from another file, I have called these .pog files. Here is an example of a .pog file:

q||tap``ctrl`l|type`docs.google.com`enter`
a||tap``alt``tab`
z||quit

Here is main.py, as you can see I just import the .pog file to use it in my program

import monitor

with monitor.Monitor.from_file('demo.pog') as monitor:
  while monitor.running:
    'vibe in the background'

This is a pain to work with as every time I want to use a different set of shortcuts, I have to either rewrite demo.pog, or open main.py and change the directory of the .pog file it opens.

I would like to be able to right click on a .pog file and choose "open with main.py" the same way I would open a file with notepad or idle. Is there a way to set up a python file to be able to open other files like this?

Upvotes: 0

Views: 1160

Answers (1)

Kieran Wood
Kieran Wood

Reputation: 1447

Your best option would be to turn the python file into an executable using pyinstaller. It will basically let you turn your .py file into a .exe on windows or plain binary on macos/linux.

Basic pyinstaller use

The easiest way to do this is to install it:

pip install pyinstaller

Then run it over your file specifying that you want the output to be a single .exe file:

pyinstaller --onefile yourfile.py

There will be a folder inside wherever yourfile.py is called /dist and inside there will be the .exe or binary file.

Getting the binary to be recognized for the extension

From there there's system-level ways to register binary programs as extension handlers that differ per OS.

Windows

With windows you can just right-click a file and select Open With...: Open With Context menu

then scroll down to find Look for another app on this PCand browse to the new .exe file you created with Pyinstaller:

Set default application extension handler

To update the file just replace the .exe file with new builds of your script using the same pyinstaller method from above.

MacOS

I don't own a mac, but here are the first two guides I found on google:

Linux

This changes by every different distribution of linux. I would recommend going to a community forum to ask this, because even across similar distributions there are custom implementations to make this "easier"

Upvotes: 1

Related Questions