欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

osascript display dialog password 有大用 有大大用

Asked 
Viewed 3k times
6

I'm in the final phase of designing a script to automate my Active Directory binding that will be used by multiple people. Because of this, I need to prompt for a user name and password. I've successfully created the prompt, but want to find some way to prevent the password from showing up in the dialog box asking for the password (this will be done remotely, I don't want the password visible).

It can be turned into stars, dots, not show up at all, anything, I just need it NOT to show visually, but still be able to be passed down to the dsconfigad command.

I've tested the script itself and it works and this is the last piece that I need to make it live.

(Excuse any extra comments on here, I've pieced this together from a lot of different sources)

#!/bin/bash

while :; do # Loop until valid input is entered or Cancel is pressed.
    user=$(osascript -e 'Tell application "System Events" to display dialog "Enter the network user name:" default answer ""' -e 'text returned of result' 2>/dev/null)
    if (( $? )); then exit 1; fi  # Abort, if user pressed Cancel.
    user=$(echo -n "$user" | sed 's/^ *//' | sed 's/ *$//')  # Trim leading and trailing whitespace.
    if [[ -z "$user" ]]; then
        # The user left the project name blank.
        osascript -e 'Tell application "System Events" to display alert "You must enter a user name; please try again." as warning' >/dev/null
        # Continue loop to prompt again.
    else
        # Valid input: exit loop and continue.
        break
    fi
done

while :; do # Loop until valid input is entered or Cancel is pressed.
    netpass=$(osascript -e 'Tell application "System Events" to display dialog "Enter the network password:" default answer ""' -e 'text returned of result' 2>/dev/null)
    if (( $? )); then exit 1; fi  # Abort, if user pressed Cancel.
    netpass=$(echo -n "$netpass" | sed 's/^ *//' | sed 's/ *$//')  # Trim leading and trailing whitespace.
    if [[ -z "$netpass" ]]; then
        # The user left the project name blank.
        osascript -e 'Tell application "System Events" to display alert "You must enter a password; please try again." as warning' >/dev/null
        # Continue loop to prompt again.
    else
        # Valid input: exit loop and continue.
            break
        fi
    done

MACNAME=$(scutil --get ComputerName)

sudo dsconfigad -add DOMAIN \
-username $user \
-password $netpass \
-computer $MACNAME \
-mobile disable \
-mobileconfirm disable \
-localhome enable \
-useuncpath enable \
-shell /bin/bash \
-ou OU=Macs,CN=Computers,DC=corp,DC=DOMAIN,DC=net \
-force \
-localpassword LOCALPASS \
-groups "GROUPS"

#sudo rm -- "$0"
Get updates on questions and answers

1 Answer  正确答案 

9

Use with hidden answer. Link:

https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_cmds.html#//apple_ref/doc/uid/TP40000983-CH216-SW12

osascript -e 'Tell application "System Events" to display dialog "Enter the network password:" with hidden answer default answer ""' -e 'text returned of result' 2>/dev/null

Your Answer


来自 https://stackoverflow.com/questions/25575635/how-to-hide-a-password-from-a-prompted-osascript-dialogue-from-a-mac-unix-shell



Asked 
Viewed 8k times
5

I would like to make a dialog input box that looks exactly like this in AppleScript: 

Except without the picture in the top left of the lock.

Also, I need to be able to save both inputs.

I know I can use tell application "System Events" to display dialog "blah blah" default answer "" end tell but I can not find a way to have multiple and labeled fields.

Get updates on questions and answers
  • Just an aside: the GUI commands are part of the Standard Additions library, not System Events - no need for tell application "System Events".  Feb 27, 2015 at 3:43

1 Answer  正确答案 


4

Natively, AppleScript doesn't offer this capability as of OSX 10.10.

To see what GUI operations are supported, check the User Interaction suite of the dictionary of the Standard Additions (StandardAdditions.def, accessible from Script Editor.app via File > Open Dictionary... > StandardAdditions.osax).

The closest approximation is a single input-field dialog - prompting for a password only - as follows (a limitation you've already noted in the question, but just to illustrate how to prompt for a password and how to use custom buttons):

display dialog ¬
    "Installer is ..." default answer ¬
    "" buttons {"Cancel", "Install Software"} ¬
    default button 2 ¬
    with hidden answer

To get what you want, you need a third-party library such as Pashua.

Here's how you would define the requested dialog in Pashua and process the returned values:

# Define the dialog using a textual definition similar to a properties file.
set dlgDef to "
# Window title (if you don't set one explicitly (even if empty), it'll be 'Pashua')
*.title = 

# Add the hint (static text).
st.type = text
st.text = Installer is trying to install new software. Type your password to allow this.

# Add the username field.
tfn.type = textfield
tfn.label = Name:

# Add the password field.
tfp.type = password
tfp.label = Password:

# Add the buttons.
cb.type = cancelbutton
db.type = defaultbutton
db.label = Install Software
"

# Show the dialog. 
# Return value is a record whose keys are the element names from the dialog
# definition (e.g., "tfn" for the  usernam text field) and whose
# values are the values entered (for input fields) or 
# whether a given button was clicked or not ("1" or "0")
set theResult to showDialog(dlgDef, "")

# Process the returned values.
if cb of theResult is "1" then
    display alert "User canceled the dialog."
else
    display alert "name=[" & tfn of theResult & "]; password=[" & tfp of theResult & "]"
end if

The dialog will look like this:

enter image description here


Overview of setting up Pashua

Note: This is a simplified overview; for full instructions, see Read me.html and Documentation.html in the downloaded disc image.

  • Download and mount the disc image from http://www.bluem.net/en/mac/pashua/

  • Place Pashua.app in /Applications~/Applications, or - in a pinch - in the same folder as the invoking script.

    • Pashua.app is the application that renders the dialogs (while a dialog is up, the menu bar shows Pashua).

  • Copy the bindings code (2 short handlers, showDialog and getPashuaPath) from Examples/AppleScript/Pashua.scpt into your script.

    • Note: Due to a bug as of this writing, you must apply a small correction to handler getPashuaPath: replace line return (path to applications folder from system domain as text) & "Pashua.app" with return (path to applications folder from system domain as text) & "Pashua.app:".

    • Alternatively, get the latest bindings code directly from the GitHub repo: https://github.com/BlueM/Pashua-Binding-AppleScript

Your Answer


来自  https://stackoverflow.com/questions/28756565/make-username-and-password-input-box-with-applescript




普通分类: