Mac Office 2011 VBA - 调用服务器脚本

发布于 2024-09-30 17:12:04 字数 466 浏览 4 评论 0原文

我正在将一个大型 VBA 项目从 Windows 移植到新的 Mac Word 2011。实际上进展非常顺利...几乎所有代码都可以运行。

我的代码需要调用服务器上的脚本。在Windows上,我调用系统函数InternetOpenUrl来调用脚本,并调用InternetReadFile来读取脚本返回的结果。例如,我调用如下脚本:

  "http://www.mysite.com/cgi-bin/myscript.pl?param1=Hello&param2=World

它返回一个类似“Success”的字符串

在 Mac 上执行等效操作的最佳方法是什么?使用 Applescript(通过 vba MacScript 函数)是答案吗?我这样做是为了显示文件选择器对话框,但我找不到调用在线脚本的 applescript 的样子。或者有更好/更快的方法来做到这一点?

提前致谢, 加里

I'm porting a large VBA project over from Windows to the new Mac Word 2011. It's actually going very well...almost all of the code is working.

My code needs to call scripts on my server. On Windows, I call the system function InternetOpenUrl to call a script and InternetReadFile to read the results returned by the script. For example, I call a script like:

  "http://www.mysite.com/cgi-bin/myscript.pl?param1=Hello¶m2=World

and it returns a string like "Success"

What's the best way to do the equivalent on the Mac? Is using Applescript (via the vba MacScript function) the answer? I do that to display the file chooser dialog, but I can't find what the applescript to call an online script would look like. Or is there a better/faster way to do this?

Thanks in advance,
gary

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

静谧幽蓝 2024-10-07 17:12:04

您可以尝试 URL Access Scripting 库,它是 curl 的前端,或者通过浏览器转到脚本并通过那里读取文本。

You can try the URL Access Scripting library, which is a front-end for curl, or go to the script via a browser and reading the text through there.

魔法唧唧 2024-10-07 17:12:04

我最近想出了这个方法,可以调用服务器将用户定义的 LaTeX 字符串转换为方程的图像。该调用是通过 VBA 通过 MacScript 命令进行的,如下所示:

command = "do shell script """ & pyPath & "python " & getURLpath & "getURL.py --formula '" _
          & Latex_Str & "' --fontsize " & Font_Size & " " & WebAdd & """"
result = MacScript(command)

看起来很难看,但这只是构建命令 do shell script /usr/bin/python {path to script}/getURL。 py --formula '{LaTeX Formula string}' --fontsize {int} {myurl} 并将其传递给命令。然后,我的 Python 脚本使用 argparse 来解析发送给它的参数,并使用 urllib 和 urllib2 来处理向服务器发送请求。 MacScript 命令读取 Python 脚本的标准输出,并将其作为字符串返回到 result

本关于 urllib2 的指南应该可以帮助您启动并运行 Python 脚本。

编辑:抱歉,我上次的回答不完整。我用来完成这项工作的 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

I recently figured this out for making a call to a server to convert a user-defined LaTeX string to an image of the equation. The call is made through VBA via the MacScript command as:

command = "do shell script """ & pyPath & "python " & getURLpath & "getURL.py --formula '" _
          & Latex_Str & "' --fontsize " & Font_Size & " " & WebAdd & """"
result = MacScript(command)

Which looks ugly, but this is just building the command do shell script /usr/bin/python {path to script}/getURL.py --formula '{LaTeX formula string}' --fontsize {int} {myurl} and passing it to the command. My Python script then uses argparse to parse the arguments sent to it, and urllib and urllib2 to handle sending the request to the server. The MacScript command read the stdout of my Python script and returns it as a string to result.

This guide on urllib2 should help you get the Python script up and running.

EDIT: Sorry, my answer was incomplete last time. The Python script I used to finish the job is below.

# 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文