gpt4 book ai didi

objective-c - NSApplescript 不会接受 initWithContentsOfURL

转载 作者:行者123 更新时间:2023-12-03 17:35:05 31 4
gpt4 key购买 nike

我是新手,所以请耐心等待。我的问题出在以下代码中:

AppleScriptController.h

#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>
@interface AppleScriptController : NSObject
-(void)test;
@end

AppleScriptController.m

#import "AppleScriptController.h"    
@implementation AppleScriptController

-(void)test{
NSString *path = [[NSBundle mainBundle] pathForResource:@"beep"
ofType:@"scpt"];
NSAppleScript *script2 = [[NSAppleScript alloc]
initWithContentsOfURL:[NSURL fileURLWithPath:path]
error:nil];
[script2 executeAndReturnError:nil];
}
@end

(beep.scpt 的副本位于支持文件中)

main.m

#import <Foundation/Foundation.h>
#import "AppleScriptController.h"

AppleScriptController *myScript = [[AppleScriptController alloc] init];
[myScript test];

当我运行它时,我收到以下错误消息:线程1:程序收到信号:“SIGABRT”

NSAppleScript *script2 = [[NSAppleScript alloc]
initWithContentsOfURL:[NSURL fileURLWithPath:path]
error:nil]; Thread 1: Program received signal: "SIGABRT"

并得到以下错误代码:

2013-01-02 23:09:09.269 Test8[1627:707] * Terminating app

due to uncaught exception 'NSInvalidArgumentException',

reason: '* -[NSURL initFileURLWithPath:]: nil string

parameter'

* First throw call stack:

(

0 CoreFoundation 0x00007fff8db7df56 __exceptionPreprocess + 198

1 libobjc.A.dylib 0x00007fff95dfbd5e objc_exception_throw + 43

2 CoreFoundation 0x00007fff8db7dd8a +[NSException raise:format:arguments:] + 106

3 CoreFoundation 0x00007fff8db7dd14 +[NSException raise:format:] + 116

4 Foundation 0x00007fff8fd6ae40 -[NSURL(NSURL) initFileURLWithPath:] + 78

5 Foundation 0x00007fff8fd6add9 +[NSURL(NSURL) fileURLWithPath:] + 47

6 Test8 0x0000000100000d9d -[AppleScriptController test] + 205

7 Test8 0x0000000100000cac main + 108

8 Test8 0x0000000100000c34 start + 52

9 ??? 0x0000000000000001 0x0 + 1

)

terminate called throwing an exceptionsharedlibrary apply-load-rules all

Current language: auto; currently objective-c

(gdb)

当我将 m 文件更改为 initWithSource 时,程序运行正常:

NSAppleScript *script1 = [[NSAppleScript alloc] initWithSource:@"beep 10"];
[script1 executeAndReturnError:nil];

我在 OS X 10.7.5 上运行 XCode 4.2.1

请告诉我哪里出错了。我确信这是某种新手错误,但我似乎无法找到它。

非常感谢任何建议!

最佳答案

来自错误代码:

reason: '* -[NSURL initFileURLWithPath:]: nil string

所以这里:

NSString *path = [[NSBundle mainBundle] pathForResource:@"beep" 
ofType:@"scpt"];
NSAppleScript *script2 = [[NSAppleScript alloc]
initWithContentsOfURL:[NSURL fileURLWithPath:path]
error:nil];

路径为零。因此,该文件很可能不在 bundle 中。将其添加为项目的目标。

如何做

例如,最后一天我需要在 bundle 中添加鲨鱼的图像,具体方法如下:

enter image description here

关于objective-c - NSApplescript 不会接受 initWithContentsOfURL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14130213/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com