【问题标题】:Applescript errors when reading user input (password) that contains escape characters读取包含转义字符的用户输入(密码)时出现 Applescript 错误
【发布时间】:2014-02-04 21:21:20
【问题描述】:

在我的部门,我们使用 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 的经验非常有限。

【问题讨论】:

标签: applescript


【解决方案1】:

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

我认为这不是“转义字符”问题。正斜杠不是转义字符。反斜杠是。问题可能是 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

【讨论】:

  • 是的,尝试这不起作用我会发布错误消息,但我现在是移动的。您将如何最好地实现字符串处理程序?我对applescript的了解真的很深。
  • 我使用 AppleScript 的文本项分隔符添加了一个处理程序来完成这项工作。当您将字符串强制转换为带有“文本项”的列表并再次使用“作为文本”返回时,它们会控制分隔符。所以我们可以操纵它们来得出我们的结果。
  • 在尝试您的处理程序后,这是我收到的错误“错误 mount_smbfs: server connection failed: No route to host number 68”。手动将斜杠设置为 \\/ 时,我也收到了此消息。
猜你喜欢
  • 2023-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多