我多次通过“泄漏”检查内存,发现总有一个漏洞。我可以请您帮个忙吗?
这里的代码:
NSAppleScript* startFinder = [[NSAppleScript alloc] initWithSource:
@"tell application \"Finder\"\n"
@" delay 1\n"
@" try\n"
@" «event GKKJload»\n"
@" on error msg number num\n"
@" display dialog \"another try\" buttons{\"i see\"} default button 1 with icon caution with title \"aaa\"\n"
@" end try\n"
@"end tell"];
[startFinder executeAndReturnError:nil];
[startFinder release];先谢谢大家。
发布于 2013-08-06 20:02:32
这就是我所做的事。我开发了一个模型视图控制器,用于编辑、编译和运行脚本。
在你这种情况下,你只会使用模型。在我的ScriptModel类中,它是另一层抽象,但我认为它很好。我可以更改对所有AppleScripts的调用,方法是将ScriptModel实现更改为NSAppleScript或OSAScript。
- (IBAction)test01:(id)sender
{
ScriptModel* startFinder = [[ScriptModel alloc] initWithSource:
@"tell application \"Finder\"\n"
@" delay 1\n"
@" try\n"
@" «event GKKJload»\n"
@" on error msg number num\n"
@" display dialog \"another try\" buttons{\"i see\"} default button 1 with icon caution with title \"aaa\"\n"
@" end try\n"
@"end tell"];
ScriptResult *scriptResult = [startFinder run];
}我正在使用ARC,所以我相信我的代码会正常工作。我不能叫ARC的释放方法。
您需要考虑的另一个选择是既不使用NSAppleScript而不是OSAScript,也根本不使用AppleScript源,而只是使用Objective调用而不是使用脚本桥:
/*
Scripting Bridge for Objective-C
http://www.macosxautomation.com/applescript/features/scriptingbridge.html
Although the Objective-C language has existing mechanisms for sending Apple Events, the new Scripting Bridge architecture greatly simplifies the coding necessary to query and control scriptable applications.
*/
#import <Foundation/Foundation.h>
#import <ScriptingBridge/ScriptingBridge.h>
#import "iTunes.h"
int main()
{
iTunesApplication *iTunes = [SBApplication applicationWithBundleIdentifier:@"com.apple.iTunes"];
NSLog(iTunes.currentTrack.name);
}我认为在Objective中运行AppleScripts只有当你想打开脚本给用户定制应用程序时才有意义。我正在考虑将脚本桥用于其他一切,而不是将AppleScripts嵌入到目标-C代码中。
发布于 2013-08-06 18:41:07
NSAppleScript因内存泄漏而臭名昭著。导入OSAKit框架并使用OSAScript代替NSAppleScript (代码的其余部分可以保持不变)。
https://stackoverflow.com/questions/18070205
复制相似问题