【问题标题】:concatenate a string and a variable into a string in applescript将字符串和变量连接成applescript中的字符串
【发布时间】:2016-03-20 14:41:07
【问题描述】:

我必须编写一个 Applescript 来根据用户自动挂载文件夹。 Applescript 编辑器会引发此错误。

此标识符后面不能有行尾。

这里是引发错误的脚本部分。

try
    set short_name to do shell script "whoami"
    set path to "afp://fileserver.local/Faculty/" & short_name
    mount volume path as user name short_name
end try

【问题讨论】:

  • 我应该补充一点,第 3 行末尾的 short_name 已突出显示。
  • 附注:您不应该看到任何错误,因为您的脚本位于 try 块中。

标签: macos applescript


【解决方案1】:

path 不能是变量名。

try
    set short_name to do shell script "whoami"
    set p to "afp://fileserver.local/Faculty/" & short_name
    display dialog p
end try

工作正常。

【讨论】:

    【解决方案2】:

    我同意 Bertrand,您的问题在于使用“路径”作为变量。有些词对applescript有特殊意义,不能用作变量,path就是其中之一。您会注意到,当您编译代码时,该路径不会像其他变量那样变成绿色,表明它是特殊的。

    如果你仍然想使用“路径”作为变量,你可以。在applescript中,您可以输入“|”围绕变量向applescript表明它是一个变量。所以这会起作用。

    try
        set short_name to do shell script "whoami"
        set |path| to "afp://fileserver.local/Faculty/" & short_name
        mount volume |path| as user name short_name
    end try
    

    请注意,使用这种技术,您实际上可以将一个变量包含在几个词中,例如...

    set |the path| to "afp://fileserver.local/Faculty/" & short_name
    

    最后一条评论...有一个applescript方法可以获取一个人的短用户名...

    set short_name to short user name of (get system info)
    

    【讨论】:

    • +1,但我会在 set short_name to short user name of (get system info) 上不同意你的观点。在我的机器上运行大约需要 2 秒。
    • 我同意(获取系统信息)非常慢。 "do shell script "whoami" " 更快
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-15
    • 2018-06-23
    • 1970-01-01
    • 1970-01-01
    • 2021-12-12
    相关资源
    最近更新 更多