Last active
January 2, 2016 09:29
-
-
Save liberize/8283362 to your computer and use it in GitHub Desktop.
sublime text plugin to open file in different browser.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Show hidden characters
[ | |
{ "keys": ["alt+o", "alt+f"], "command": "open_in_browser_alt", "args": {"browser": "firefox"} }, | |
{ "keys": ["alt+o", "alt+s"], "command": "open_in_browser_alt", "args": {"browser": "safari"} } | |
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sublime | |
import sublime_plugin | |
import subprocess | |
import tempfile | |
class OpenInBrowserAltCommand(sublime_plugin.ApplicationCommand): | |
def run(self, **kwargs): | |
browser = kwargs['browser'] | |
browser_cmd_map = { | |
'safari': ['open', '-a', 'safari'], | |
'firefox': ['open', '-a', 'firefox'] | |
} | |
if browser in browser_cmd_map: | |
cmd = browser_cmd_map[browser] | |
view = sublime.Window.active_view(sublime.active_window()) | |
if view.file_name(): | |
cmd.append(view.file_name()) | |
subprocess.Popen(cmd) | |
else: | |
temp = tempfile.NamedTemporaryFile(delete=False) | |
content = view.substr(sublime.Region(0, view.size())) | |
temp.write(content.encode('utf-8')) | |
temp.flush() | |
cmd.append(temp.name) | |
subprocess.Popen(cmd) | |
temp.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment