【问题标题】:get current file name from sublime_plugin.WindowCommand从 sublime_plugin.WindowCommand 获取当前文件名
【发布时间】:2013-10-31 17:45:30
【问题描述】:

我为sublime text 3 开发插件。并想获取当前打开的文件路径...

absolute1 = self.window.view.file_name()

...其中selfsublime_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())

如果selfsublime_plugin.TextCommand 我可以毫无问题地获取当前文件路径:

fileName = self.view.file_name()

...但是self 必须是sublime_plugin.WindowCommand 因为我想使用open_file 方法:

self.window.open_file(absolute2)

【问题讨论】:

    标签: python plugins sublimetext2 sublimetext3


    【解决方案1】:

    对于 Sublime Text 3,对我有用的命令是:

    self.view.window().active_view().file_name()
    

    【讨论】:

      【解决方案2】:

      查看 API (http://www.sublimetext.com/docs/3/api_reference.html#sublime.Window)。 self 是一个窗口对象。所以你需要做self.window.active_view() 来获取视图。

      【讨论】:

      • 我尝试过,但失败了:absolute1 = self.active_window().view.file_name() AttributeError: 'OpenrelCommand' object has no attribute 'active_window'
      • 抱歉,我的意思是self.active_view()
      • 对不起。 absolute1 = self.active_view().file_name() AttributeError: 'OpenrelCommand' object has no attribute 'active_view'
      • 伙计,我今天遇到了问题。哈哈。 self.window.active_view()
      【解决方案3】:

      对于 sublime Text 3,我认为使用以下内容:

      myCompleteName = self.view.file_name()
      

      可以是一个解决方案,我已经尝试使用 sublime_plugin.TextCommand 并且效果很好

      【讨论】:

        猜你喜欢
        • 2012-12-21
        • 2011-05-12
        • 2011-08-18
        • 2010-10-26
        • 2014-03-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多