@@ -36,6 +36,10 @@ def show_context_menu(paths: Sequence[os.PathLike | str]):
36
36
paths = [_safe_path_parse (p ) for p in paths ]
37
37
else :
38
38
return
39
+
40
+ if not paths :
41
+ print ("No usable paths provided." )
42
+ return
39
43
40
44
menu = QMenu ()
41
45
@@ -48,25 +52,39 @@ def show_context_menu(paths: Sequence[os.PathLike | str]):
48
52
# FIXME: Handle multiple items; currently only the first item is used. This might mean that we need to get the Verbs in a different way?
49
53
# TODO: Check if https://github.com/NickHugi/PyKotor/blob/master/Libraries/Utility/src/utility/system/windows_context_menu.py handles multiple items better
50
54
# May need to take a look at SHMultiFileProperties and https://stackoverflow.com/a/34551988/1839209.
55
+ if items [0 ] is None :
56
+ print ("Could not retrieve context menu items." )
57
+ return
51
58
verbs = items [0 ].Verbs ()
52
- for verb in verbs :
53
- if verb .Name :
54
- app = QApplication .instance ()
55
- action = QAction (verb .Name , app )
56
- # Copying the path does not work using the default context menu action,
57
- # hence we override it
58
- # FIXME: Find a way that works independently of the language
59
- if "path" or "Pfad" in verb .Name :
60
- # Copy path to clipboard
61
- action .triggered .connect (lambda _ , p = paths [0 ]: app .clipboard ().setText (str (p )))
59
+ if verbs is not None :
60
+ for verb in verbs :
61
+ if verb .Name :
62
+ app = QApplication .instance ()
63
+ action = QAction (verb .Name , app )
64
+ # Copying the path does not work using the default context menu action,
65
+ # hence we override it
66
+ # FIXME: Find a way that works independently of the language
67
+ if "path" in verb .Name or "Pfad" in verb .Name :
68
+ # Copy path to clipboard
69
+ action .triggered .connect (lambda _ , p = paths [0 ]: copy_path_to_clipboard (p ))
70
+ else :
71
+ action .triggered .connect (lambda _ , v = verb : execute_verb (v ))
72
+ menu .addAction (action )
62
73
else :
63
- action .triggered .connect (lambda _ , v = verb : execute_verb (v ))
64
- menu .addAction (action )
65
- else :
66
- menu .addSeparator ()
74
+ menu .addSeparator ()
67
75
68
76
menu .exec (QCursor .pos ())
69
77
78
+ def copy_path_to_clipboard (path ):
79
+ """
80
+ Copy the specified path to the clipboard.
81
+ """
82
+ print (f"Copying path to clipboard: { path } " )
83
+ app = QApplication .instance ()
84
+ if app is None :
85
+ app = QApplication ([])
86
+ app .clipboard ().setText (str (path ))
87
+
70
88
71
89
def execute_verb (verb ):
72
90
"""
@@ -75,8 +93,8 @@ def execute_verb(verb):
75
93
Args:
76
94
verb: The verb to execute.
77
95
"""
96
+ print (f"Executing verb: { verb .Name } " )
78
97
try :
79
- print (f"Executing verb: { verb .Name } " )
80
98
verb .DoIt ()
81
99
except Exception as e :
82
100
show_error_message (f"An error occurred while executing the action: { e } " )
0 commit comments