2009-06-16 2 views
0

첨부 파일이있는 이메일을 보내는 짧은 applescript를 만들었습니다. 이제이 스크립트를 코코아 애플리케이션에 통합하려고합니다. 인터넷에서 찾은 다음 코드를 시도했습니다.코코아에서 applescript 통합

NSAppleScript *mailScript; 
NSString *scriptString= [NSString stringWithFormat:@"the applescript"]; 
mailScript = [[NSAppleScript alloc] initWithSource:scriptString]; 
[mailScript executeAndReturnError:nil]; 
[mailScript release]; 

이 코드는 작동하지 않습니다. 나는 코코아에서 완전한 초보자이며 도움을 사용할 수 있습니다.

업데이트 : 전자 메일이 생성되었습니다. 첨부 파일을 추가 할 때 applescript가 멈춘 것처럼 보입니다. scripteditor에서 runned 할 때 applescript가 완벽하게 작동합니다. 어떤 단서?

감사합니다.

+1

FYI : Adium은 NSAppleScript 누출 메모리에 문제가있었습니다. OSAKit 프레임 워크에서 OSAScript를 사용해보십시오. 프레임 워크는 Mac OS X과 함께 제공되며 API는 거의 동일합니다. –

답변

1

코드는 괜찮아 보입니다. AppleScript에 오류가있는 것 같습니다.

는 다음과 같은 시도 :

NSAppleScript *mailScript; 
NSAppleEventDescriptor *resultDescriptor; 
NSString *scriptString= [NSString stringWithFormat:@"the applescript"]; 
mailScript = [[NSAppleScript alloc] initWithSource:scriptString]; 
resultDescriptor = [mailScript executeAndReturnError:nil]; 
NSLog([resultDescriptor stringValue]); 
[mailScript release]; 

NSLog가 출력 콘솔에 오류를 설명하는 문자열. 이렇게하면 문제를 찾는 데 도움이됩니다.

+1

- [NSAppleScript executeAndReturnError :]은 오류가 발생하면 nil을 반환하도록 문서화됩니다. –

+0

좋은 지적은 ... 내가 생각하지 못한 것입니다. ;) 당신의 대답은 내가 말했던 것보다 더 비슷합니다. 그래서 +1. – Naaff

5

-[NSAppleScript executeAndReturnError:]에서 오류를 무시하지 않은 경우 오류가 무엇인가요? 그 내용이 무엇이 잘못되었는지에 관해 당신에게 이야기합니까? 그것은 당신의 응용 프로그램에 올바른 장소에 도착하는 시간이 걸리고 그냥 AppleScript를 검사 할 경우

NSDictionary *dict = nil; 
if ([mailScript executeAndReturnError: &dict] == nil) 
{ 
    //ooh, it went wrong, look at dict 
} 
else 
{ 
    // well, that worked! 
} 
1

, 당신은 osascript 명령을 통해 터미널에서 실행하고 결과를 볼 수 있습니다

osascript -e 'applescript here'; 
0

SBApplication은 작동해야하지만 이전에는 사용하지 않았습니다. @cocoadevcentral에 따르면

:

The SBApplication class provides a mechanism enabling an Objective-C program to send Apple events to a scriptable application and receive Apple events in response. It thereby makes it possible for that program to control the application and exchange data with it. Scripting Bridge works by bridging data types between Apple event descriptors and Cocoa objects.

Although SBApplication includes methods that manually send and process Apple events, you should never have to call these methods directly. Instead, subclasses of SBApplication implement application-specific methods that handle the sending of Apple events automatically.

For example, if you wanted to get the current iTunes track, you can simply use the currentTrack method of the dynamically defined subclass for the iTunes application—which handles the details of sending the Apple event for you—rather than figuring out the more complicated, low-level alternative:

[iTunes propertyWithCode:'pTrk']; 

If you do need to send Apple events manually, consider using the NSAppleEventDescriptor class.

희망하는 데 도움이 :

SBApplication: use to make cross-application scripting calls with Objective-C instead of AppleScript. Ex: get current iTunes track.

다음은이 문서에서 발췌 한 것입니다!