从NSTimer调用的方法获取结果(obj-c)

时间:2016-03-20 13:25:26

标签: objective-c nstimer

根据我的结论,NSTimer会调用您的方法一次,尽管重复多少次意味着您无法从方法中获得更新结果?

如何使用NSTimer实现以下功能?

1)永远使用applescript循环获取某些窗口标题

- (void) timer {
if (!_timer){

    //Create timer , timer only gets called once and returns once?
    _timer = [NSTimer scheduledTimerWithTimeInterval: 1.0f
                                               target: self
                                            selector: @selector(getWindowNames:)
                                             userInfo: nil repeats:YES];

    [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSDefaultRunLoopMode];
    [[NSRunLoop currentRunLoop] run];
    }
}

2)使用applescript抓住标题窗口

//Get the names of all running windows
- (void) getWindowNames : (NSTimer*) timer{

NSDictionary *error = nil;

//AppleScript to get all running window titles
appleScriptFindWindows = [[NSAppleScript alloc] initWithSource:
                                        @"tell application \"System Events\" to get the title of every window of process \"MyProcess\" whose name contains \"Eagle\""];

//Execute and get the result of the OSAScript
result = [appleScriptFindWindows executeAndReturnError:&error];

//Convert to the result to a string
NSString *stringResult = [NSString stringWithFormat:@"%@", result];

//I would like to be able to store a comparison variable here such as....
//NSString *previousResult = stringResult

//If (![previousResult isEqualToString: stringResult]) ....
//Call a method or return to do stuff 

当我搜索每1秒找到新的窗口标题(谁有不同的标识符)我需要能够比较字符串,每次定时器调用getWindowNames,数据被删除(我相信?)所以有什么东西例如可以从我的类中的所有方法访问的“类”或“全局类”变量,并保持我可用于比较的相同值?

如果是这样,或者即使不是,那么理解如何正确构建我想要做的事情的一些帮助将不胜感激。

0 个答案:

没有答案
相关问题