Sublime Forum

Open in Browser

#1

How can I enable the “Open in Browser” command for other file types, such as xml and xhtml?

My apologies in advance if this is already answered elsewhere, I haven’t been able to find it.

0 Likes

Easy way to open URLs in content?
#2

This is most likely related question: Open edited file with custom browser

Abstracted saying — the question is how to open current file with some needed
application.

0 Likes

#3

Perhaps related, but I think the solutions will be quite different for the two questions. The “Open in Browser” command already exists; it is simply disabled for all file types that are not HTML. I would guess this is a setting in the file-type preferences or something. The other question regards modifying the command itself, which may involve writing a plugin. Both would certainly be useful.

0 Likes

#4

Open in Browser is implemented by the plugin Packages/HTML/OpenInBrowser.py

If you open that up, you can see that it’s hardcoded to only be enabled file names that end in .html or .htm - it would be easy to modify your local copy to use a different set of file extensions.

0 Likes

#5

Awesome, thanks a lot.

It seems I was wrong, and that they are related, as the script could also likely be modified to open a different browser or application.

0 Likes

#6

OpenInBrowser.py

[code]import sublime, sublimeplugin
import webbrowser

class OpenInBrowserCommand(sublimeplugin.TextCommand):
def run(self, view, args):
if view.fileName():
webbrowser.open_new_tab(“file://” + view.fileName())

def isEnabled(self, view, args):
	return view.fileName() and (view.fileName()-5:] == ".html" or
		view.fileName()-4:] == ".htm" or
		view.fileName()-4:] == ".php")

[/code]
this works, but it’s sending this to the browser “file:///C:/xampp/htdocs/test.php”. the problem being that my php environment needs to be accessed as simply http ://localhost/test.php. any idea on how to conditionally remove the “C:/xampp/htdocs/” from the beginning and send via “http://” if we’re dealing with a php file. total python newb. :blush:

sean

0 Likes