Sublime Forum

Command "is_visible" for Images Only

#1

Hi all. I’m creating a plugin that adds a new right click menu option for files in the side bar. I’d like that menu option to only show for PNG, JPEG, and WEBP files. Is that possible? If so, can you guide me to some information on this or show me an example? Thanks so much for your time.

0 Likes

#2

The is_visible() method gets the same arguments that the run() method would get run with if the command was executed, so that you can make the command visible or invisible in the menu based on what it would do.

Since items in the side bar menu get either a dirs, files or paths (depending on how you set up your menu entry), is_visible() would get the files that are selected and you can return True or False as appropriate.

0 Likes

#3
# Make command visible only when the file is a PNG
def is_visible(self, files):
	return pathlib.Path(files[0]).suffix == ".png"

Right? It works. Magical. Thanks!

0 Likes

#4

Well… this is kinda right, but not quite being that it only checks the first selected file… It’s a start.

0 Likes

#5

The argument will get an array of file names; you need to compare against all of them, and (presumably) return True only if they’re all images.

0 Likes

#6

Right. Now I’m gettin’ somewhere.

# Make command visible only when the file is a PNG, JPG, or WEBP
def is_visible(self, files):
	for file in files:
		file_ext = pathlib.Path(file).suffix
		if file_ext == ".png" or file_ext == ".jpg" or file_ext == ".webp":
			result = True
		else:
			result = False
			break
			
	return result

Thank you so much @OdatNurd (and thanks for those videos)

1 Like