【问题标题】:Passing Multiple Parameters from Applescript to Terminal Command Script将多个参数从 Applescript 传递到终端命令脚本
【发布时间】:2015-05-04 04:19:06
【问题描述】:

我一直试图弄清楚如何将多个参数从 Applescript 传递到终端命令脚本。例如,在运行终端命令文件时,您可以像这样以编程方式接收参数:

#!/bin/bash

var=$1

var=$2

我一直在使用的 Applescript 代码供参考:

tell application "System Events" to set app_directory to POSIX path of (container of (path to me))

set thisFile to "Dev"

set testTarget to "/Users/lab/Desktop/TestTarget/"

do shell script "/Users/lab/Desktop/TempRoot/mycommand.command " & thisFile & testTarget with administrator privileges

我认为我出错的地方是第二个参数的输入。当我只有一个参数时,它通过就好了:

do shell script "/path/to/command/mycommand.command" &var with administrative privileges

我很好奇传递第二个参数的正确语法是什么。如果有人有任何建议,请告诉我!另外,如果您需要更多信息,我很乐意提供!

【问题讨论】:

    标签: macos bash parameters terminal applescript


    【解决方案1】:

    您只需要在参数之间添加一个空格。现在,thisFiletestTarget 之间没有添加空格。您的命令如下所示:

    /Users/lab/Desktop/TempRoot/mycommand.command Dev/Users/lab/Desktop/TestTarget/
    

    将您的 shell 脚本行更改为:

    do shell script "/Users/lab/Desktop/TempRoot/mycommand.command " & thisFile & space & testTarget with administrator privileges
    

    在构建脚本时我发现有助于确保我的 shell 命令在运行之前是正确的。因此,与其直接构建它,不如将命令存储在一个变量中并记录它。稍后,将日志记录语句替换为do shell script 命令。

    set shellScript to "/Users/lab/Desktop/TempRoot/mycommand.command " & thisFile & space & testTarget with administrator privileges
    log shellScript
    -- do shell script shellScript
    

    【讨论】:

    • 这成功了!我会支持这篇文章,但显然我没有足够的 stackoverflow 积分 :( 但是非常感谢!
    • @mmilutinovic1313:您不需要声誉积分来接受您的问题的答案(点击复选标记)。
    • 另一个值得一提的提示:要确保将(文字)文件名等字符串未经修改地传递给 shell(以保护字符串免受 shell 扩展),请在它们前面加上 quoted form of
    • 而且,记住“shellScript 的引用形式”来处理空格和引用字符会很有帮助。
    • 谢谢你们。您是否也对尝试在可可应用程序中实现这种类型的脚本有任何建议?我让这个在这里工作,但是当我将它切换到objective-c时,我遇到了一些语法错误。如果你有时间,这里是另一个帖子:stackoverflow.com/questions/28840814/…
    猜你喜欢
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 2020-05-18
    • 1970-01-01
    • 2016-10-26
    • 1970-01-01
    • 2017-07-18
    • 2018-01-06
    相关资源
    最近更新 更多