Skip to content Skip to sidebar Skip to footer

Blocking Call While Opening A File In Python On Osx

I am using python 2.7 on osx 10.9 I want to open a file in their default file opener like TextEdit for .txt file, pdf opener for .pdf file etc. As file is opened, it should block

Solution 1:

Here's the deal with OSX. It doesn't close an application just because you "close" it. OSX is intended to speed up and make ease of everything you do, there for it leaves the application as is unless you do something new with it (open a new file which open up the application or you force close it).

You can do:

from subprocess import Popen
handle = Popen('open -W ' + FileName, shell=True)

# Do some stuff
handle,terminate()

And that would shut down the application. I really didn't understand what exactly it is you want to do because your question is quite badly written (not that i'm any bettter with my English). But if you want to wait for the application to terminate, you'd do:

from time import sleep
while handle.poll() isNone:
    sleep(0.025)
# do something after TextEditor has shut down

But then again, you'd have to force:ably close the application from the dock, because again.. OSX doesn't shut down the application just because you press the close button.

What you could do is something like:

from subprocess import Popen, STDOUT, PIPE

watchdog = Popen('iosnoop -f ' + FileName, shell=True, stdout=PIPE, stderr=STDOUT)
textfile = Popen('open -W ' + FileName, shell=True)

while watchdog.poll() is None:
    output = watchdog.stdout.readline()
    if'close'inoutput.lower():
        break

textfile.terminate()
watchdog.terminate()
watchdog.stdout.close()

This would:

  • Open a I/O snoop which prints all information regarding OPEN, SEEK, CLOSE etc for the filename given
  • Open up the texteditor with the file you requested
  • If 'CLOSE' is present in the IO Snoop, we close the texteditor

It's just an idea out of the blue but i solved similar issues with OSX this way. THere's also dtrace, iosnoop, execsnoop

Post a Comment for "Blocking Call While Opening A File In Python On Osx"