Sublime Forum

Opening files

#1

Hi List,
I’m new to ST2 and new to Python, so forgive me: my question will be very very silly… :frowning:

In the long run, I’m trying to build a plugin that (e.g.) opens the file “~/Documents/001.tex” if I ctrl-double-click inside the curly braces of “{001}”.

But as a first step, I’m trying to make a plugin that opens “~/Documents/001.tex” if the caret is inside “{001}”, say as in “{0|01}”, and mycommand is invoked.

What’s wrong with the following code?

import sublime
import sublime_plugin

class MycommandCommand(sublime_plugin.WindowCommand):
    def run(self, edit):
        self.view.run_command("expand_selection", {"to": "brackets"})
        sel = self.view.sel()
        docnum = self.view.substr(sel[0])
        open_file("~/Documents/0007xx/${docnum}.tex")

Many, many thanks in advance for any help.

bblue

0 Likes

#2

OK, I managed the easy part!

This code works (although I still don’t understand the why of the "v = " syntax…): if I select “001” and then run mycommand, it opens the file “~/Desktop/001.tex”.

import sublime
import sublime_plugin
import os

class MycommandCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        sel = self.view.sel()
        docnum = self.view.substr(sel[0])
        filepath = '/Users/bblue/Desktop/'
        filename = docnum
        ext = '.tex'
        if os.path.exists(filepath+filename+ext):
        	v = sublime.active_window().open_file(filepath+filename+ext)
    	else:
        	print('Oops: '+filepath+filename+ext+' does not exist!')
    	return 0

Now remains the hard part: how to associate this with a ctrl-double-click…

Anyone?

TIA :wink:

bblue

0 Likes

#3

This maybe helpful :smiley:
https://forum.sublimetext.com/t/can-i-make-double-click-do-normal-behaviour-my-own/6962/3&hilit=double+click#p35945

0 Likes

#4

Yes zfw, thank you.
I’m afraid to say that I read it, but didn’t manage to apply to my case.
I know this should contain enough information, but I’m too new to these things… :frowning:

Do you have any idea :wink:

Thanks again,

bblue

0 Likes