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.
Command "is_visible" for Images Only
OdatNurd
#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
# 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
Well… this is kinda right, but not quite being that it only checks the first selected file… It’s a start.
0 Likes
OdatNurd
#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
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
1 Like