What I am trying to do is (in Mac OS X / MacOS) create a dialog in using osascript. I'd like to add an icon to it. This part is easy and works.
However, the problem is that if the user is in dark or light mode my icon file won't look right. So, I've got two .icns files which is stored on all Macs in my environment in a specific directory (that directory is located at /Library/ctxsit/). I can call up one or the other using a variable, but when I add a function to determine which file to use, it fails.
First, the part that works is:
icon=(/Library/ctxsit/CompanyIconB.icns)
title="Company IT Notification"
echo $title
prompt=$(osascript -e "display dialog \"Click Continue to logout from current user and set this Mac back to the Welcome Screen. This will leave the current user directory and profile intact.
Please make sure your work is saved. After Continuing, the device will appear to be in a Fresh OS state.
\" buttons {\"Cancel\", \"Continue\"} with title \"$title\" default button 2 with icon \"$icon\" ")
if [[ $prompt == "button returned:Continue" ]]
then
sleep 2
prompt=$(osascript -e "display dialog \"Are you sure? Clicking Continue will run the script and Restart this Mac.
\" buttons {\"Continue\", \"Cancel\"} with title \"$title\" default button 2 with icon \"$icon\" ")
fi
If the Mac is in Dark mode, then that first line of my code would have
icon=(/Library/ctxsit/CompanyIconW.icns)
All above is in the "working" version of my script. So, I have added a function to the "non-working" version.
Next, the part which does not work is:
#!/bin/bash
currentOS=$(sw_vers -productVersion)
currentUser=$( echo "show State:/Users/ConsoleUser" | scutil | awk '/Name :/ { print $3 }' )
#mode=$(sudo -u "$currentUser" defaults read -g AppleInterfaceStyle)
OScheck=$([[ (($currentOS < 10.14)) ]]; echo $?)
function setIcon()
{
if [[ $OScheck == "1" ]]; then
echo "Mojave or newer"
if [[ $mode == "Dark" ]]; then
echo "Dark mode enabled"
icon="CompanyIconW.icns"
else
echo "Light mode enabled"
icon="CompanyIconB.icns"
fi
else
echo "Older than Mojave"
icon="CompanyIconB.icns"
fi
}
setIcon
echo $icon
title="Company IT Notification"
echo $title
prompt=$(osascript -e "display dialog \"Click Continue to logout from current user and set this Mac back to the Welcome Screen. This will leave the current user directory and profile intact.
Please make sure your work is saved. After Continuing, the device will appear to be in a Fresh OS state.
\" buttons {\"Cancel\", \"Continue\"} with title \"$title\" default button 2 with icon \"$icon\" ")
if [[ $prompt == "button returned:Continue" ]]
then
sleep 2
prompt=$(osascript -e "display dialog \"Are you sure? Clicking Continue will run the script and Restart this Mac.
\" buttons {\"Continue\", \"Cancel\"} with title \"$title\" default button 2 with icon \"$icon\" ")
fi
Finally, when I run this script from terminal, I get this result:
bash-3.2$ ./Scratch2.sh
Mojave or newer
Light mode enabled
CompanyB.icns
Company IT Notification
0:393: execution error: A resource wasn’t found. (-192)
0:134: execution error: A resource wasn’t found. (-192)
The only part I can think of to address is how in my "working" version, I have an actual path-to-file whereas my "non-working" version does not include that piece. This leads to my main question:
How can I set a variable based off of the return/result/exit code/ of a function?
e.g. take whichever mode the computer is in (light, dark) and use file 1 for light and file 2 for dark.