Changeset 1109 for trunk/makeMacApp.py
- Timestamp:
- Oct 15, 2013 5:08:50 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/makeMacApp.py
r1077 r1109 1 1 #!/usr/bin/env python 2 '''This script creates an AppleScript app to launch GSAS-II. The app is 2 ''' 3 *makeMacApp: Create Mac Applet* 4 =============================== 5 6 This script creates an AppleScript app to launch GSAS-II. The app is 3 7 created in the directory where the GSAS-II script is located. 4 8 A softlink to Python is created, but is named GSAS-II, so that … … 22 26 project="GSAS-II" 23 27 24 # find the main GSAS-II script if not on command line 25 if len(sys.argv) == 1: 26 script = os.path.abspath(os.path.join( 27 os.path.split(__file__)[0], 28 "GSASII.py" 29 )) 30 elif len(sys.argv) == 2: 31 script = os.path.abspath(sys.argv[1]) 32 else: 33 Usage() 28 if __name__ == '__main__': 29 # find the main GSAS-II script if not on command line 30 if len(sys.argv) == 1: 31 script = os.path.abspath(os.path.join( 32 os.path.split(__file__)[0], 33 "GSASII.py" 34 )) 35 elif len(sys.argv) == 2: 36 script = os.path.abspath(sys.argv[1]) 37 else: 38 Usage() 34 39 35 # make sure we found it36 if not os.path.exists(script):37 print("\nFile "+script+" not found")38 Usage()39 if os.path.splitext(script)[1].lower() != '.py':40 print("\nScript "+script+" does not have extension .py")41 Usage()42 # where the app will be created43 scriptdir = os.path.split(script)[0]44 # name of app45 apppath = os.path.abspath(os.path.join(scriptdir,project+".app"))46 iconfile = os.path.join(scriptdir,'gsas2.icns') # optional icon file40 # make sure we found it 41 if not os.path.exists(script): 42 print("\nFile "+script+" not found") 43 Usage() 44 if os.path.splitext(script)[1].lower() != '.py': 45 print("\nScript "+script+" does not have extension .py") 46 Usage() 47 # where the app will be created 48 scriptdir = os.path.split(script)[0] 49 # name of app 50 apppath = os.path.abspath(os.path.join(scriptdir,project+".app")) 51 iconfile = os.path.join(scriptdir,'gsas2.icns') # optional icon file 47 52 48 # find the python application; must be an OS X app49 pythonpath,top = os.path.split(os.path.realpath(sys.executable))50 while top:51 if 'Resources' in pythonpath:52 pass53 elif os.path.exists(os.path.join(pythonpath,'Resources')):54 break55 pythonpath,top = os.path.split(pythonpath)56 else:57 print("\nSorry, failed to find a Resources directory associated with "+str(sys.executable))58 sys.exit()59 pythonapp = os.path.join(pythonpath,'Resources','Python.app','Contents','MacOS','Python')60 if not os.path.exists(pythonapp):61 print("\nSorry, failed to find a Python app in "+str(pythonapp))62 sys.exit()53 # find the python application; must be an OS X app 54 pythonpath,top = os.path.split(os.path.realpath(sys.executable)) 55 while top: 56 if 'Resources' in pythonpath: 57 pass 58 elif os.path.exists(os.path.join(pythonpath,'Resources')): 59 break 60 pythonpath,top = os.path.split(pythonpath) 61 else: 62 print("\nSorry, failed to find a Resources directory associated with "+str(sys.executable)) 63 sys.exit() 64 pythonapp = os.path.join(pythonpath,'Resources','Python.app','Contents','MacOS','Python') 65 if not os.path.exists(pythonapp): 66 print("\nSorry, failed to find a Python app in "+str(pythonapp)) 67 sys.exit() 63 68 64 # new name to call python65 newpython = os.path.join(apppath,"Contents","MacOS",project)69 # new name to call python 70 newpython = os.path.join(apppath,"Contents","MacOS",project) 66 71 67 if os.path.exists(apppath): # cleanup68 print("\nRemoving old "+project+" app ("+str(apppath)+")")69 shutil.rmtree(apppath)72 if os.path.exists(apppath): # cleanup 73 print("\nRemoving old "+project+" app ("+str(apppath)+")") 74 shutil.rmtree(apppath) 70 75 71 # create an AppleScript that launches python with the requested app 72 shell = os.path.join("/tmp/","appscrpt.script") 73 f = open(shell, "w") 74 f.write('''(* GSAS-II AppleScript by B. Toby (brian.toby@anl.gov) 76 # create an AppleScript that launches python with the requested app 77 AppleScript = '''(* GSAS-II AppleScript by B. Toby (brian.toby@anl.gov) 75 78 It can launch GSAS-II by double clicking or by dropping a data file 76 79 or folder over the app. … … 132 135 end repeat 133 136 end open 134 '''.format(newpython,script,newpython,script)) 135 f.close() 137 ''' 138 shell = os.path.join("/tmp/","appscrpt.script") 139 f = open(shell, "w") 140 f.write(AppleScript.format(newpython,script,newpython,script)) 141 f.close() 136 142 137 try:138 subprocess.check_output(["osacompile","-o",apppath,shell],stderr=subprocess.STDOUT)139 except subprocess.CalledProcessError, msg:140 print '''Error compiling AppleScript.141 Report the next message along with details about your Mac to toby@anl.gov'''142 print msg.output143 sys.exit()143 try: 144 subprocess.check_output(["osacompile","-o",apppath,shell],stderr=subprocess.STDOUT) 145 except subprocess.CalledProcessError, msg: 146 print '''Error compiling AppleScript. 147 Report the next message along with details about your Mac to toby@anl.gov''' 148 print msg.output 149 sys.exit() 144 150 145 # create a link to the python app, but named to match the project146 os.symlink(pythonapp,newpython)151 # create a link to the python app, but named to match the project 152 os.symlink(pythonapp,newpython) 147 153 148 # change the icon149 oldicon = os.path.join(apppath,"Contents","Resources","droplet.icns")150 if os.path.exists(iconfile) and os.path.exists(oldicon):151 shutil.copyfile(iconfile,oldicon)154 # change the icon 155 oldicon = os.path.join(apppath,"Contents","Resources","droplet.icns") 156 if os.path.exists(iconfile) and os.path.exists(oldicon): 157 shutil.copyfile(iconfile,oldicon) 152 158 153 # Edit the app plist file to restrict the type of files that can be dropped154 d = plistlib.readPlist(os.path.join(apppath,"Contents",'Info.plist'))155 d['CFBundleDocumentTypes'] = [{156 'CFBundleTypeExtensions': ['gpx'],157 'CFBundleTypeName': 'GSAS-II project',158 'CFBundleTypeRole': 'Editor'}]159 plistlib.writePlist(d,os.path.join(apppath,"Contents",'Info.plist'))159 # Edit the app plist file to restrict the type of files that can be dropped 160 d = plistlib.readPlist(os.path.join(apppath,"Contents",'Info.plist')) 161 d['CFBundleDocumentTypes'] = [{ 162 'CFBundleTypeExtensions': ['gpx'], 163 'CFBundleTypeName': 'GSAS-II project', 164 'CFBundleTypeRole': 'Editor'}] 165 plistlib.writePlist(d,os.path.join(apppath,"Contents",'Info.plist')) 160 166 161 print("\nCreated "+project+" app ("+str(apppath)+162 ").\nViewing app in Finder so you can drag it to the dock if, you wish.")163 subprocess.call(["open","-R",apppath])167 print("\nCreated "+project+" app ("+str(apppath)+ 168 ").\nViewing app in Finder so you can drag it to the dock if, you wish.") 169 subprocess.call(["open","-R",apppath])
Note: See TracChangeset
for help on using the changeset viewer.