首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用大量内存的URL查找器

使用大量内存的URL查找器
EN

Code Review用户
提问于 2015-02-10 22:03:37
回答 1查看 129关注 0票数 3

我正试图通过Mac应用程序获取浏览器的URL。我写了一些AppleScript,并试图在可可中使用它。问题是,当我用仪器观看它时,内存在增加,在3-4个小时结束时,它几乎是20 at。

代码语言:javascript
复制
#import "AppDelegate.h"
#import <Carbon/Carbon.h>

@interface AppDelegate ()
@property (weak) IBOutlet NSWindow *window;
@end

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {

    [NSTimer scheduledTimerWithTimeInterval:1
                                     target:self
                                   selector:@selector(collect)
                                   userInfo:nil
                                    repeats:YES];
}

- (void)collect{
    [self runWithEvent];
}

- (void)runWithEvent{
    NSURL *URL = [[NSBundle mainBundle] URLForResource:@"frontmostapptitle" withExtension:@"scpt"];
    if (URL) {
        NSAppleScript *appleScript = [[NSAppleScript alloc] initWithContentsOfURL:URL error:NULL];

        NSAppleEventDescriptor *returnDescriptor = [self lookUpRunningApp];

        NSDictionary *error = nil;
        NSAppleEventDescriptor *resultEventDescriptor = [appleScript executeAppleEvent:returnDescriptor error:&error];

        if (! resultEventDescriptor) {
            NSLog(@"%s AppleScript run error = %@", __PRETTY_FUNCTION__, error);
        }
        else {
            NSLog(@"%@", [self stringForResultEventDescriptor:resultEventDescriptor]);
        }
    }
}


- (NSAppleEventDescriptor *)lookUpRunningApp{
    // target
    ProcessSerialNumber psn = {0, kCurrentProcess};
    NSAppleEventDescriptor *target = [NSAppleEventDescriptor descriptorWithDescriptorType:typeProcessSerialNumber bytes:&psn length:sizeof(ProcessSerialNumber)];

    // function
    NSAppleEventDescriptor *function = [NSAppleEventDescriptor descriptorWithString:@"LookUpRunningApp"];

    // event
    NSAppleEventDescriptor *event = [NSAppleEventDescriptor
                                     appleEventWithEventClass:kASAppleScriptSuite
                                     eventID:kASSubroutineEvent
                                     targetDescriptor:target
                                     returnID:kAutoGenerateReturnID
                                     transactionID:kAnyTransactionID];
    [event setParamDescriptor:function forKeyword:keyASSubroutineName];
    return event;
}

- (NSString *)stringForResultEventDescriptor:(NSAppleEventDescriptor *)resultEventDescriptor
{
    NSString *result = nil;

    if (resultEventDescriptor) {
        if ([resultEventDescriptor descriptorType] != kAENullEvent) {
            if ([resultEventDescriptor descriptorType] == kTXNUnicodeTextData) {
                result = [resultEventDescriptor stringValue];
            }
        }
    }
    return result;
}

@end
EN

回答 1

Code Review用户

发布于 2015-02-13 00:56:35

代码语言:javascript
复制
- (NSString *)stringForResultEventDescriptor:(NSAppleEventDescriptor *)resultEventDescriptor
{
    NSString *result = nil;

    if (resultEventDescriptor) {
        if ([resultEventDescriptor descriptorType] != kAENullEvent) {
            if ([resultEventDescriptor descriptorType] == kTXNUnicodeTextData) {
                result = [resultEventDescriptor stringValue];
            }
        }
    }
    return result;
}

可以更简单地重写此方法:

代码语言:javascript
复制
- (NSString *)stringForResultEventDescriptor:(NSAppleEventDescriptor *)resultEventDescriptor {
    return (resultEventDescriptor.descriptorType == kTXNUnicodeTextData) ? resultEventDescriptor.stringValue : nil;
}
代码语言:javascript
复制
- (void)collect{
    [self runWithEvent];
}

这种方法似乎完全没有必要。为什么计时器不直接调用runWithEvent呢?

票数 2
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/80206

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档