2011-08-05 23 views
1

我想知道如何從傳遞參數的可可應用程序執行applescript。 我已經看到在stackoverflow的其他問題中執行沒有參數的applescripts是多麼容易,但是使用NSAppleScript類,其中,我沒有看到沒有解決我的問題的方法。有人有什麼主意嗎。使用params從Cocoa App執行Applescript

我想可可碼與鄰此外殼相同的效果:

osascript teste.applescript "snow:Users:MyUser:Desktop:MyFolder" "snow:Users:MyUser:Desktop:Example:" 

所以它可以運行該AppleScript的。

on run argv 

    set source to (item 1 of argv) 

    set destiny to (item 2 of argv) 

    tell application "Finder" to make new alias file at destiny to source 
    0 

end run 

任何幫助表示讚賞。提前致謝。

回答

1

我對AppleScript並不是太熟悉,但我似乎記得他們嚴重基於(相當蹩腳的)Apple Events機制,該機制可追溯到56k調制解調器是最酷的Gadget你的房子。

因此,我猜你正在尋找executeAppleEvent:error:這是NSAppleScript的一部分。也許你可以在NSAppleEventDescriptor的實例中找到一些關於如何封裝執行參數的信息,你必須使用這個函數。

4

看看我的GitHub存儲庫,我有一個NSAppleEventDescriptor類別,它使創建NSAppleEventDescriptor以調用具有參數的不同AppleScript過程以及對來自和來自多個AppleScript類型的強制轉換更容易。

NSAppleEventDescriptor-NDCoercion

3

我發現更容易執行這塊代碼。我從here獲取了一個代碼並將其修改爲我的目的。

- (BOOL) executeScriptWithPath:(NSString*)path function:(NSString*)functionName andArguments:(NSArray*)scriptArgumentArray 
{ 
    BOOL executionSucceed = NO; 

    NSAppleScript   * appleScript; 
    NSAppleEventDescriptor * thisApplication, *containerEvent; 
    NSURL     * pathURL = [NSURL fileURLWithPath:path]; 

    NSDictionary * appleScriptCreationError = nil; 
    appleScript = [[NSAppleScript alloc] initWithContentsOfURL:pathURL error:&appleScriptCreationError]; 

    if (appleScriptCreationError) 
    { 
     NSLog([NSString stringWithFormat:@"Could not instantiate applescript %@",appleScriptCreationError]); 
    } 
    else 
    { 
     if (functionName && [functionName length]) 
     { 
      /* If we have a functionName (and potentially arguments), we build 
      * an NSAppleEvent to execute the script. */ 

      //Get a descriptor for ourself 
      int pid = [[NSProcessInfo processInfo] processIdentifier]; 
      thisApplication = [NSAppleEventDescriptor descriptorWithDescriptorType:typeKernelProcessID 
                      bytes:&pid 
                      length:sizeof(pid)]; 

      //Create the container event 

      //We need these constants from the Carbon OpenScripting framework, but we don't actually need Carbon.framework... 
      #define kASAppleScriptSuite 'ascr' 
      #define kASSubroutineEvent 'psbr' 
      #define keyASSubroutineName 'snam' 
      containerEvent = [NSAppleEventDescriptor appleEventWithEventClass:kASAppleScriptSuite 
                     eventID:kASSubroutineEvent 
                  targetDescriptor:thisApplication 
                    returnID:kAutoGenerateReturnID 
                   transactionID:kAnyTransactionID]; 

      //Set the target function 
      [containerEvent setParamDescriptor:[NSAppleEventDescriptor descriptorWithString:functionName] 
            forKeyword:keyASSubroutineName]; 

      //Pass arguments - arguments is expecting an NSArray with only NSString objects 
      if ([scriptArgumentArray count]) 
      { 
       NSAppleEventDescriptor *arguments = [[NSAppleEventDescriptor alloc] initListDescriptor]; 
       NSString    *object; 

       for (object in scriptArgumentArray) { 
        [arguments insertDescriptor:[NSAppleEventDescriptor descriptorWithString:object] 
             atIndex:([arguments numberOfItems] + 1)]; //This +1 seems wrong... but it's not 
       } 

       [containerEvent setParamDescriptor:arguments forKeyword:keyDirectObject]; 
       [arguments release]; 
      } 

      //Execute the event 
      NSDictionary * executionError = nil; 
      NSAppleEventDescriptor * result = [appleScript executeAppleEvent:containerEvent error:&executionError]; 
      if (executionError != nil) 
      { 
       NSLog([NSString stringWithFormat:@"error while executing script. Error %@",executionError]); 

      } 
      else 
      { 
       NSLog(@"Script execution has succeed. Result(%@)",result);   
       executionSucceed = YES; 
      } 
     } 
     else 
     { 
      NSDictionary * executionError = nil; 
      NSAppleEventDescriptor * result = [appleScript executeAndReturnError:&executionError]; 

      if (executionError != nil) 
      { 
       NSLog([NSString stringWithFormat:@"error while executing script. Error %@",executionError]); 
      } 
      else 
      { 
       NSLog(@"Script execution has succeed. Result(%@)",result); 
       executionSucceed = YES; 
      } 
     } 
    } 

    [appleScript release]; 

    return executionSucceed; 
} 
相關問題