Alternative to the Shell script for OSX (not only)

I have had some trouble with session handling in OSX (apparently trouble with absolute file paths). So I decided to write an alternative script to launch TextAdept. It ended up as a small Python program I placed in my /usr/local/bin

#!/usr/bin/env python
from __future__ import print_function
import os
import subprocess
import sys

def mdfind(query):
    p = subprocess.Popen(["mdfind" , query], stdout = subprocess.PIPE)
    res,_ = p.communicate()
    return res.strip()

if "TEXTADEPT_APP_DIR" not in os.environ:
    os.environ["TEXTADEPT_APP_DIR"] = "/Applications/"
textadept_app_dir = os.environ["TEXTADEPT_APP_DIR"]

if not os.path.isdir(textadept_app_dir):
    textadept_app_dir = mdfind("kMDItemCFBundleIdentifier == 'com.textadept'")

if not os.path.isdir(textadept_app_dir):
    print ("Textadept doesn't appear to be at %s" % textadept_app_dir)
    raise SystemExit

    n if n.startswith('-') else os.path.abspath(n) for n in sys.argv[1:]

textadept.insert(0,os.path.join(textadept_app_dir, "Contents/MacOS/textadept_osx"))

subprocess.Popen(textadept, stderr = subprocess.PIPE).communicate()

Edited on Friday, March 21st 2014 at 7:21 AM.