4

我设法将 Mac Os Finder 中的图标覆盖(如 Dropbox)与 SIMBL 插件集成在一起!我使用 swizzle 方法来覆盖一些查找器功能。

这是我的调酒方法:

void PluginSwizzleInstanceMethod(Class cls, SEL oldSel, SEL newSel) 
{
    Method mnew = class_getInstanceMethod(cls, newSel);
    Method mold = class_getInstanceMethod(cls, oldSel);
    method_exchangeImplementations(mnew, mold);
}

所以目前我像这样覆盖drawWithFrame方法:

Class cls = NSClassFromString(@"TIconAndTextCell");
SEL oldSel = @selector(drawWithFrame:inView:);
SEL newSel = @selector(PluginDrawWithFrame:inView:);
PluginSwizzleInstanceMethod(cls, oldSel, newSel);

我的所有图标都显示但不在我的桌面或将项目显示为图标的视图上...为什么?

谢谢你 ,

4

2 回答 2

2

我只是设法为桌面做一些事情,所以我会发布以防它仍然与某人相关:

如您所见,Finder 在桌面上绘制图标的方式不同。
负责此的一类是TDesktopIcon
例如,可以通过覆盖 drawIconInContext: 方法来更改图标,并将 thumbnailImage 属性设置为他喜欢的任何 NSImage*。

- (void) FO_drawIconInContext:(struct CGContext *)arg1 {
    NSImage *image;
    // set image... 
    [(TDesktopIcon*)self setThumbnailImage:image];
    [self FO_drawIconInContext:arg1];
}

获取每个项目的 url 并不像使用 TIconAndTextCell 或 IKImageBrowserCell 那样直接。

为了获取 URL,您需要覆盖 TDesktopViewController 的 prepareToDrawNode:方法。您从 arg1 获取 URL,如下所示:

- (void) FO_prepareToDrawNode:(const struct TFENode *)arg1 {
    struct OpaqueNodeRef *opr = arg1->fNodeRef;  
    struct FINode *fiNode = [FINode nodeFromNodeRef:opr];  
    NSURL *url = [fiNode previewItemURL];  
    // save url somewhere (I use NSUserDefaults) 
    [self FO_prepareToDrawNode:arg1];  
}  

也许有更好的方法,但这就是我想出来的......

于 2012-11-15T21:28:51.657 回答
2

知道了!(此代码仅适用于 10.7)我遇到了问题,因为在将最终图像渲染为缩略图之前绘制了图标....所以我得到了这些工件,其中原始图标丢失了....覆盖总是在那里......这是我错误的绘制操作顺序......!

我什至有目标节点的名称...您需要 TFENodeHelper 类将 TFENode 转换为文件路径...

//
//  DesktopViewIconOverlay.h
//  FinderIconOverlayExample
//
//  Created by Orbitus007 on 2/20/13.
//
//

#import <Foundation/Foundation.h>
#import <Quartz/Quartz.h>
#import "Finder.h"

@interface DesktopViewIconOverlay : NSObject

+ (void)pluginLoad;
- (void) FO_prepareToDrawNode:(const struct TFENode *)arg1;
- (void) FO_drawIconInContext:(struct CGContext *)arg1;

@end

//
//  DesktopViewIconOverlay.m
//  FinderIconOverlayExample
//
//  Created by Orbitus007 on 2/20/13.
//
//

#import "DesktopViewIconOverlay.h"
#import "FinderIconOverlayExample.h"
#include <objc/objc.h>
#include <objc/runtime.h>
#import <Quartz/Quartz.h>
#import "TFENodeHelper.h"

static TFENodeHelper *gNodeHelper;





@implementation DesktopViewIconOverlay

+ (void)pluginLoad
{



    // Create helper object
    gNodeHelper = [[TFENodeHelper alloc] init];
    if (gNodeHelper == nil) {
        NSLog(@"Failed to instantiate 'TFENodeHelper' class");
        return;
    }


    Method old, new;
    Class self_class = [self class];
    Class finder_class = [objc_getClass("TDesktopIcon") class];

    class_addMethod(finder_class, @selector(FO_drawIconInContext:),
                    class_getMethodImplementation(self_class, @selector(FO_drawIconInContext:)),"v@:@");

    old = class_getInstanceMethod(finder_class, @selector(drawIconInContext:));
    new = class_getInstanceMethod(finder_class, @selector(FO_drawIconInContext:));
    method_exchangeImplementations(old, new);


    finder_class = [objc_getClass("TDesktopViewController") class];


    class_addMethod(finder_class, @selector(FO_prepareToDrawNode:),
                    class_getMethodImplementation(self_class, @selector(FO_prepareToDrawNode:)),"v@:@");

    old = class_getInstanceMethod(finder_class, @selector(prepareToDrawNode:));
    new = class_getInstanceMethod(finder_class, @selector(FO_prepareToDrawNode:));
    method_exchangeImplementations(old, new);

}


- (void) FO_prepareToDrawNode:(const struct TFENode *)arg1 {

    NSString *path = [gNodeHelper pathForNode:arg1];
    NSLog(@"Path = %@", path);
    [[NSUserDefaults standardUserDefaults] setValue:path forKey:@"TDesktopIconURL"];

    //struct OpaqueNodeRef *opr = arg1->fNodeRef;

    //NSLog(@"path = %@", [[FINode nodeWithFENode:arg1] fullPath]);

    //id fiNode = [FINode nodeFromNodeRef:opr];
    //NSURL *url = [fiNode previewItemURL];
    //[[NSUserDefaults standardUserDefaults] setValue:[url path] forKey:@"TDesktopIconURL"];
    //NSLog(@"sending ", arg1->fNodeRef);
    // save url somewhere (I use NSUserDefaults)
    [self FO_prepareToDrawNode:arg1];
}



- (void) FO_drawIconInContext:(struct CGContext *)arg1
{
    [self FO_drawIconInContext:arg1];

    NSString *iconPath = [[NSUserDefaults standardUserDefaults] valueForKey:@"TDesktopIconURL"];
    NSLog(@"recieved %@", iconPath);

    if (![iconPath isEqualToString:@"/Users/h0xff/Desktop/Restart Finder.app"])
        return;

    NSString *imagePath = @"/Users/h0xff/Desktop/020513_icons/128_synced_green.png";
    NSImage *overlay = [[NSImage alloc] initWithContentsOfFile:imagePath];

    NSImage *mainImage = [(TDesktopIcon*)self thumbnailImage];

    float width = 256.0;
    float height = 256.0;

    NSImage *finalImage = [[NSImage alloc] initWithSize:NSMakeSize(width, height)];

    [finalImage lockFocus];

    // draw the base image
    [mainImage drawInRect:NSMakeRect(0, 0, width, height)
                 fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];

    // draw the overlay image at some offset point
    [overlay drawInRect:NSMakeRect(0, 0, [overlay size].width/1.5, [overlay size].height/1.5)
               fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];

    [finalImage unlockFocus];


    // set image...
    [(TDesktopIcon*)self setThumbnailImage:finalImage];
    [self FO_drawIconInContext:arg1];
}

@end

此代码源自 Alexey Zhuchkov 和许多其他人......如果您有任何问题,请提出问题!

//
//  TFENodeHelper.h
//  FinderMenu
//
//  Helper class to get full path from TFENode
//
//  Created by Alexey Zhuchkov on 11/4/12.
//  Copyright (c) 2012 InfiniteLabs. All rights reserved.
//

#import <Foundation/Foundation.h>

// Forward declarations
struct TFENode;

typedef enum {
    TFENodeCtorTypeUnknown,
    TFENodeCtorTypeNodeWithFENode,  // nodeWithFENode
    TFENodeCtorTypeNodeFromNodeRef,  // nodeFromNodeRef
} TFENodeCtorType;

typedef enum {
    TFENodePathMethodTypeUnknown,
    TFENodePathMethodTypeFullPath,  // fullPath
    TFENodePathMethodTypePreviewItemURL,  // previewItemURL
} TFENodePathMethodType;

@interface TFENodeHelper : NSObject {
    @private
    Class _class;
    TFENodeCtorType _ctorType;
    TFENodePathMethodType _pathMethodType;
}

- (NSString *)pathForNode:(const struct TFENode *)node;
@end


//
//  TFENodeHelper.m
//  FinderMenu
//
//  Created by Alexey Zhuchkov on 11/4/12.
//  Copyright (c) 2012 InfiniteLabs. All rights reserved.
//

#import "TFENodeHelper.h"

#import <objc/runtime.h>

#import "Finder.h"

@implementation TFENodeHelper

- (id)init {
    self = [super init];
    if (self) {
        _class = NSClassFromString(@"FINode");
        _ctorType = TFENodeCtorTypeUnknown;
        _pathMethodType = TFENodePathMethodTypeUnknown;

        if (_class) {
            if (class_getClassMethod(_class, @selector(nodeWithFENode:))) {
                _ctorType = TFENodeCtorTypeNodeWithFENode;
            } else if (class_getClassMethod(_class, @selector(nodeFromNodeRef:))) {
                _ctorType = TFENodeCtorTypeNodeFromNodeRef;
            }

            if (class_getInstanceMethod(_class, @selector(fullPath))) {
                _pathMethodType = TFENodePathMethodTypeFullPath;
            } else if (class_getInstanceMethod(_class, @selector(previewItemURL))) {
                _pathMethodType = TFENodePathMethodTypePreviewItemURL;
            }
        }

        if (!_class
            || (_ctorType == TFENodePathMethodTypeUnknown)
            || (_pathMethodType == TFENodePathMethodTypeUnknown)) {

            [self release];
            return nil;
        }
    }
    return self;
}

- (NSString *)pathForNode:(const struct TFENode *)node {
    FINode *fiNode = nil;
    NSString *path = nil;
    switch (_ctorType) {
        case TFENodeCtorTypeNodeWithFENode:
            fiNode = [_class nodeWithFENode:node];
            break;
        case TFENodeCtorTypeNodeFromNodeRef:
            fiNode = [_class nodeFromNodeRef:node->fNodeRef];
            break;
        default:
            break;
    }

    NSURL *url;
    if (fiNode) {
        switch (_pathMethodType) {
            case TFENodePathMethodTypeFullPath:
                path = [fiNode fullPath];
                break;
            case TFENodePathMethodTypePreviewItemURL:
                url = [fiNode previewItemURL];
                path = [url path];
            default:
                break;
        }
    }

    return path;
}

@end
于 2013-02-21T14:20:01.877 回答