【问题标题】:Automator launching python scriptAutomator 启动 python 脚本
【发布时间】:2019-12-24 21:02:21
【问题描述】:

所以我试图让我的 Excel 电子表格中的一个按钮启动 Python 脚本。显然这只能在 Windows 上实现,所以在 Mac 版本上,人们有一个解决方法,他们使用 Excel 启动 AppleScript,然后启动 Python 脚本。

在 Automator 中,我尝试选择应用程序,但当它不起作用时,我选择了快速操作,并且两次都插入了“运行 Shell 脚本”模块。

在其中我将 Shell 设置为“usr/bin/python”并将输入作为“As Arguments”传递。然后在我输入的文本字段中

do shell script "/Users/user_name/Desktop/integrationtest.py"

我试过了

do shell script "python /Users/user_name/Desktop/integrationtest.py"

当我点击 Automator 右上角的运行时,它会显示

The action “Run Shell Script” encountered an error: “  File "<string>", line 1
    do shell script "/Users/user_name/Desktop/integrationtest.py"
           ^
SyntaxError: invalid syntax”

  File "<string>", line 1
    do shell script "/Users/user_name/Desktop/integrationtest.py"
           ^
SyntaxError: invalid syntax

不太确定我做错了什么 - 有什么想法吗?感谢您的帮助

【问题讨论】:

  • 我会指出 Automator 有一个 Run Shell Script 操作,这可能比尝试在 AppleScript 中从 do shell script 运行 shell 脚本提供更好的结果。不过,我并不是很喜欢 Excel 按钮,所以我稍后会研究一下。

标签: macos terminal applescript automator


【解决方案1】:

您正在混淆 AppleScript 和 Shell Script 语法。

  • 运行 Shell 脚本动作语法是

    python /Users/user_name/Desktop/integrationtest.py
    
  • 运行 AppleScript 动作语法是

    on run {input, parameters}
        do shell script "python /Users/user_name/Desktop/integrationtest.py"
        return input
    end run
    

【讨论】:

    【解决方案2】:

    我建议你一步一步来:首先打开终端应用程序并测试你的python脚本运行良好:

    python /Users/user_name/Desktop/integrationtest.py
    

    如果OK,然后打开Applescript Editor并创建一个单行脚本:(在Applescript中,某些环境设置可能与通常的bash终端不同)

    do shell script "python /Users/user_name/Desktop/integrationtest.py"
    

    如果此 Applescript 测试再次按预期工作,则从 Excel 调用此 Applescript。调用方法取决于您的 Excel 版本:Excel 2011 使用 Macscript 函数,Excel 2016 使用 ApplescriptTask。

    【讨论】:

      猜你喜欢
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 2023-03-27
      • 2020-09-27
      • 2021-08-15
      • 2013-02-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多