incanus / snarf

Snarf allows you to drag an ICNS file either onto its main window or onto its dock icon and will instantly update to use the dragged file as its new icon.

snarf / TriggerImageView.m
100644 55 lines (43 sloc) 1.243 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//
// TriggerImageView.m
// Snarf
//
// Created by Justin R. Miller on 6/13/07.
// Copyright 2007 Code Sorcery Workshop. All rights reserved.
//
 
#import "TriggerImageView.h"
 
@implementation TriggerImageView
 
- (id)init
{
    self = [super init];
    
    if (self != nil)
    {
        [self registerForDraggedTypes:[NSArray arrayWithObject:NSFilenamesPboardType]];
    }
    
    return self;
}
 
- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
{
    //
    // Handle dragging entry and validate if an application or not.
    //
    
    NSString *path = [[[sender draggingPasteboard] propertyListForType:NSFilenamesPboardType] objectAtIndex:0];
        
    if ([[[sender draggingPasteboard] propertyListForType:NSFilenamesPboardType] count] == 1 && [path hasSuffix:@".icns"])
    {
        return NSDragOperationCopy;
    }
    else
    {
        return NSDragOperationNone;
    }
}
 
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
{
    //
    // Handle drag release and process if an application.
    //
    
    NSString *path = [[[sender draggingPasteboard] propertyListForType:NSFilenamesPboardType] objectAtIndex:0];
    [[NSApp delegate] application:NSApp openFile:path];
        
    return YES;
}
 
@end