macos 在applescript中将字符串和变量连接成字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7210188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 08:21:48  来源:igfitidea点击:

concatenate a string and a variable into a string in applescript

macosapplescript

提问by jeremyjjbrown

I have to write an Applescript to automount a folder depending on the user. Applescript Editor throws this error.

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

A end of line can't go after this identifier.

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

Here the portion of the script that is throwing the error.

这里是抛出错误的脚本部分。

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

回答by Bertrand Marron

pathcan't be a variable name.

path不能是变量名。

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

Works fine.

工作正常。

回答by regulus6633

I agree with Bertrand, your problem is with using "path" as a variable. Some words have a special meaning to applescript and can't be used as a variable, path being one of them. You'll notice that when you compile your code that path doesn't turn green like other variables indicating it's special.

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

If you still wanted to use "path" as the variable you can though. In applescript you can put "|" around the variable to indicate to applescript that it is a variable. So this would work.

如果您仍然想使用“路径”作为变量,您可以。在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

Note that using this technique you can actually have one variable in several words like...

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

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

One last comment... there is an applescript method to get the short user name of a person...

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

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