Dockutil Woes in Swift

psherotov
Contributor

My question may have a ridiculously easy answer, but here goes. We have a lab environment with a teacher who works with grades Pre-K, K, 1, 2, 3.

We have user accounts (standard user accounts in AD for these grades). 

The teacher wanted to have a specific dock configuration for these standard user accounts. After looking at a bunch of options I went with Dockutil. 

A friend of mine with considerably more scripting experience helped me create a single script that used case to configure the dock for those specific user accounts.

The trouble is that script was made before Dockutil was rebuilt using Swift. How do I adapt the script to work now without Python??

The script is below:

#!/bin/bash

#We need to wait for the dock to actually start
until [[ $(pgrep Dock) ]]; do
    wait
done

#Get the current logged in user that we'll be modifying
if [ ! -z "$3" ]; then
	user=$3
else
	user=$(python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "\n");')
fi

#Set variables
du="/usr/local/bin/dockutil"
userHome="/Users/$user"
networkHome="smb://server.com/Students$/$user"

#Function for applying dock configuration
createBaseDock()
{
    #Remove all items for logged in user
    $du --remove all --no-restart $userHome
    
    #Adding base items to the dock    
    $du --add '/Applications/Google Chrome.app' --position 1 --no-restart $userHome
    $du --add '/Applications/Safari.app'  --position 2 --no-restart $userHome
    $du --add '/Applications/Comic Life 3.app' --position 3 --no-restart $userHome
    $du --add '/Applications/The Print Shop 4.app' --position 4 --no-restart $userHome
    $du --add '/Applications/KID PIX.app' --position 5 --no-restart $userHome
}

#Function for finishing base dock
finishBaseDock()
{
    #Add local downloads
    $du --add '~/Downloads' --section others --position last --no-restart $userHome

    killall Dock
}

    case $user in
        p|k)
            echo "p or k found"
            createBaseDock
            finishBaseDock
            ;; 
        1)
            echo "1 found"
            createBaseDock
            $du --add '/System/Applications/Photo Booth.app' --position 6 --no-restart $userHome
            finishBaseDock
            ;;
        2)
            echo "2 found"
            createBaseDock
            $du --add '/System/Applications/Photo Booth.app' --position 6 --no-restart $userHome
            finishBaseDock
            ;;
        3)
            echo "3 found"
            createBaseDock
            $du --add '/System/Applications/Photo Booth.app' --position 6 --no-restart $userHome
            $du --add '/Applications/Adobe Photoshop 2020/Adobe Photoshop 2020.app' --position 7 --no-restart $userHome
            finishBaseDock
            ;;
    esac

exit 0

 

 

1 ACCEPTED SOLUTION

I made some changes to your script because repetition is discouraged in coding (DRY: Dont Repeat Yourself).

In your `case` statement, you were running a `createBaseDock` and a `finishBaseDock` with each case but you can just run the create before the case check and the finish one after.

 

#!/bin/bash

#We need to wait for the dock to actually start
until [[ $(pgrep Dock) ]]; do
    wait
done

#Get the current logged in user that we'll be modifying
if [ ! -z "$3" ]; then
	user=$3
else
	user=$( scutil <<< "show State:/Users/ConsoleUser" | awk '/Name :/ && ! /loginwindow/ { print $3 }' )
fi

#Set variables
du="/usr/local/bin/dockutil"
userHome="/Users/$user"
networkHome="smb://server.com/Students$/$user"

#Function for applying dock configuration
createBaseDock()
{
    #Remove all items for logged in user
    $du --remove all --no-restart $userHome
    
    #Adding base items to the dock    
    $du --add '/Applications/Google Chrome.app' --position 1 --no-restart $userHome
    $du --add '/Applications/Safari.app'  --position 2 --no-restart $userHome
    $du --add '/Applications/Comic Life 3.app' --position 3 --no-restart $userHome
    $du --add '/Applications/The Print Shop 4.app' --position 4 --no-restart $userHome
    $du --add '/Applications/KID PIX.app' --position 5 --no-restart $userHome
}

#Function for finishing base dock
finishBaseDock()
{
    #Add local downloads
    $du --add '~/Downloads' --section others --position last --no-restart $userHome

    killall Dock
}

createBaseDock

    case $user in
        p|k) echo "p or k found";; 
        1)  echo "1 found"
            $du --add '/System/Applications/Photo Booth.app' --position 6 --no-restart $userHome;;
        2)  echo "2 found"
            $du --add '/System/Applications/Photo Booth.app' --position 6 --no-restart $userHome;;
        3)  echo "3 found"
            $du --add '/System/Applications/Photo Booth.app' --position 6 --no-restart $userHome
            $du --add '/Applications/Adobe Photoshop 2020/Adobe Photoshop 2020.app' --position 7 --no-restart $userHome;;
    esac

finishBaseDock

exit 0

 

 

View solution in original post

5 REPLIES 5

cbrewer
Valued Contributor II

Use the following to get the logged in user. The python method was tried and true for many years and this is the modern replacement.

https://scriptingosx.com/2020/02/getting-the-current-user-in-macos-update/ 

 

 

user=$( scutil <<< "show State:/Users/ConsoleUser" | awk '/Name :/ && ! /loginwindow/ { print $3 }' )

 

 

I figured it was probably something simple, to someone (not me) who is fluent in scripting. Thank you! :-)

 

Below is the revised script with the change you suggested, look right?

 

#!/bin/bash

#We need to wait for the dock to actually start
until [[ $(pgrep Dock) ]]; do
    wait
done

#Get the current logged in user that we'll be modifying
if [ ! -z "$3" ]; then
	user=$3
else
	user=$( scutil <<< "show State:/Users/ConsoleUser" | awk '/Name :/ && ! /loginwindow/ { print $3 }' )
fi

#Set variables
du="/usr/local/bin/dockutil"
userHome="/Users/$user"
networkHome="smb://server.com/Students$/$user"

#Function for applying dock configuration
createBaseDock()
{
    #Remove all items for logged in user
    $du --remove all --no-restart $userHome
    
    #Adding base items to the dock    
    $du --add '/Applications/Google Chrome.app' --position 1 --no-restart $userHome
    $du --add '/Applications/Safari.app'  --position 2 --no-restart $userHome
    $du --add '/Applications/Comic Life 3.app' --position 3 --no-restart $userHome
    $du --add '/Applications/The Print Shop 4.app' --position 4 --no-restart $userHome
    $du --add '/Applications/KID PIX.app' --position 5 --no-restart $userHome
}

#Function for finishing base dock
finishBaseDock()
{
    #Add local downloads
    $du --add '~/Downloads' --section others --position last --no-restart $userHome

    killall Dock
}

    case $user in
        p|k)
            echo "p or k found"
            createBaseDock
            finishBaseDock
            ;; 
        1)
            echo "1 found"
            createBaseDock
            $du --add '/System/Applications/Photo Booth.app' --position 6 --no-restart $userHome
            finishBaseDock
            ;;
        2)
            echo "2 found"
            createBaseDock
            $du --add '/System/Applications/Photo Booth.app' --position 6 --no-restart $userHome
            finishBaseDock
            ;;
        3)
            echo "3 found"
            createBaseDock
            $du --add '/System/Applications/Photo Booth.app' --position 6 --no-restart $userHome
            $du --add '/Applications/Adobe Photoshop 2020/Adobe Photoshop 2020.app' --position 7 --no-restart $userHome
            finishBaseDock
            ;;
    esac

exit 0

I made some changes to your script because repetition is discouraged in coding (DRY: Dont Repeat Yourself).

In your `case` statement, you were running a `createBaseDock` and a `finishBaseDock` with each case but you can just run the create before the case check and the finish one after.

 

#!/bin/bash

#We need to wait for the dock to actually start
until [[ $(pgrep Dock) ]]; do
    wait
done

#Get the current logged in user that we'll be modifying
if [ ! -z "$3" ]; then
	user=$3
else
	user=$( scutil <<< "show State:/Users/ConsoleUser" | awk '/Name :/ && ! /loginwindow/ { print $3 }' )
fi

#Set variables
du="/usr/local/bin/dockutil"
userHome="/Users/$user"
networkHome="smb://server.com/Students$/$user"

#Function for applying dock configuration
createBaseDock()
{
    #Remove all items for logged in user
    $du --remove all --no-restart $userHome
    
    #Adding base items to the dock    
    $du --add '/Applications/Google Chrome.app' --position 1 --no-restart $userHome
    $du --add '/Applications/Safari.app'  --position 2 --no-restart $userHome
    $du --add '/Applications/Comic Life 3.app' --position 3 --no-restart $userHome
    $du --add '/Applications/The Print Shop 4.app' --position 4 --no-restart $userHome
    $du --add '/Applications/KID PIX.app' --position 5 --no-restart $userHome
}

#Function for finishing base dock
finishBaseDock()
{
    #Add local downloads
    $du --add '~/Downloads' --section others --position last --no-restart $userHome

    killall Dock
}

createBaseDock

    case $user in
        p|k) echo "p or k found";; 
        1)  echo "1 found"
            $du --add '/System/Applications/Photo Booth.app' --position 6 --no-restart $userHome;;
        2)  echo "2 found"
            $du --add '/System/Applications/Photo Booth.app' --position 6 --no-restart $userHome;;
        3)  echo "3 found"
            $du --add '/System/Applications/Photo Booth.app' --position 6 --no-restart $userHome
            $du --add '/Applications/Adobe Photoshop 2020/Adobe Photoshop 2020.app' --position 7 --no-restart $userHome;;
    esac

finishBaseDock

exit 0

 

 

psherotov
Contributor

@Tangentism THANK YOU!!!! Works great!!!!

psherotov
Contributor

@cbrewer THANK YOU! Just wanted to be sure an acknowledge that your solution worked and to of course thank you :-)