Home Download Buy Blog Forum Support

Opening files

Opening files

Postby bblue on Thu Aug 30, 2012 7:02 pm

Hi List,
I'm new to ST2 and new to Python, so forgive me: my question will be very very silly... :-(

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?

Code: Select all
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
bblue
 
Posts: 26
Joined: Mon Oct 10, 2011 10:35 am

Re: Opening files

Postby bblue on Thu Aug 30, 2012 9:47 pm

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".

Code: Select all
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 ;-)
--
bblue
bblue
 
Posts: 26
Joined: Mon Oct 10, 2011 10:35 am

Re: Opening files

Postby zfw on Mon Sep 03, 2012 3:16 pm

zfw
 
Posts: 1
Joined: Thu Aug 02, 2012 2:12 pm

Re: Opening files

Postby bblue on Mon Sep 03, 2012 3:37 pm



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... :-(

Do you have any idea ;-)

Thanks again,
--
bblue
bblue
 
Posts: 26
Joined: Mon Oct 10, 2011 10:35 am


Return to Plugin Development

Who is online

Users browsing this forum: Exabot [Bot] and 4 guests

cron