Try saving this as "EditOneAndQuit" and then do:
chmod +x EditOneAndQuit
Then start that from Python:
#!/bin/bash
# Start textedit in background
open "$1" &
# Wait till textedit has zero documents open
while true
do
sleep 1
docs=`osascript -e 'tell application "textedit" to get documents'`
if [ -z "$docs" ]; then
# Kill off poor old textedit
osascript -e 'tell application "textedit" to quit'
exit
fi
done
Try it from the shell first, by creating a document and editing it:
ls > fred.txt
./OpenOneAndQuit fred.txt
you should see that the script, along with textedit, exits when you close the document by clicking the red button.