2014-04-30 48 views
0

我最近正在研究一个AppleScript项目,该项目需要将用户的密码复制到剪贴板供以后使用。我已经有了提示用户输入密码的部分,但是如何将返回的文本(他们的密码)设置到剪贴板。我当前的代码给我的错误:Can’t make text returned of «script» into type text.这是我到目前为止有:如何将剪贴板设置为AppleScript返回的文本

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 

回答

0

当通过do shell script运行shell脚本我通常使用的AppleScript内的变量捕获结果。我不知道如果有一个“正确”的方式做到这一点,但你的脚本,我删除

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 
相关问题