Sublime Forum

How to get the filename from an image view?

#1

I was trying to create a delete_open_file command, that I can bind to a key and trigger it to delete the file associated with the currently active view

import os.path

import sublime
import sublime_plugin


class DeleteOpenFileCommand(sublime_plugin.WindowCommand):
    def run(self):
        view = self.window.active_view()
        if view is None:
            return
        file_name = view.file_name()
        if file_name is None or not os.path.exists(file_name):
            return
        if sublime.ok_cancel_dialog("Delete %s?" % file_name, "Delete"):
            if not view.close():
                return
            import Default.send2trash as send2trash
            send2trash.send2trash(file_name)

this works but not for image previews for which self.window.active_view() returns None, so how can I implement this correctly ?

0 Likes

#2

If I remember correctly, you would normally get None when trying to get the active_view when the image preview is active. But you can get the sheet with active_sheet. Assuming you can’t get the view and you do get a sheet, you can assume you are looking at an image preview and use window.extract_variables().get('file') to get the file name.

3 Likes