2011-03-31 54 views
1

我有一個調用python腳本的Adobe Air程序。我不認爲動作3.0正在進行適當的調用。代碼:從Actionscript調用python

 var file:File; 
     var args:Vector.<String> = new Vector.<String>; 

      file = new File().resolvePath("/usr/bin/python"); 

      var pyScript:File; 
      pyScript = File.applicationDirectory.resolvePath("python/mac/merge.py"); 

      var tempOutPath:String = File.applicationStorageDirectory.resolvePath("out.pdf").nativePath; 
      args.push(pyScript.nativePath, "-w", "-o", tempOutPath, "-i"); 

      for(var x:int; x < numFilesToProcess; x++){ 

       var pdfPath:String = File(pdfs.getItemAt(x)).nativePath; 

       args.push(pdfPath); 

      } 

      callNative(file, args); 

在終端(蘋果機),以下工作正常:

python merge.py -w -o out.pdf -i file1.pdf file2.pdf 

的args.push(pyScript.native ....行是問題的一個我會很感激一些幫助

回答

6

我遇到過使用Air的類似問題我需要從Air應用程序打印到收據打印機我無法從應用程序本身執行此操作,因此我使用了python RPC服務器來完成我通過http與它交談。下面是一個簡化版本給你一個想法:

蟒蛇RPC服務器

from SimpleXMLRPCServer import SimpleXMLRPCServer 
from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler 

class RequestHandler(SimpleXMLRPCRequestHandler): 
    rpc_paths = ('/','/RPC2') 

server = SimpleXMLRPCServer(('localhost', 8123), requestHandler=RequestHandler) 

def myService(arg0, arg1): 
    #do the stuff 
    return 0 

server.register_function(myService) 
server.serve_forever() 

在空氣中我創建呼叫作爲一個XML字符串,使我的請求。 我沒有顯示所有的細節,因爲我使用JavaScript不動作,所以請把它當作僞代碼。

// XML as a string 
// possibly create the XML and toXMLString() it? 
var data:String = ' 
<?xml version="1.0"?> 
<methodCall> 
    <methodName>myService</methodName> 
    <params> 
     <param> 
      <string>file1.pdf</string> 
     </param> 
     <param> 
      <string>file2.pdf</string> 
     </param> 
    </params> 
</methodCall>'; 

var req:URLRequest = new URLRequest('localhost:8123'); 
rec.method = 'POST'; 
rec.data = data; 
var loader:URLLoader = new URLLoader(req); 
//etc