读取包含转义字符的用户输入(密码)时出现Applescript错误

时间:2014-02-04 21:21:21

标签: applescript

在我的部门中,我们使用samba共享服务器作为员工的存储空间。对于使用Apple机器的员工,我们尝试使用AppleScript应用程序来获取用户的用户名和密码,然后使用它们来安装驱动器。当用户输入包含转义字符的密码时,会出现问题;例如 abcdefg / 。以下是设置用户名和密码变量的对话框代码:

set user_name_dialog to display dialog "Enter your User ID: " default answer "" buttons {"Next"} default button "Next"
set user_name to text returned of user_name_dialog
--this line prompts the user for their password and sets the returned result as the variable "user_password"
set user_password_dialog to display dialog "Enter your Password. " & return & return & "WARNING: If you are running Panther (MacOS 10.3), your input will be displayed in this box as clear text." default answer "" buttons {"Next"} default button "Next" with hidden answer
set user_password to text returned of user_password_dialog

这是运行用于安装驱动器的shell脚本行:

do shell script "mount -t smbfs //" & user_name & ":" & user_password & "@SOME IP ADDRESS/SOME FOLDER/ /SOME MOUNT POINT/" & user_name & "-SOME SHARE"

现在我从一个小小的研究中得知,它可能就像某个额外的引号一样简单,我甚至尝试过

quoted form of

没有任何运气。输入带有转义字符的字符串时收到的错误消息是这样的:

error "mount_smbfs: server connection failed: No route to host" number 68

任何帮助都会非常赞赏,因为我对AppleScript的体验非常有限。

1 个答案:

答案 0 :(得分:0)

注意:以下只是猜测。我没有测试任何一个。

我不相信这是一个“逃脱字符”的问题。正斜杠不是逃避字符。反斜杠是。问题可能是smbfs命令将正斜杠解释为路径变量的除数,这导致错误。所以问题变成了......你怎么能避免正斜杠和smbfs命令之间的冲突呢?

当您将命令传递给shell时,我会尝试转义正斜杠。请注意,applescript也使用反斜杠作为转义字符,因此要将转义字符传递给shell,您必须转义转义符。因此,编写一个自定义处理程序来传递密码,以便为您执行该转义过程。

所以如果这是你的密码,“abcdefg /”然后尝试将其传递为“abcdefg \\ /”(注意双反斜杠)。如上所述,您可以编写自定义处理程序来检查“/”字符的密码,如果找到,则在其前面插入“\\”。但在编写该处理程序之前,请手动尝试所提出的解决方案,看看它是否能解决您的问题。

set pword to "/abc/defg/"
escapeSlash(pword)

on escapeSlash(x)
    if x contains "/" then
        set AppleScript's text item delimiters to "/"
        set textItems to text items of x
        set AppleScript's text item delimiters to "\\/"
        set x to textItems as text
        set AppleScript's text item delimiters to ""
    end if
    return x
end escapeSlash