0

I'm currently working on a script to notify a user when their password will expire. The prompt appears, but no matter what button I press, it results to the same outcome, "Change Later was chosen". Any ideas?

#!/bin/bash
button=$(osascript -e 'tell application "System Events" to display dialog "Change password now or later?" buttons {"Change Later", "Change Now"} default button 1 with title "Password Update Required" with icon caution')

if [[ $button = "Change Now" ]]; then
    open /System/Library/PreferencePanes/Accounts.prefPane
    echo "Opening Preferences"
else
    echo "Change Later was chosen"
    exit 0
fi
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • Have you confirmed what is stored into `$button` (i.e. whether a string is recorded)? What output does that `echo $button` statement give you? – Jason K Lai Feb 06 '20 at 17:07
  • Run your code with `bash -x yourscript`, and tell us what the line `[[ $button = "Change Now" ]]` logs. – Charles Duffy Feb 06 '20 at 17:07
  • @JasonKLai, `echo $button` is unreliable. Someone looking at its output can't tell the difference between `button="Change Now"` and `button=" Change Now "`, but the comparison on the lines below cares very much about that difference. See [BashPitfalls #14](http://mywiki.wooledge.org/BashPitfalls#echo_.24foo). – Charles Duffy Feb 06 '20 at 17:08
  • 1
    The output of `osascript` is `button returned:Change Now`, not simply `Change Now`. – chepner Feb 06 '20 at 17:10

2 Answers2

0

osascript doesn't simply output the name of the button you press; it outputs a more "descriptive" string, prefixed with button returned:. You need to account for that in your check:

if [[ $button = "button returned:Change Now" ]]
chepner
  • 497,756
  • 71
  • 530
  • 681
  • 1
    It isn't so much "descriptive" as it is just the way the resulting AppleScript _record_ is coerced to a string. An alternative would be to add `return button returned of the result` to the script to return the desired key from the record. – red_menace Feb 06 '20 at 18:02
  • @red_menace Thanks; I wasn't sure exactly what the source of the extended text was, if it was AppleScript itself or some side effect of `osascript`. – chepner Feb 07 '20 at 03:30
0

Also worth considering is that you are free to perform the check that determines which button was pressed in the AppleScript portion of the code as well, which in some ways makes more sense, as you can open System Preferences with it too:

#!/usr/bin/env bash
osascript << OSA 2>/dev/null && exit 0 || echo "Change Later was chosen"
display dialog "Change password now or later?" buttons ¬
        {"Change Later", "Change Now"} default button 1 ¬
        with title "Password Update Required" with icon caution

if the button returned of the result ¬
        is "Change Later" then error

tell application id "com.apple.SystemPreferences"
        reveal pane id "com.apple.preferences.users"
        repeat until the id of the current pane is ¬
                "com.apple.preferences.users"
                delay 0.5
        end repeat
        activate
end tell
OSA

Here, rather than saving the outcome of the script to a variable, the exit status of osascript reflects the user's choice: "Change Later" terminates osascript with an error, thus returning an exit status of 1; "Change Now" allows the script to progress onward and opens System Preferences to the Users & Groups pane, before returning an exit status of 0.

CJK
  • 5,732
  • 1
  • 8
  • 26