【问题标题】:Mac Office 2011 VBA - calling a server scriptMac Office 2011 VBA - 调用服务器脚本
【发布时间】:2011-05-05 11:31:33
【问题描述】:

我正在将一个大型 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 的样子。还是有更好/更快的方法来做到这一点?

提前致谢, 加里

【问题讨论】:

    标签: macos vba applescript


    【解决方案1】:

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

    【讨论】:

    • 有人建议在 nc 中使用“do shell script”,效果很好: response = MacScript("do shell script" & Chr(34) & "echo 'GET " & scriptUrl & " HTTP/1.0\n\n' | nc domainName 80" & Chr(34)" 其中脚本 url 类似于“/cgi-bin/myscript.pl”,域名类似于“mydomain.com”
    【解决方案2】:

    我最近想出了这个方法来调用服务器以将用户定义的 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 解析发送给它的参数,并使用 urlliburllib2 处理将请求发送到服务器。 MacScript 命令读取我的 Python 脚本的标准输出并将其作为字符串返回给 result

    This guide on 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-20
      相关资源
      最近更新 更多