2011-02-05 34 views
12

Có thể chạy mã AppleScript bên trong ứng dụng Cocoa không?Chạy AppleScript từ ứng dụng Cocoa

Tôi đã thử lớp NSAppleScript nhưng không thành công.

Ngoài ra, Apple có cho phép điều này không?

+1

'NSAppleScript' là một lớp học nào như vậy sẽ được cho phép bởi chủ trương App Store của Apple. –

Trả lời

11

Bạn đã đề cập xcode không lưu tập lệnh vào đường dẫn tài nguyên của ứng dụng. Đúng rồi. Bạn phải nói với xcode để làm điều này. Đầu tiên, thêm tập lệnh đã biên dịch vào dự án của bạn. Sau đó mở mục tiêu của bạn và tìm hành động "Sao chép tài nguyên gói". Kéo tập lệnh của bạn từ danh sách tệp vào hành động đó. Bằng cách này, tập lệnh của bạn được sao chép vào tài nguyên của ứng dụng một cách tự động, do đó bạn không phải thực hiện việc đó bằng tay.

Bất cứ khi nào tôi sử dụng AppleScript đã biên soạn trong ứng dụng ca cao I, 1) thêm tập lệnh vào dự án, 2) tạo lớp mới để điều khiển AppleScript, 3) sử dụng phương thức init bên dưới cho lớp và 4) kéo tập lệnh vào hành động "Sao chép tài nguyên gói" của mục tiêu.

- (id)init { 
    NSURL *scriptURL = [[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:@"applescripts" ofType:@"scpt"]]; 
    if ([self initWithURLToCompiledScript:scriptURL] != nil) { //attempt to load the script file 
    } 

    return self; 
} 
+0

Cảm ơn. Đã giải quyết được sự cố. :) –

12

Giải quyết!

XCode không lưu tệp tập lệnh của tôi vào đường dẫn tài nguyên của ứng dụng. Để chạy mã AppleScript từ Ứng dụng Cocoa, hãy sử dụng mã này:

NSString* path = [[NSBundle mainBundle] pathForResource:@"ScriptName" ofType:@"scpt"]; 
NSURL* url = [NSURL fileURLWithPath:path];NSDictionary* errors = [NSDictionary dictionary]; 
NSAppleScript* appleScript = [[NSAppleScript alloc] initWithContentsOfURL:url error:&errors]; 
[appleScript executeAndReturnError:nil]; 
[appleScript release]; 
+1

Tôi có tập lệnh táo mà từ đó tôi có thể lấy nội dung thư và chủ đề. khi tôi gọi kịch bản này từ mã trên làm thế nào tôi có thể nhận được kết quả này dữ liệu (thư cơ thể và chủ đề) của Mail? – kushalrshah

2

Từ Apple Documentation https://developer.apple.com/library/mac/technotes/tn2084/_index.html

- (IBAction)addLoginItem:(id)sender 
{ 
    NSDictionary* errorDict; 
    NSAppleEventDescriptor* returnDescriptor = NULL; 

    NSAppleScript* scriptObject = [[NSAppleScript alloc] initWithSource: 
       @"\ 
       set app_path to path to me\n\ 
       tell application \"System Events\"\n\ 
       if \"AddLoginItem\" is not in (name of every login item) then\n\ 
       make login item at end with properties {hidden:false, path:app_path}\n\ 
       end if\n\ 
       end tell"]; 

    returnDescriptor = [scriptObject executeAndReturnError: &errorDict]; 
    [scriptObject release]; 

    if (returnDescriptor != NULL) 
    { 
     // successful execution 
     if (kAENullEvent != [returnDescriptor descriptorType]) 
     { 
      // script returned an AppleScript result 
      if (cAEList == [returnDescriptor descriptorType]) 
      { 
       // result is a list of other descriptors 
      } 
      else 
      { 
       // coerce the result to the appropriate ObjC type 
      } 
     } 
    } 
    else 
    { 
     // no script result, handle error here 
    } 
} 
Các vấn đề liên quan