2014-04-30 83 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 
相關問題