* If BuildApplet.py is used as an applet it starts with a version of
  sys.exutable that isn't usuable on an #!-line. That results in generated
  applets that don't actually work. Work around this problem by resetting
  sys.executable.
* argvemulator.py didn't work on intel macs. This patch fixes this
  (bug #1491468)
diff --git a/Mac/scripts/BuildApplet.py b/Mac/scripts/BuildApplet.py
index 756218f..ae5e3a3 100644
--- a/Mac/scripts/BuildApplet.py
+++ b/Mac/scripts/BuildApplet.py
@@ -16,6 +16,18 @@
 import buildtools
 import getopt
 
+if not sys.executable.startswith(sys.exec_prefix):
+    # Oh, the joys of using a python script to bootstrap applicatin bundles
+    # sys.executable points inside the current application bundle. Because this
+    # path contains blanks (two of them actually) this path isn't usable on
+    # #! lines. Reset sys.executable to point to the embedded python interpreter
+    sys.executable = os.path.join(sys.prefix, 
+            'Resources/Python.app/Contents/MacOS/Python')
+
+    # Just in case we're not in a framework:
+    if not os.path.exists(sys.executable):
+        sys.executable = os.path.join(sys.exec_prefix,  'bin/python')
+
 def main():
     try:
         buildapplet()