2011-08-05 5 views
1

パラメータを渡すココアアプリケーションからAppleScriptを実行する方法を知りたいと思います。 ここで私は問題を解決する方法が見当たらないNSAppleScriptクラスを使用していますが、ここではstackoverflowの他の質問ではパラメータなしでAppleScriptを実行するのがどれほど簡単かを見てきました。誰にも分かりません。CocoaアプリケーションからApplescriptをパラメータで実行する

私は、このシェルOと同じ効果とココアのコードを希望:

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とあまり慣れていないんだけど、私は、彼らが頻繁に(かなり安っぽい)アップルイベントバック56Kモデムで一番クールなガジェットだった日にさかのぼりメカニズムに基づいていることを覚えているようですお宅。

したがって、私はNSAppleScriptの一部であるexecuteAppleEvent:error:を探していると思います。実行引数をカプセル化する方法については、NSAppleEventDescriptorのインスタンスにこの関数と一緒に渡す必要がある情報があります。

4

私のGitHubリポジトリを見てください。私はNSAppleEventDescriptorのカテゴリを用意しています。NSAppleEventDescriptorを作成して、引数のある別の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; 
} 
関連する問題