0

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

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
gilesjohn
  • 15
  • 1
  • 4

2 Answers2

1

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.

Community
  • 1
  • 1
Ned Deily
  • 83,389
  • 16
  • 128
  • 151
  • My answer crossed with your update. So it looks like you are using a GUI app so the first part of the answer applies. Note, that for testing purposes, you *could* set the file association manually in the Finder by using the Finder `Get Info` command and changing the `Open with` field for the file or all files of that type. – Ned Deily Nov 17 '14 at 20:43
  • To clarify, I'm not trying to make OS X open a file with the application, instead I am trying to retrieve any information the system sends when I click 'Open with' which will allow the application to open the file and retrieve the text inside it. – gilesjohn Nov 17 '14 at 21:00
  • OS X communicates that information to the app via Apple Events like `open application` and `open document`. py2app` provides an "argv" emulator to process the initial `open application` and `open document` events. If you are using something else or if you want to process multiple file openings etc, you'd need to supply an event handler of some sort, perhaps using `pyobjc`. There's more information here: https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ScriptableCocoaApplications/SApps_handle_AEs/SAppsHandleAEs.html – Ned Deily Nov 17 '14 at 21:14
  • Ah, but you are using Tkinter. Tk will do the event processing for you. Update to follow. – Ned Deily Nov 17 '14 at 21:17
  • Great thanks, by adding "--argv-emulation" to the options in py2app I was able to receive the file name as an input. – gilesjohn Nov 17 '14 at 21:27
  • Can I select your comment as the answer or should I select the answer? – gilesjohn Nov 17 '14 at 21:31
  • That will work for the simple case of just opening one or more documents at app launch time. If you want full Apple Event support, for example, allowing the user to open additional documents while the app is running, you can use the Tk event callbacks described in the update above. – Ned Deily Nov 17 '14 at 21:32
  • You need to select the answer, I think. – Ned Deily Nov 17 '14 at 21:32
-1

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.

André Aulich
  • 190
  • 10