Need Help With Applescript Password Prompt

Hello,

I’m sure this isn’t the best method to use, but I don’t know of a better way at the moment…

So using AppleScript, I am wondering how to most effectively (and securely) prompt for a password.

At the moment I am asking for user input from a display dialog prompt and putting it in a variable which will then be used later in a do shell script command.

For the most part it works, but the problem is that when the user input has special characters like “$” or “&” for example, it will fail. Therefore I need to find a way around that, or just use a different method to prompt for username/password altogether.

Any ideas?

One approach is to use do shell script with the administrator privileges property. The user will be prompted for a password, and the script will not prompt for authentication again for five minutes. I don’t know how this will work with special characters, though. For example,

do shell script "ls ~/" with administrator privileges

This is discussed in a technical note at:

https://developer.apple.com/library/archive/technotes/tn2065/_index.html#//apple_ref/doc/uid/DTS10003093-CH1-TNTAG1-HOW_DO_I_GET_ADMINISTRATOR_PRIVILEGES_FOR_A_COMMAND_

A sample script would be nice

Sorry guys, wrote the initial post in haste and forgot to put an example of my script…

So anyway, here’s what I’m doing:

set admin_password_dialog to display dialog "Enter your Domain Admin password: " with title "Domain Admin Password" default answer "" buttons {"Cancel", "Next"} default button "Next" with hidden answer
set button_returned to button returned of admin_password_dialog
set admin_password to text returned of admin_password_dialog

do shell script "/usr/sbin/dsconfigad -f -a " & pc_name & " -domain " & domain_join & " -u " & domain_admin & " -p " & admin_password & " -ou " & OU_join & "" with administrator privileges

do shell script "/usr/bin/dscl -u " & domain_admin & " -P " & admin_password & " '/Active Directory/" & Sdomain_join & "/All Domains' -append '/Computers/" & pc_name & "$' Description 'Some description goes here'"

Of course all the other variables in the “do shell script” lines are handled prior to this part, so I’m only concerned with the “admin_password” variable for this question.

Again, if there is a better and more secure way to prompt for this, I’m all ears.

While I was waiting for your response i wrote a little script to test

I think your’e problem is using “with administrator privileges” needs “user name” and “password” parameters

I created an account on my Mac called “test” with password “robert&$”
I don’t know what other characters would need to be escaped but you can add them to the string

also special characters in the password need to be escaped

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

local cc
set cc to ""
repeat until cc ≠ ""
	try
		set cc to text returned of (display dialog "Enter password…" default answer cc with hidden answer)
	on error -- usually means user chose cancel
		return
	end try
end repeat
set cc to escapeText(cc)
try
	set cc to do shell script "ls -al /System/Library/" user name "test" password cc with administrator privileges
on error errMessage number errNum
	display alert errMessage message ("Error number: " & errNum)
	return
end try
display dialog cc buttons {"OK"} default button 1 with title "Got Answer!" giving up after 10

on escapeText(astring)
	local escString, i
	set escString to ""
	repeat with i from 1 to length of astring
		if (text i of astring) is in "™£¢∞§¶•`~;:" then
			set escString to escString & "\\" & (text i of astring)
		else
			set escString to escString & (text i of astring)
		end if
	end repeat
	return escString
end escapeText