1+ /*
2+ Copyright (c) 2018-2025, Sveinbjorn Thordarson <sveinbjorn@sveinbjorn.org>
3+ All rights reserved.
4+
5+ Redistribution and use in source and binary forms, with or without modification,
6+ are permitted provided that the following conditions are met:
7+
8+ 1. Redistributions of source code must retain the above copyright notice, this
9+ list of conditions and the following disclaimer.
10+
11+ 2. Redistributions in binary form must reproduce the above copyright notice, this
12+ list of conditions and the following disclaimer in the documentation and/or other
13+ materials provided with the distribution.
14+
15+ 3. Neither the name of the copyright holder nor the names of its contributors may
16+ be used to endorse or promote products derived from this software without specific
17+ prior written permission.
18+
19+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22+ IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23+ INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24+ NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25+ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26+ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+ POSSIBILITY OF SUCH DAMAGE.
29+ */
30+
131#import " NSPathControl+ContextMenu.h"
232#import " NSWorkspace+Additions.h"
3- #import " IconUtils.h"
33+ #import < objc/runtime.h>
34+
35+ static const char * kFilePathAssociatedObjectKey = " filePath" ;
436
537@implementation NSPathControl (ContextMenu)
638
39+ // Generate contextual menu for a Ctrl-clicked item in the Path Control
740- (NSMenu *)menuForEvent : (NSEvent *)event {
8-
941 NSPoint point = [self convertPoint: [event locationInWindow ] fromView: nil ];
1042 NSRect frame = [self bounds ];
1143 NSPathCell *cell = (NSPathCell *)[self cell ];
@@ -23,7 +55,7 @@ - (NSMenu *)menuForEvent:(NSEvent *)event {
2355
2456 NSMenu *menu = [[NSMenu alloc ] initWithTitle: @" " ];
2557
26- // Open
58+ // Open (with default app)
2759 NSMenuItem *openItem = [[NSMenuItem alloc ] initWithTitle: @" Open" action: @selector (openPath: ) keyEquivalent: @" " ];
2860 [openItem setTarget: self ];
2961 [openItem setRepresentedObject: path];
@@ -38,11 +70,15 @@ - (NSMenu *)menuForEvent:(NSEvent *)event {
3870 }
3971 [menu addItem: openItem];
4072
41- // Open With
73+ // Open With submenu
4274 NSMenuItem *openWithItem = [[NSMenuItem alloc ] initWithTitle: @" Open With" action: nil keyEquivalent: @" " ];
4375 NSMenu *openWithMenu = [[NSMenu alloc ] init ];
76+ objc_setAssociatedObject (openWithMenu, kFilePathAssociatedObjectKey , path, OBJC_ASSOCIATION_RETAIN_NONATOMIC );
4477 [openWithItem setSubmenu: openWithMenu];
45- [[NSWorkspace sharedWorkspace ] openWithMenuForFile: path target: self action: @selector (openPathWithApplication: ) menu: openWithMenu];
78+ [[NSWorkspace sharedWorkspace ] openWithMenuForFile: path
79+ target: self
80+ action: @selector (openPathWithApplication: )
81+ menu: openWithMenu];
4682 [menu addItem: openWithItem];
4783
4884 [menu addItem: [NSMenuItem separatorItem ]];
@@ -53,12 +89,13 @@ - (NSMenu *)menuForEvent:(NSEvent *)event {
5389 [showItem setRepresentedObject: path];
5490 [menu addItem: showItem];
5591
56- // Get Info
57- NSMenuItem *getInfoItem = [[NSMenuItem alloc ] initWithTitle: @" Get Info" action: @selector (showPathInfoInFinder: ) keyEquivalent: @" " ];
92+ // Get Info in Finder
93+ NSMenuItem *getInfoItem = [[NSMenuItem alloc ] initWithTitle: @" Show Info in Finder " action: @selector (showPathInfoInFinder: ) keyEquivalent: @" " ];
5894 [getInfoItem setTarget: self ];
5995 [getInfoItem setRepresentedObject: path];
6096 [menu addItem: getInfoItem];
6197
98+ // Separator
6299 [menu addItem: [NSMenuItem separatorItem ]];
63100
64101 // Copy Path
@@ -78,8 +115,8 @@ - (void)openPath:(id)sender {
78115}
79116
80117- (void )openPathWithApplication : (id )sender {
81- NSString *appPath = [sender representedObject ];
82- NSString *filePath = [[[ sender menu ] supermenu ] representedObject ] ;
118+ NSString *appPath = [sender toolTip ];
119+ NSString *filePath = objc_getAssociatedObject ([ sender menu ], kFilePathAssociatedObjectKey ) ;
83120 [[NSWorkspace sharedWorkspace ] openFile: filePath withApplication: appPath];
84121}
85122
0 commit comments