0

I want to open an existing pdf in Preview and a text file in TextEdit (which will either be edited or not), wait for some input, and then close both windows (and let the process repeat).

What would be the best way to do this on Mac OS? subprocess.call(['open', '-a', 'TextEdit', filename]) works fine, but I'm not sure how to terminate the process on the user's input.

EDIT: I'm opening the process as such:

txt=subprocess.Popen(['open','-a','/Applications/TextEdit.app/','path_filename'])
user_input = raw_input("Continue? Enter 'quit' to exit")
txt.kill()  # nothing happens
if user_input.lower() == 'quit':
    break
2
  • How would you know the user has finished entering text? What are you trying to do overall? There's probably a better way... Commented Apr 10, 2018 at 20:47
  • Is the user supposed to comment on the PDF by entering text in TextEdit or something? Or is the text going to be copied from TextEdit into the PDF? Commented Apr 10, 2018 at 21:46

2 Answers 2

1

Updated Answer

From your comments, it seems that the Python app doesn't actually need to know the outcome of the user's check of the PDF, just that it needs to know when he has finished checking.

AFAIK, the easiest way is to use the scripting functionality of TextEdit to ask it for a list of the open documents every second or so. Then you could wait until the user closes the document you opened for him and it will disappear from the list.

The script could look something like this, called GetTextEditDocumentList:

#!/bin/bash
osascript -e 'tell app "TextEdit" to get documents'

It will print a list of open documents on its stdout which you can read with Python. Or you could pass it a filename as a parameter and it could loop, with say a 1 second delay in the middle, until that file/document is no longer open.

You can just try it in the terminal while you open and close a few documents with TextEdit to see how it works.


Original Answer

You appear to be trying to use TextEdit to get some text (a commentary maybe) about a PDF visible in Preview, when a text entry dialog box might be more appropriate.

Whilst you could install, and force any user to install Tkinter or wxPython, you can actually do that very simply using Applescript which is already built into macOS without needing to install anything.

So, save the following as dialog:

#!/bin/bash
input=$(osascript <<EOF
tell application "System Events" 
   display dialog "Enter something:" default answer "Some default answer" with title "Gimme da thing" buttons {"Okey dokey"}
end tell
EOF
)
echo $input

And make it executable with:

chmod +x dialog

Then it will pop up a dialog box like this:

enter image description here

And the output is:

./dialog 
button returned:Okey dokey, text returned:No, I refuse

Then you can very simply call this from Python, using:

subprocess.Popen(['dialog'],stdout=subprocess.PIPE)

Of course, you could parameterize the title, prompt and default text and pass those to the script if you need to be more flexible than the static title I made up.

Sign up to request clarification or add additional context in comments.

9 Comments

I'm trying to compare the data in the pdf to the data in the text file. So my goal is to have the two files open side-by-side, be compared (manually), and then closed before the next set opens.
Ok, so how do you know the user has compared and is ready for the next file?
I was going to just have it wait for an input from the user (e.g. press enter (in cmd line) when ready to continue).
So how do you know whether the file contents were the same or not?
I've got that part, but how does your program know what the outcome of the comparison was?
|
0

As far as I know what you want to achieve isn't possible. This is unless you could find a way to catch a character event inside the external program.

I would rather advise you to create your own GUI (Graphical User Interface) in Python, e.g. via wxPython, and then embed a wx.TextCtrl in it so you will have much more control (e.g. it would be easy to catch many types of KeyEvents).

However, to close the application in your example I would do something like:

retrieve_pid_1 = subprocess.Popen(['ps', '-A'], stdout=subprocess.PIPE)
retrieve_pid_2 = subprocess.Popen(['grep', '-m1', 'TextEdit'], stdin=retrieve_pid_1.stdout, stdout=subprocess.PIPE)
retrieve_pid_3 = subprocess.Popen(['awk', '{print $1}'], stdin=retrieve_pid_2.stdout, stdout=subprocess.PIPE)
process_number = retrieve_pid_3.communicate()[0].decode('ASCII').rstrip()
kill_process = subprocess.Popen(['kill', process_number])

1 Comment

How would I go about creating something simple to open two files wait for a key and then close both files (and repeat).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.