Skip to content

Commit c3b0d9a

Browse files
committed
steal file associations
1 parent 4f61cbe commit c3b0d9a

File tree

2 files changed

+53
-26
lines changed

2 files changed

+53
-26
lines changed

Delegate.m

Lines changed: 52 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
TagSetting=1,
44
TagTheme=2,
55
TagProjectMode=3,
6-
TagTab=4
6+
TagTab=4,
7+
TagFileAssociation=5
78
};
89

910
@implementation Delegate
@@ -81,6 +82,8 @@ -(void)applicationWillFinishLaunching:(NSNotification*)note
8182
[self addItemTitle:@"Close" action:@"performClose:" key:@"w" to:fileMenu];
8283
[self addSeparatorTo:fileMenu];
8384
[self addItemTitle:@"Save" action:@"amySave:" key:@"s" to:fileMenu];
85+
[self addSeparatorTo:fileMenu];
86+
[self addItemTitle:@"" action:@"amyClaimFileAssociation:" key:@"" to:fileMenu].tag=TagFileAssociation;
8487

8588
NSMenu* editMenu=[self addMenuTitle:@"Edit" to:bar];
8689
[self addItemTitle:@"Undo" action:@"undo:" key:@"z" to:editMenu];
@@ -134,29 +137,6 @@ -(void)applicationWillFinishLaunching:(NSNotification*)note
134137
NSApp.mainMenu=bar;
135138
}
136139

137-
-(void)amySelectTab:(NSMenuItem*)sender
138-
{
139-
int value=sender.keyEquivalent.intValue;
140-
if(value==9)
141-
{
142-
value=INT_MAX;
143-
}
144-
145-
NSArray<NSWindow*>* windows=NSApp.keyWindow.tabbedWindows;
146-
[windows[MIN(value,windows.count)-1] makeKeyAndOrderFront:nil];
147-
}
148-
149-
-(void)amyAbout:(NSMenuItem*)sender
150-
{
151-
NSString* gitInfo=[NSString stringWithUTF8String:stringify(gitHash)];
152-
if(gitInfo.length==0)
153-
{
154-
gitInfo=@"[unknown Git commit]";
155-
}
156-
157-
alert([NSString stringWithFormat:@"Amy's meme text editor\n\n%@\n\ndo not actually use this",gitInfo]);
158-
}
159-
160140
-(BOOL)validateUserInterfaceItem:(NSObject<NSValidatedUserInterfaceItem>*)item
161141
{
162142
if([item isKindOfClass:NSMenuItem.class])
@@ -182,6 +162,24 @@ -(BOOL)validateUserInterfaceItem:(NSObject<NSValidatedUserInterfaceItem>*)item
182162
case TagTab:
183163
disabled=!self.projectMode;
184164
break;
165+
case TagFileAssociation:
166+
;
167+
Document* document=NSApp.keyWindow.windowController.document;
168+
if(document)
169+
{
170+
menuItem.title=[NSString stringWithFormat:@"Claim File Type (%@)",document.fileType];
171+
NSString* existing=((NSString*)LSCopyDefaultRoleHandlerForContentType((CFStringRef)document.fileType,kLSRolesAll)).autorelease;
172+
if([existing isEqual:NSBundle.mainBundle.bundleIdentifier])
173+
{
174+
disabled=true;
175+
}
176+
}
177+
else
178+
{
179+
menuItem.title=@"Claim File Type";
180+
disabled=true;
181+
}
182+
break;
185183
}
186184

187185
menuItem.state=checked?NSControlStateValueOn:NSControlStateValueOff;
@@ -191,7 +189,30 @@ -(BOOL)validateUserInterfaceItem:(NSObject<NSValidatedUserInterfaceItem>*)item
191189
}
192190
}
193191

194-
return [self respondsToSelector:item.action];
192+
return true;
193+
}
194+
195+
-(void)amySelectTab:(NSMenuItem*)sender
196+
{
197+
int value=sender.keyEquivalent.intValue;
198+
if(value==9)
199+
{
200+
value=INT_MAX;
201+
}
202+
203+
NSArray<NSWindow*>* windows=NSApp.keyWindow.tabbedWindows;
204+
[windows[MIN(value,windows.count)-1] makeKeyAndOrderFront:nil];
205+
}
206+
207+
-(void)amyAbout:(NSMenuItem*)sender
208+
{
209+
NSString* gitInfo=[NSString stringWithUTF8String:stringify(gitHash)];
210+
if(gitInfo.length==0)
211+
{
212+
gitInfo=@"[unknown Git commit]";
213+
}
214+
215+
alert([NSString stringWithFormat:@"Amy's meme text editor\n\n%@\n\ndo not actually use this",gitInfo]);
195216
}
196217

197218
-(void)amySettingsToggle:(NSMenuItem*)sender
@@ -215,6 +236,12 @@ -(void)amyToggleProjectMode:(NSMenuItem*)sender
215236
WindowController.syncProjectMode;
216237
}
217238

239+
-(void)amyClaimFileAssociation:(NSMenuItem*)sender
240+
{
241+
Document* document=NSApp.keyWindow.windowController.document;
242+
LSSetDefaultRoleHandlerForContentType((CFStringRef)document.fileType,kLSRolesAll,(CFStringRef)NSBundle.mainBundle.bundleIdentifier);
243+
}
244+
218245
-(void)handleFrameChange:(NSWindow*)window
219246
{
220247
if(!self.projectMode)

Document.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ -(BOOL)writeSafelyToURL:(NSURL*)url ofType:(NSString*)type forSaveOperation:(NSS
5050
return result;
5151
}
5252

53-
-(void)amySave:(id)sender
53+
-(void)amySave:(NSMenuItem*)sender
5454
{
5555
[self saveDocument:nil];
5656
}

0 commit comments

Comments
 (0)