我为开发插件sublime text 3
。并想要获取当前打开的文件路径...
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
我可以毫无问题地获取当前文件路径:
fileName = self.view.file_name()
...但是self
一定是sublime_plugin.WindowCommand
因为我想使用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()
获取视图。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句