Cocoa Dialog Help

jthurwood
New Contributor III

Hi all

I'm having a few issues trying to get a Cocoa Dialog script to work. I'm basically asking for a few variables which a then want it to set the computername based on these variables but its failing.

Can anyone see where i'm going wrong?

!/bin/sh

Set CocoaDialog Location

CD="/Path/to/CocoaDialog.app/Contents/MacOS/CocoaDialog"

CD_APP="/Users/Shared/CocoaDialog.app"
CD="$CD_APP/Contents/MacOS/CocoaDialog"
last7ofserial=$system_profiler SPHardwareDataType | awk '/Serial/ {print $4}' | rev | cut -c -7 | rev
mac="$MAC"

Dialog to enter the Location and the create $COMPUTERNAME variable

location=$("$CD" standard-dropdown --string-output --title "Choose a Location" --height 150 --text "Location" --items "London" "New York" "Shanghai")
devicetype=$("$CD" standard-dropdown --string-output --title "Choose a Device Type" --height 150 --text "Device Type" --items "LT" "DT")

COMPUTERNAME=$location$devicetype$mac$last7ofserial

Set Hostname using variable created above

scutil –-set ComputerName $COMPUTERNAME
scutil –-set HostName $COMPUTERNAME
scutil –-set LocalName $COMPUTERNAME

Dialog to confirm that the hostname was changed and what it was changed to.

tb=`$CD ok-msgbox --text "Computer Name Changed!"
--informative-text "The computer name has been changed to $COMPUTERNAME"
--no-newline --float`
if [ "$tb" == "1" ]; then
echo "User said OK"
elif [ "$tb" == "2" ]; then
echo "Canceling"
exit
fi

2 REPLIES 2

ryan_ball
Valued Contributor

You had several issues. I've made some changes that should work. This leverages the Jamf binary to change the name rather than scutil of which your syntax was wrong anyways.

One thing you'll need to do is set your $MAC variable. I'm not sure what that is even doing. Also, you may consider setting up something so that if somebody chooses "New York" as the location then in the computer name it only puts "NY" etc. so that you don't have that long of a computer name.

#!/bin/bash

# Set CocoaDialog Location
CD="/Users/Shared/CocoaDialog.app/Contents/MacOS/CocoaDialog"
last7ofserial=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}' | rev | cut -c -7 | rev)
mac="$MAC" # This is definitely an issue. You have not set the $MAC variable so what is this even doing?
errorIcon="/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/AlertStopIcon.icns"

# Dialog to enter the Location and the create computerName variable
while [[ -z "$location" ]]; do
    location=$("$CD" standard-dropdown --string-output --title "Choose a Location" --height 150 --text "Please choose a location." --items "London" "New York" "Shanghai")
    buttonClicked=$(/bin/echo "$location" | awk 'NR==1{print}')
    # Below will prompt for location, print it to upper case, then remove spaces
    location=$(/bin/echo "$location" | awk 'NR>1{print}' | awk '{print toupper($0)}' | sed 's/ //g')
    [[ $buttonClicked = "Cancel" ]] && echo "User canceled; exiting." && exit 0
done

while [[ -z "$deviceType" ]]; do
    deviceType=$("$CD" standard-dropdown --string-output --title "Choose a Device Type" --height 150 --text "Please chose a device type." --items "LT" "DT")
    buttonClicked=$(/bin/echo "$deviceType" | awk 'NR==1{print}')
    deviceType=$(/bin/echo "$deviceType" | awk 'NR>1{print}')
    [[ $buttonClicked = "Cancel" ]] && echo "User canceled; exiting." && exit 0
done

computerName="${location}${deviceType}${mac}${last7ofserial}"

# Use Jamf Binary to set Hostname using variable created above
/usr/local/bin/jamf setComputerName -name "$computerName"

# Dialog to confirm that the hostname was changed and what it was changed to.
if [[ "$?" == "0" ]]; then
    $CD ok-msgbox --text "Computer Name Changed!" 
        --informative-text "The computer name has been changed to $computerName." 
        --no-newline --float --no-cancel &> /dev/null &
else
    $CD ok-msgbox --text "Error!" 
        --informative-text "There was an error changing the computer name." 
        --icon-file "$errorIcon" --no-newline --float --no-cancel &> /dev/null &
fi

exit 0

ryan_ball
Valued Contributor

@jthurwood Does that help?