简体   繁体   中英

How to set the clipboard to the returned text on AppleScript

I was recently working on an AppleScript project that would require the user's password to be copied to the clipboard for later use. I already have the part where the user is prompted for their password but how do I set the text returned (their password) to the clipboard. My current code gives me the error: Can't make text returned of «script» into type text. This is what I have so far:

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

When running shell scripts via do shell script I typically use a variable within the applescript to capture the result. I'm not sure if there is a "right" way to do this, but with your script I removed

set a to (text returned) as list  
set AppleScript's text item delimiters to linefeed  
set a to a as text  

and captured the result of the do shell script command as the variable a , which I then put on the clipboard. Here is the modified 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 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

If you wanted to slim it down even more, you could eliminate the variable a altogether and capture the result of the do shell script command directly to the clipboard:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM