從問題提取的答案:
我明白了!
我可以使用,我定義我的command
爲以下的result = MacScript(command)
函數來調用我的Python腳本:
command = "do shell script """ & pyPath & "python " & getURLpath & "getURL.py --formula '" _
& Latex_Str & "' --fontsize " & Font_Size & " " & WebAdd & """"
我的Python腳本被稱爲getURL.py
,以及處理該請求基於可選參數的服務器--formula
和--fontsize
分別爲Latex_Str
和Font_Size
,以及服務器的網址爲WebAdd
。我還爲我的Python腳本添加了一些功能來處理傳遞代理設置。通過MacScript
的上述命令返回Python腳本的stdout,這是從服務器返回的。該Python腳本如下:
# Import the required libraries
from urllib import urlencode
from urllib2 import Request, urlopen, URLError, ProxyHandler, build_opener, install_opener
import argparse
# Set up our argument parser
parser = argparse.ArgumentParser(description='Sends LaTeX string to web server and returns meta data used by LaTeX in Word project')
parser.add_argument('webAddr', type=str, help='Web address of LaTeX in Word server')
parser.add_argument('--formula', metavar='FRML', type=str, help='A LaTeX formula string')
parser.add_argument('--fontsize', metavar='SIZE', type=int, default=10, help='Integer representing font size (can be 10, 11, or 12. Default 10)')
parser.add_argument('--proxServ', metavar='SERV', type=str, help='Web address of proxy server, i.e. http://proxy.server.com:80')
parser.add_argument('--proxType', metavar='TYPE', type=str, default='http', help='Type of proxy server, i.e. http')
# Get the arguments from the parser
args = parser.parse_args()
# Define formula string if input
if args.formula:
values = {'formula': str(args.fontsize) + '.' + args.formula} # generate formula from args
else:
values = {}
# Define proxy settings if proxy server is input.
if args.proxServ: # set up the proxy server support
proxySupport = ProxyHandler({args.proxType: args.proxServ})
opener = build_opener(proxySupport)
install_opener(opener)
# Set up the data object
data = urlencode(values)
data = data.encode('utf-8')
# Send request to the server and receive response, with error handling!
try:
req = Request(args.webAddr, data)
# Read the response and print to a file
response = urlopen(req)
print response.read()
except URLError, e:
if hasattr(e, 'reason'): # URL error case
# a tuple containing error code and text error message
print 'Error: Failed to reach a server.'
print 'Reason: ', e.reason
elif hasattr(e, 'code'): # HTTP error case
# HTTP error code, see section 10 of RFC 2616 for details
print 'Error: The server could not fulfill the request.'
print 'Error code: ', e.code
# print e.read()
如果有人好奇,完整的代碼將可在project page一次我做固定幾個額外的錯誤並對其進行測試。 (工作)Windows版本已經在那裏。
我一直在努力實現這一點。我仍然有一個錯誤,但我認爲這可能會長期完成這項工作。如果我無法弄清楚是什麼導致了我的錯誤,我會很快回復這裏,提供更多信息。謝謝! – Engineero