【问题标题】:How do I get osascript/Applescript to accept command line parameters with SPACES in them?如何让 osascript/Applescript 接受带有空格的命令行参数?
【发布时间】:2017-06-25 10:51:22
【问题描述】:

如果使用其中没有空格的 CLP'S 调用此代码(例如“~ testing”或“/Volumes/BOOTCAMP testing”),它运行良好。但是,如果 CLP 包含空格(例如“/Volumes/Shared External testing”、“~ test results”或“/Volumes/Shared External test results”,则它会出现故障或中止并显示错误消息。

以下代码 sn -p 是演示问题的最低限度:

on run (clp)
    if clp's length is not 2 then error "Incorrect Parameters"
end run

实际应用的相关代码为:

on run (clp)
    if clp's length is not 2 then error "Incorrect Parameters"
    local destination, libraryName
    set destination to clp's item 1
    set libraryName to clp's item 2

这个应用程序除了一个目录来保存它的数据作为第一个参数和文件名保存它作为第二个参数。第一个损坏的示例将导致目标设置为“/volumes/Shared”,库名称设置为“外部”,其余的被忽略。第二个损坏的示例将导致目标设置为“~”,库名称设置为“测试”,其余的被忽略。最后一个损坏的示例将导致与第一个示例相同的结果。

我已经在 Google 上进行了广泛的搜索,并且已经尝试将包含参数的空格包含在以下各项中,但均未成功:双引号、单引号和 {}。我还尝试用逗号而不是空格分隔封闭的参数,并将 all 参数括在 {} 中。同样,这些都没有成功。我还咨询了“info osascript”,但无济于事。

有没有人有解决方案,或者有更多的想法可以尝试?

【问题讨论】:

  • 错误将在于您如何分配 clp。显示该代码。此外,您可以将 clp 作为第一行记录以查看发生了什么。

标签: macos applescript osascript


【解决方案1】:

测试脚本:

on run (clp)
    if clp's length is not 2 then error "Incorrect Parameters"
    local destination, libraryName
    set destination to clp's item 1
    set libraryName to clp's item 2
    display dialog libraryName -- libraryName contains "test 123"
    display dialog destination -- destination contains "/Volumes/Shared External"
end run

当我在 bash shell

中执行此命令时
osascript '/the full path/of the/testingScript.scpt' '/Volumes/Shared External' 'test 123'

对话框正确显示两个参数。

【讨论】:

  • 我刚刚使用“/etc/shells”中的每个 shell 手动测试了您的命令行/测试脚本,它可以与 所有这些一起使用!
猜你喜欢
  • 2013-11-24
  • 2021-07-22
  • 2013-05-23
  • 1970-01-01
  • 2018-12-14
  • 2019-05-16
  • 1970-01-01
  • 1970-01-01
  • 2021-09-18
相关资源
最近更新 更多