我最近正在处理一个AppleScript项目,该项目需要将用户的密码复制到剪贴板以备将来使用。我已经有了提示用户输入密码的部分,但我如何将返回的文本(他们的密码)设置到剪贴板。我当前的代码给了我错误:Can’t make text returned of «script» into type text.
这就是我目前所拥有的:
set usr to short user name of (system info)
repeat
display dialog "Please enter login password to continue:" default answer "" buttons {"Submit"} with title "Enter password" with icon stop with hidden answer
set pswd to text returned of the result
try
do shell script "echo test" user name usr password pswd with administrator privileges
exit repeat
end try
end repeat
set a to (text returned) as list
set AppleScript's text item delimiters to linefeed
set a to a as text
set the clipboard to a
当通过do shell script
运行shell脚本时,我通常使用appdescription中的一个变量来捕获结果。我不确定是否有"正确"的方法来做这件事,但在你的脚本中,我删除了
set a to (text returned) as list
set AppleScript's text item delimiters to linefeed
set a to a as text
并捕获do shell script
命令的结果作为变量a
,然后我将其放在剪贴板上。这是修改后的脚本:
set usr to short user name of (system info)
repeat
display dialog "Please enter login password to continue:" default answer "" buttons {"Submit"} with title "Enter password" with icon stop with hidden answer
set pswd to text returned of the result
try
set a to do shell script "echo test" user name usr password pswd with administrator privileges
exit repeat
end try
end repeat
set the clipboard to a
如果您想进一步精简它,可以完全消除变量a
,并将do shell script
命令的结果直接捕获到剪贴板:
set usr to short user name of (system info)
repeat
display dialog "Please enter login password to continue:" default answer "" buttons {"Submit"} with title "Enter password" with icon stop with hidden answer
set pswd to text returned of the result
try
set the clipboard to (do shell script "echo test" user name usr password pswd with administrator privileges)
exit repeat
end try
end repeat