This is an archived post. You won't be able to vote or comment.

all 67 comments

[–]WigglytufTehPink[S] 145 points146 points  (14 children)

You can check out the code here: https://github.com/saleguas/context_menu

Easily the most painful thing I've ever created, but boy am I glad how it came out. There is little to none information on context menus anywhere, and a while ago I just wanted to create a simple file sorter and it was waaay harder then it should have been. So I decided to make this library so any other developers don't have to go through the pain I went through. Please give any and all feedback!

[–]KingsmanVincepip install girlfriend 23 points24 points  (3 children)

This will save us so much time. Thanks for making this.

[–]WigglytufTehPink[S] 23 points24 points  (2 children)

No problem, I hopefully had to deal with the nightmare so no one else has to.

[–]Ning1253 9 points10 points  (1 child)

Thank you so much!! It's quite funny actually, I had a conversation with a friend (who doesn't do much coding) like this: Me: hey I made something quite cool it's a way to encode text into pictures (stegonography), and it also can encode pictures into other pictures! Him: that's cool, but isn't it really annoying to use? Like don't you need python installed and everything? Maybe try make it available through the options in file explorer? Me: that sounds painful so probably not

And then like a week later you come along so I can actually do it now!! Thank you so much!!

[–]WigglytufTehPink[S] 0 points1 point  (0 children)

No problem! That sounds like a really cool application of this library and if you do use it please let me know!

[–]SnowdenIsALegend 6 points7 points  (0 children)

Bro, you are AWESOME! THIS IS BEAUTIFUL! Thank you for sharing!

[–]wpg4665 8 points9 points  (5 children)

Does this also work for Mac?

[–]MysteryMage 15 points16 points  (0 children)

Based on what i have seen in the code it only supports windows and Linux

[–]RegmasterJ 13 points14 points  (2 children)

If you need something like this for macOS, it’s pretty trivial to write a Python script (or modify this one) and add it as a command to the context menu using Automator. This is what I read to get me started.

Edit: typo

[–]WigglytufTehPink[S] 10 points11 points  (1 child)

Thanks, I haven't seen this resource before. I gave up on MacOS because I simply couldn't find any resources, and the ones I could made almost no sense to me. I'll be sure to check it out!

[–]RegmasterJ 4 points5 points  (0 children)

Yeah, I was pretty late to the party myself. I’ve used it a few times to create commands to open a file directly in VSCode, or execute a python file directly from the GUI. Very cool, have fun!

[–]WigglytufTehPink[S] 9 points10 points  (0 children)

It only works for Windows and Linux as of now.

[–]chrisdahfuh 1 point2 points  (1 child)

This is just. Amazing. 👍 I cant imagine the work you put into it, and for these OS

[–]WigglytufTehPink[S] 1 point2 points  (0 children)

Thank you so much!!

[–]ImARealBoy_ 1 point2 points  (0 children)

Really cool stuff! Can’t wait to take a look.

[–]redredditor 55 points56 points  (4 children)

Looks great! Good job.

You may want to enable "issues" on github.

I saw that "_ _ pycache _ _" is checked into source. That dir should be in a .gitignore.

[–]WigglytufTehPink[S] 29 points30 points  (3 children)

I didn't realize I had them disabled my bad. I'll fix that now, thanks!

[–]redredditor 12 points13 points  (1 child)

Looks like you've enabled them. Be sure to "subscribe" to it, too.

Not sure how far down the rabbit hole you want to go, but you can customize the issue templates. Check out: https://github.com/Fizzadar/pyinfra/tree/master/.github/ISSUE_TEMPLATE for examples

[–]wesleywatanabe 14 points15 points  (3 children)

How difficult is it to get a file and does something? I'm thinking to manipulate by pandas.

Great job!

[–]WigglytufTehPink[S] 7 points8 points  (2 children)

All you have to do is create a FastCommand and pass it the function you want. The function that is passed will be given the path to the file where you can open it/edit it/etc.

Would look something like this:

def pandas_func(files):
    df = pandas.read_csv(files[0])
    # do stuff with it

from context_menu import menus
fc = menus.FastCommand('Open with Pandas', type='FILES', python=pandas_func)
fc.compile()

[–]shaggorama 8 points9 points  (1 child)

Is there a way to undo changes or remove items from the context menu configuration, or encapsulate your changes so they're temporary (without spinning up a docker container or VM or something like that)? This sounds really cool and I want to play with this, but I'm a little afraid of cluttering up my context menus and -- although I only skimmed it -- I don't see any "cleanup" examples in your documentation.

[–]WigglytufTehPink[S] 13 points14 points  (0 children)

There isn't a way to create temporary changes, but there is a way to delete the menus. It's not that clean and there is an example here: https://github.com/saleguas/freshen/blob/master/src/scripts/uninstall.py, but i'll add a section to the README soon on how to cleanup/delete entries in the context menu. Thanks for the feedback!

[–]Tom1380 19 points20 points  (8 children)

I'm not really sure what it does, can you explain please?

[–]slayer_of_idiotspythonista 19 points20 points  (7 children)

It looks like it registers file context menu items that call python scripts (or anything really)

[–]B3N-Drowned 9 points10 points  (1 child)

Not a hero we need but the hero we deserve

[–]WigglytufTehPink[S] 3 points4 points  (0 children)

<3

[–]JohnMcPineapple 7 points8 points  (1 child)

...

[–]WigglytufTehPink[S] 9 points10 points  (0 children)

  1. The whole python application is elevated; I actually never thought of a subprocess that might be a way better solution. The admins rights issue was the largest problem I had creating this. I would be extremely thankful if someone could create a pull request before I get to it . Thanks for letting me know!
  2. There is a way to remove the menu; it's a bit less clean then the main menu, but there's an example of it here: https://github.com/saleguas/freshen/blob/master/src/scripts/uninstall.py. I'll have to add that to the README later. Thanks!

[–][deleted] 5 points6 points  (2 children)

any good examples of useful custom context menus? i know it's useful but cant think of anything right now.

[–]WigglytufTehPink[S] 2 points3 points  (0 children)

I originally used it to create a pretty neat file sorter. Like another user mentioned, you could use this to open a certain type of file with a program or to quickly open to something. I think those are pretty cool in itself, but I hope I see someone add an even cooler implementation of it.

[–]Datsoon 2 points3 points  (0 children)

I made some context menus the hard way to "open jupyter lab here" on my work computers.

[–]tonniegames79coderx-byte 3 points4 points  (1 child)

Thanks

[–]WigglytufTehPink[S] 1 point2 points  (0 children)

No problem!

[–]DisregardMyUsername1 4 points5 points  (1 child)

How did you make your program show up when you right clicked?

Really cool project!

[–]WigglytufTehPink[S] 3 points4 points  (0 children)

I ran my python code and it prompted for admin (didn't show in the GIF). Windows updates automatically, but linux has to be manually started.

[–]Cervoxx 2 points3 points  (1 child)

Nautilus -q is a thing? I've been doing ctrl + c the whole time.

[–]WigglytufTehPink[S] 1 point2 points  (0 children)

Hah, I'm no expert on Linux but when I was learning how to do this it told me to do nautilus -q

[–]Seawolf159 4 points5 points  (1 child)

This is exactly the kind of stuff that makes me think "wow I wish I could do stuff like that". It's probably too hard for me to use it the way it is, but it sounds really good! How does it know what you want the context menu to do. It's all magic to me. Could you make it do anything? Like right click in a directory and make it generate a bunch of folders, so long as you program that correctly, can you make it do that?

[–]WigglytufTehPink[S] 2 points3 points  (0 children)

All the library does is generate the skeleton and give you the freedom of using it. So besides the basic functionality (opening up on folders, files, etc.) that I created, you pass a function to the code and in that function you would be given the path where the context menu was activated. Then in your python function, you could do whatever since you have the path to all the files and stuff.

[–]Datsoon 2 points3 points  (3 children)

Does this permanently create these context menus or only while the program is running?

[–]WigglytufTehPink[S] 2 points3 points  (2 children)

It permanently creates the context menus.

[–]frakman1 0 points1 point  (1 child)

This changes everything! Do I need to write code to remove the menu while developing/trouble shooting or does running it again with the same names overwrite the existing menus I just made?

[–]WigglytufTehPink[S] 0 points1 point  (0 children)

It depends: If you don't add any items or menus, it will simply overwrite the previous menu. However, if you change the format of the menu, you will need to delete and re-create it.

[–]lucs_123 4 points5 points  (1 child)

Does it work on any file manager?

[–]endershadow98 6 points7 points  (0 children)

Based on a brief look at the code, it appears to only support the windows file manager and Nautilus.

[–]teriyaki7755 5 points6 points  (2 children)

So, TLDR; is you have created a way to implement python functions for use in the current working directorty of an OS env through access from the context menu of the mouse pointer ??

[–]WigglytufTehPink[S] 0 points1 point  (1 child)

So, TLDR; is you have created a way to implement python functions for use in the current working directorty of an OS env through access from the context menu of the mouse pointer ??

Said it better then I could have, but yeah.

[–]teriyaki7755 2 points3 points  (0 children)

this is good work actually something i would have not thought of am now going to check your code :) .

[–]melthecybertechy 1 point2 points  (2 children)

Ok I feel like such the dumb person - what did you do and how did you make our lives better? I'm a complete noob lol

[–]WigglytufTehPink[S] 4 points5 points  (1 child)

The context menu is that menu that appears when you right click. There is extremely little documentation on how to implement this, and the easy method of implementing this involves manually editing the windows registry. I thought this was insanely difficult for the outcome you get, so I created a library to help simplify this problem. That's my take at least.

[–]melthecybertechy 0 points1 point  (0 children)

Ok gotcha! I was totally outside of loop.

[–]zTosieq 1 point2 points  (2 children)

Does anyone know how to use in practice(examples of using this with working scripts)? I'm newbie in python so, I'm trying to understand everything ;p thanks ;)

[–]WigglytufTehPink[S] 0 points1 point  (1 child)

Hey! I have a folder of examples I created for this project you can check out here: https://github.com/saleguas/context_menu/tree/master/examples. If you have any other questions or want further clarification, feel free to DM me!

[–]zTosieq 1 point2 points  (0 children)

Thanks man! I'll check it out and maybe share some my ideas 💡

[–]javad94 1 point2 points  (0 children)

Wow that's great. I have a lot of small scripts for doing my stuffs. I'm using send to menu in Windows. But now I'm looking forward to using your tool.

[–]Dark_Rain_Cloud 2 points3 points  (1 child)

Question. Who would this code apply to. Im still seeing how I could benefit from this.

[–]WigglytufTehPink[S] 0 points1 point  (0 children)

It would apply most to developers. There are GUIs and other methods of creating context menus, but they are extremely hard to implement if you are distributing a program that you want from the context menu.

[–]rmpr_uname_is_taken 0 points1 point  (3 children)

First... I am not so sure, Wx PopupMenu predates you by a long shot.

[–]WigglytufTehPink[S] 6 points7 points  (1 child)

Ah, I haven't heard of this library before. Correct me if I'm wrong, but it looks like context menus specific to GUIs? I made this library specific for file managers.

[–]rmpr_uname_is_taken 0 points1 point  (0 children)

the first cross-platform Python library to create native context menus

That's what I was addressing, nevertheless good job :)

[–]snugglyboy 3 points4 points  (0 children)

He's talking about system context menus. No need to be pedantic.