Using "Open with" on a text file with a python application - python

I've been looking for a way to open a text file with a text editor I made with python, and assumed it had something to do with system arguments, so made a simple application which would write the arguments sent by the system to a text window, and used "open with" on the text file and application, but the only argument was the path of the application. Many questions similar to mine have been answered on here but none of the answers have worked for me. How would I do this?
Thanks for any responses.
(I'm using OS X 10.9.5, with python 2.7)
Tried code:
from Tkinter import *
import sys, time
root = Tk()
root.geometry('300x200')
text = Text(root)
text.pack(side=TOP, fill="both", expand=True)
text.insert(END, sys.argv)
for x in xrange(len(sys.argv)):
text.insert(END,sys.argv[x])
root.mainloop()
Displayed text:
['/Path/pyfe.app/Contents/Resources/file_opener.py']/Path/pyfe.app/Contents/Resources/file_opener.py

If I understand your question correctly, you are talking about opening a file with the Finder's Open with context menu when clicking on a file. If so, it's probably a duplicate of MacOSX - File extension associate with application - Programatically. The standard way is to create an OS X app bundle (for Python programs, you can use py2app to do that) and then set proper key type in its Info.plist. That's assuming your text editor is a true GUI app (uses Tkinter or Cocoa or whatever) and not just a program that runs in a shell terminal window (in Terminal.app for example). In that case, you might be able to create a simple wrapper app (even using AppleScript or Automator and modifying its Info.plist as above) to launch your Python program in a terminal window and pass in the file name from the open event. To properly handle multiple files opened at different times would require more work.
UPDATE: as you have clarified, you are using Python Tkinter and a real GUI app. The native OS X Tk implementation provides an Apple Event handler to allow you to process Apple Events like Open Document. The support is described in tcl terms in the Tcl/Tk documentation so you need to translate it to Python but it's very straightforward. Python's IDLE app has an example of one, see, for instance, addOpenEventSupport in macosxSupport.py. For simple apps using py2app, you could instead use py2app's argv emulation.

I published a detailed description at https://moosystems.com/articles/8-double-click-on-files-in-finder-to-open-them-in-your-python-and-tk-application.html.
As answered above you need to adapt your Info.plist file to tell OS X that your app can at least view the file types you want to process.
Then you can use py2app's argv emulator to access any files dragged on your app while it's not running.
Then install a Tk event handler to accept files while the app is already running.
Find the details in the linked article.

Related

Simple Windows Popup Window using Python

I am trying to find the easiest (with shortest code) way of displaying a popup dialog window in Windows in my non-GUI Python 3.7 program. Basically, it tries to find and grab control of a window with a particular name. If it finds the window, it does its thing and all is good. If it doesn't find the window I'd like a wee popup message saying "Window not found". I know I can do this using PyQt5 or similar, but it is an awful lot of code just display a dialog!
You could do
import ctypes
mymessage = 'A message'
title = 'Popup window'
ctypes.windll.user32.MessageBoxA(0, mymessage, title, 0)
ctypes is part of the standard library, so this works on Windows without the need to install any packages.
A most simple way I could think of (not necessarily elegant) would be creating a .vbs file and calling MsgBox from there
MsgBox "Window not found!", 65, "Information"
Then call that file from Python using
>>> import os
>>> os.system("call popupmessage.vbs")
However, this is not cross-platform, but I assume you are using Windows.
Also keep in mind the directory in the os.system call is relative to your working directory unless you specify the full (or absolute) directory.
You should refer to https://www.tutorialspoint.com/vbscript/vbscript_dialog_boxes.htm on how to use MsgBox

Ubuntu python: interface for running multiple script

I've created a few python scripts that I have to run every once in a while, but now I constantly have to type or search the location to the script in order to execute it. I have been looking for simple software to create an app with buttons, but without success. I have also seen "python?" scripts with an interface where you can select options with arrow keys, but also for this I have not found how it is done. Is there anyone who knows how I can make one of these that I can open the interface and select the script I want to execute? It would really save me a lot of time.
You can Tkinter to create the GUI, and for each button create a function that runs your python script like this:
# Create the button
button1 = Button \
(root, text='Start script1!', command=lambda: script1())
def script1():
os.system('python ~/path/to/script1.py')
def script2():
os.system('python ~/path/to/script2.py')
I recommend using PyQt because that framework is the powerful tool for building GUI on python and for running scripts from python code as system processes you can use subprocess module. I think you know how to use Google to find tutorials =) And also you can check the official page with GUI FAQ here. Thank you!

Specify Input File in SPSS Statistics

In older versions of SPSS, one could ask for the path to a file using a syntax similar to this:
filename = GetFilePath("*.txt","txt", ,"Specify input file",0)
How can I achieve the same result using recent versions of SPSS, that rely on Python?
I found the answer. So similarly to how the VBA in .SBS script works, Python can use a library directly from SPSS to show a file input dialog.
BEGIN PROGRAM.
from Tkinter import Tk
from tkFileDialog import askopenfilename
Tk().withdraw()
filename = askopenfilename()
print(filename)
END PROGRAM.
sbs/wwd scripts are still supported on Statistics.
As for the prompt, you could create a wwd/sbs dialog to prompt for the file name and then open it, or, without using scripting, you could create a custom dialog box using the Custom Dialog Builder (on Utilities) containing a Browse control and issue a GET command.
If you want this to happen when Statistics is launched, you can create a startup script or use the STATS PROJECT extension command, depending on exactly what you want to do.
I am not aware of SPSS Statistics ever having a function such as GetFilePath, but you can use the Python spssaux.getDatasetInfo function for this, e.g., in a program write
import spssaux
filename = spssaux.getDatasetInfo()

python 3 create a GUI to set directory in a program

I would like to create a GUI that pops up asking where to download a file using python. I would like it to be similar to the interface that Google Chrome uses when downloading a file as that looks pretty standard. Is there a default module or add on that I can use to create thus GUI? or will I have to create myself? any help would be appreciated.
If You mean the dialog window, in which You chose where to put file, it's tkinter.filedialog (https://docs.python.org/3/library/tkinter.html), which would give the most native look and feel.
But if You mean the dialog, in which You chose whether to save file in default location or specify another one, there's no such widget, but You may build it on Your own. For that case You probably should dig into Chromium sources, to determine how exactly it acts(https://chromium.googlesource.com/)
There are a number of GUI toolkits you could use, including:
Kivy (modern touch-enabled)
Tkinter (bundled with Python)
These have file chooser widgets, which you could use that would provide standard-looking interfaces to your file system.
How do you want to run this program?

Drag drop file onto Python script

I'm looking for a cross-platform way of making my Python script process a file's path by implementing a drag n drop method. At the moment I manually go to the terminal and use the sys.argv method:
python myscript.py /Python/myfile.xls
However this is slow and "techy". Ideally I would a quick and interactive way of allowing a file be processed by my Python script. I primarily need this to work for Mac but cross-platform would be better.
If you want to use Tkinter, have a look at the Tkinter DnD binding from here http://klappnase.bubble.org/TkinterDnD/index.html
When run, the binding shows an example with a listbox that allows you to drag a file on to it.
Do you want to drag and drop the myfile.xls onto your python script within your file navigator ? Say Finder or whatever on Mac, Explorer on Win, Nautilus etc. ? In that case there will not be a simple cross-platform solution, given that you will have to hook into different software on different systems.
For a Mac specific solution try AppleScript - here is a sample
And for something Pythonic there is http://appscript.sourceforge.net/ , http://docs.python.org/library/macosa.html
Otherwise the solution is in the answer above. Use a custom GUI built in Tk, or wx or QT. You can look up their respective documentation for drag and drop, they do have cross-platform ways of doing it.
It'd be easiest to just write a small GUI with Tkinter or something similar and have the user select a file from within the GUI. Something along these lines:
import tkFileDialog
f = tkFileDialog.askopenfilename()
# Go on from there; f is a handle to the file that the user picked
I'm not aware of any cross platform methods to get a script to work with drag and drop, however. This is probably easier, though.

Categories

Resources