Mac Office 2011 VBA - 调用服务器脚本
我正在将一个大型 VBA 项目从 Windows 移植到新的 Mac Word 2011。实际上进展非常顺利...几乎所有代码都可以运行。
我的代码需要调用服务器上的脚本。在Windows上,我调用系统函数InternetOpenUrl来调用脚本,并调用InternetReadFile来读取脚本返回的结果。例如,我调用如下脚本:
"http://www.mysite.com/cgi-bin/myscript.pl?param1=Hello¶m2=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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试 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.我最近想出了这个方法,可以调用服务器将用户定义的 LaTeX 字符串转换为方程的图像。该调用是通过 VBA 通过 MacScript 命令进行的,如下所示:
看起来很难看,但这只是构建命令 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 脚本如下。
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: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 usesargparse
to parse the arguments sent to it, andurllib
andurllib2
to handle sending the request to the server. TheMacScript
command read the stdout of my Python script and returns it as a string toresult
.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.