在AppleScript中将引号添加为文本

时间:2018-02-08 22:12:19

标签: applescript

我试图制作AppleScript,您可以在其中更改Mac的本地主机名。我试图通过终端命令来完成它,但是如果用户想要在主机名中使用空格,它就不起作用,对于带空格的命令,你需要使用引号,并且至于我知道,你不能添加"作为AppleScript中的文本。这就是我想要做的事情:

set computername to display dialog "Type the desired name" default answer "" with icon note buttons {"Continue"} default button "Continue" as text
display dialog (text returned of computername) & " is this correct? " & "."

display dialog "If it askes you, please type your password in. (It may ask more than once)"

do shell script "scutil --set LocalHostName " & """ & (text returned of computername) & """

display dialog "Changes will be applied after a restart. Click continue to save changes and restart."

do shell script "shutdown -r now"

我需要输入"在输入名称之前以及之后。

 do shell script "scutil --set LocalHostName " & """ & (text returned of computername) & """

but I just get errors

提前致谢

1 个答案:

答案 0 :(得分:0)

首先,我们鼓励您说服用户不要在主机名中使用空格字符。这样做非常容易出错。

AppleScript提供了一个简单的解决方案来正确处理shell脚本中的转义参数:

do shell script "scutil --set LocalHostName " & quoted form of (text returned of computername)