Skip to content

Instantly share code, notes, and snippets.

@tnydwrds
Created June 10, 2011 06:07
Show Gist options
  • Save tnydwrds/1018311 to your computer and use it in GitHub Desktop.
Save tnydwrds/1018311 to your computer and use it in GitHub Desktop.
opun.py - open with default app and keep file associations
#!/usr/bin/env python
import optparse
import os
import sys
# Change this to whatever app you want to open by default.
DEFAULT_APP = "Coda"
def main(args):
parser = optparse.OptionParser()
# These are just wrappers for the OS X "open" command and will all be passed when it is run.
parser.add_option('-a', '--app', metavar="APPLICATION", default=DEFAULT_APP, dest="app",
help="Opens with the specified application. Default is %s" % DEFAULT_APP,)
parser.add_option('-w', '--wait', action="store_true", default=False, dest='wait',
help="Blocks until the used application is closed (even if they were already running).")
parser.add_option('-n', '--new', default=False, action="store_true", dest="new",
help="Open a new instance of the application even if one is already running")
parser.add_option('-g', '--background', default=False, action="store_true", dest="background",
help="Does not bring not bring the application to the foreground.")
(opts, args) = parser.parse_args(args)
switches = ""
switches += 'W' if opts.wait else ""
switches += 'n' if opts.new else ""
switches += 'g' if opts.background else ""
# If we have switches add the tack.
switches = "-" + switches if switches else ""
# Create files if any filenames passed in do not exits.
[open(f, 'w') for f in args if not os.path.isfile(f)]
# Run OS X "open" command.
cmd = "open %s -a %s %s" % (switches, opts.app, " ".join(args))
os.system(cmd)
if __name__ == "__main__":
main(sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment