Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

Password request dialog from the command line with AppleScript (See related posts)

unset -v input

#input="$(/usr/bin/osascript <<-'__HEREDOC__'
input="$(/usr/bin/osascript 2>/dev/null <<-'__HEREDOC__'
   with timeout of 300 seconds
      tell application "Finder"
--      tell application "System Events"
         activate
         set Input to display dialog "Please enter your password:" \
            with title "Password" \
            with icon caution \
            default answer "" \
            buttons {"Cancel", "OK"} \
            default button 2 \
            with hidden answer \
            giving up after 295
         return text returned of Input as string
      end tell
   end timeout
__HEREDOC__
)"

printf "%s\n" "${input}"


open -a 'Script Editor'

# copy & paste the following snippet into Script Editor 

with timeout of 300 seconds
	tell application "Finder"
		activate
		set Input to display dialog "Please enter your password:" with title "Password" with icon caution default answer "" buttons {"Cancel", "OK"} default button 2 giving up after 295 with hidden answer
		return text returned of Input as string
	end tell
end timeout


#---------------------------------------


unset -v input name
#export name="$(/usr/bin/logname)"
export name="$(/usr/bin/whoami)"

input="$(/usr/bin/osascript 2>/dev/null <<-__HEREDOC__
   with timeout of 300 seconds
      tell application "Finder"
--      tell application "System Events"
         activate
         set my_name to "${name}"
         set my_pass to display dialog "Enter password for " & quoted form of my_name \
            with title "Login Window" \
            with icon note \
            default answer "" \
            buttons {"Cancel", "OK"} \
            default button 2 \
            with hidden answer \
            giving up after 295
            return text returned of my_pass as string
      end tell
   end timeout
__HEREDOC__
)"


printf "%s\n" "${input}"

You need to create an account or log in to post comments to this site.