问题描述
我为sublime text 3
开发了插件.并想要获取当前打开的文件路径...
I develop plugin for sublime text 3
. And want to get currently opened file path ...
absolute1 = self.window.view.file_name()
...,其中self
是sublime_plugin.WindowCommand
但是失败:
AttributeError: 'Window' object has no attribute 'view'
插件的完整代码:
import sublime, sublime_plugin
import re, os, os.path
class OpenrelCommand(sublime_plugin.WindowCommand):
def run(self):
relative = sublime.get_clipboard()
absolute1 = self.window.view.file_name()
absolute2 = os.path.normpath(os.path.join(os.path.dirname(absolute1), relative))
self.window.open_file(absolute2)
def is_enabled(self):
return bool(sublime.get_clipboard().strip())
如果self
将是sublime_plugin.TextCommand
,我可以毫无问题地获得当前文件路径:
If self
would be sublime_plugin.TextCommand
I could get current file path without a problem:
fileName = self.view.file_name()
...但是self
必须是sublime_plugin.WindowCommand
,因为我想使用方法open_file
:
... but self
must be sublime_plugin.WindowCommand
because I want to use method open_file
:
self.window.open_file(absolute2)
推荐答案
看看API( http://www.sublimetext.com/docs/3/api_reference.html#sublime.Window ). self
是一个窗口对象.因此,您需要执行self.window.active_view()
来获取视图.
Take a look at the API (http://www.sublimetext.com/docs/3/api_reference.html#sublime.Window). self
is a window object. So you need to do self.window.active_view()
to get the view.
这篇关于从sublime_plugin.WindowCommand获取当前文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!