Setting Safari Defaults to show favorites toolbar on login per user

GabeShack
Valued Contributor III

I've been trying to script a plist change to com.Safari.plist that sets the value for the favorites toolbar show to true.

Here is what i'm running, and what it returns:

#!/bin/sh
defaults write /Users/$3/Library/Preferences/com.apple.Safari.plist ShowFavoritesBar-v2 -bool true
defaults read com.apple.Safari
exit 0

Script result: {
"ShowFavoritesBar-v2" = 1;
}

I think I'm missing that step that tells the OS to ignore the cashed version of this file and use the modified one.

Gabe Shackney
Princeton Public Schools

Gabe Shackney
Princeton Public Schools
2 ACCEPTED SOLUTIONS

bpavlov
Honored Contributor

killall cfprefsd
after writing to plist

View solution in original post

GabeShack
Valued Contributor III

Thanks all so it looks like this and now works:

#!/bin/sh
defaults write /Users/$3/Library/Preferences/com.apple.Safari.plist ShowFavoritesBar-v2 -bool true
defaults write /Users/$3/Library/Preferences/com.apple.Safari NewWindowBehavior -int 0
killall cfprefsd
chown $3 /Users/$3/Library/Preferences/com.apple.Safari.plist
exit 0

Gabe Shackney
Princeton Public Schools

Gabe Shackney
Princeton Public Schools

View solution in original post

10 REPLIES 10

bpavlov
Honored Contributor

killall cfprefsd
after writing to plist

GabeShack
Valued Contributor III

Thanks, knew I didn't know that.

Gabe Shackney
Princeton Public Schools

Gabe Shackney
Princeton Public Schools

GabeShack
Valued Contributor III

So actually this is causing the file to be owned by the system and takes permission away from the user. Any ideas?

Gabe Shackney
Princeton Public Schools

Not applicable

Scripts in Casper run as root, so you might need to make sure the .plist gets chowned to the current user.

Have you tried capturing this preference and adding to the User Template folder instead of scripting it?

GabeShack
Valued Contributor III

Yes but my problem is these are machines that we are transferring user folders to as upgrades and then it wipes the template settings so I have to script it. So I'll have to run this with some chown parameters I guess.

Gabe Shackney
Princeton Public Schools

GabeShack
Valued Contributor III

Thanks all so it looks like this and now works:

#!/bin/sh
defaults write /Users/$3/Library/Preferences/com.apple.Safari.plist ShowFavoritesBar-v2 -bool true
defaults write /Users/$3/Library/Preferences/com.apple.Safari NewWindowBehavior -int 0
killall cfprefsd
chown $3 /Users/$3/Library/Preferences/com.apple.Safari.plist
exit 0

Gabe Shackney
Princeton Public Schools

Gabe Shackney
Princeton Public Schools

sepiemoini
Contributor III
Contributor III

Was hoping that the above workflow would work for AllowJavaScriptFromAppleEvents but sadly this doesn't appear to be working. To simplify matters, I've removed the $3 in my local script and specified the account being used and still no dice :(

Any ideas, @gshackney? Worth noting that I'm running 10.12.2 and hope to incorporate this with other various 10.12.x builds.

#!/bin/bash

defaults write /Users/$3/Library/Preferences/com.apple.Safari.plist AllowJavaScriptFromAppleEvents -bool TRUE
killall cfprefsd
chown $3 /Users/$3/Library/Preferences/com.apple.Safari.plist

exit 0

GabeShack
Valued Contributor III

@sepiemoini Isn't that setting true by default? That and allow javascript from smart search field is also defaulted to true I believe. I also don't see the key for that feature in the plist, whether enabled or disabled.

Gabe Shackney
Princeton Public Schools

Gabe Shackney
Princeton Public Schools

sepiemoini
Contributor III
Contributor III

@gshackney Yup, the key is no longer listed in the plist by default so giving up on this as I am imaging Apple has done away with being able to modify that aspect of the file.

Luke_cater
New Contributor III

Something I cooked up, feel free to tweak to your org. 

#!/bin/bash

plistPath="/Library/LaunchAgents/com.safari.configuration.agent.plist"
scriptPath="/Library/Scripts/safari.configuration.sh"

defaults write "$plistPath" Label -string "com.safari.configuration.agent"
defaults write "$plistPath" ProgramArguments -array -string /bin/sh -string "$scriptPath"
defaults write "$plistPath" RunAtLoad -boolean yes
chown root:wheel "$plistPath"
chmod 655 "$plistPath"


cat << 'EOF1' > "$scriptPath"
#!/bin/bash

loggedInUser=$(who | awk '/console/{print $1}')
safariDir="/Users/$loggedInUser/Library/Safari"

if [ ! -d "$safariDir" ]; then

mkdir -p /Users/$loggedInUser/Library/Safari

# Define bookmarks list
arrList=$(cat << EOF
Website1,https://www.website1.com
Website2,https://www.website2.com
Website3,https://www.website3.com
EOF
)

# Backup and set internal field separator and set variables
IFS_old=$IFS
IFS=$'\n'
#

PLISTBUDDY=/usr/libexec/PlistBuddy

# Add multiple plists to modify to this array 
arrPlist=()
arrPlist+=("/Users/${loggedInUser}/Library/Safari/Bookmarks.plist")
arrPlist+=("/System/Library/User Template/English.lproj/Library/Safari/Bookmarks.plist")

# Function for creating bookmark entries
function addBookmarkItem {
	echo "Name: ${2}"
	echo "URL: ${3}"
	if [ ${1} -eq 0 ]; then
		echo 'Adding Array'
		${PLISTBUDDY} -c "Add :Children:1:Children array" "${PLIST}" 
	fi
	echo 'Adding Dictionary items'
	${PLISTBUDDY} -c "Add :Children:1:Children:${1} dict" "${PLIST}"
	${PLISTBUDDY} -c "Add :Children:1:Children:${1}:URIDictionary dict" "${PLIST}" 
	${PLISTBUDDY} -c "Add :Children:1:Children:${1}:URIDictionary:title string ${2}" "${PLIST}" 
	${PLISTBUDDY} -c "Add :Children:1:Children:${1}:URLString string ${3}" "${PLIST}" 
	${PLISTBUDDY} -c "Add :Children:1:Children:${1}:WebBookmarkType string WebBookmarkTypeLeaf" "${PLIST}" 
	echo ''
}

# Function for creating a new plist
function createPlist {
	FOLDER="${PLIST/\/Bookmarks.plist/}"
	if [ ! -d "${FOLDER}" ]; then
		echo 'Creating Safari preferences folder.'
		/bin/mkdir -p "${FOLDER}"
		/usr/bin/xattr -r -d com.apple.quarantine "${FOLDER}"
	fi
	echo "Creating plist: $PLIST"
	${PLISTBUDDY} -c 'Add :Sync dict' "${PLIST}"
	${PLISTBUDDY} -c 'Add :CloudKitMigrationState integer 1' "${PLIST}" 
	${PLISTBUDDY} -c 'Add :Title string' "${PLIST}" 
	${PLISTBUDDY} -c 'Add :WebBookmarkFileVersion integer 1' "${PLIST}" 
	${PLISTBUDDY} -c 'Add :WebBookmarkType string WebBookmarkTypeList' "${PLIST}"
	${PLISTBUDDY} -c 'Add :Children array' "${PLIST}"
	${PLISTBUDDY} -c 'Add :Children:0 dict' "${PLIST}"
	${PLISTBUDDY} -c 'Add :Children:0:Title string History' "${PLIST}"
	${PLISTBUDDY} -c 'Add :Children:0:WebBookmarkIdentifier string History' "${PLIST}"
	${PLISTBUDDY} -c 'Add :Children:0:WebBookmarkType string WebBookmarkTypeProxy' "${PLIST}"
	${PLISTBUDDY} -c 'Add :Children:1 array' "${PLIST}"
	${PLISTBUDDY} -c 'Add :Children:1 dict' "${PLIST}"
	${PLISTBUDDY} -c 'Add :Children:1:Title string BookmarksBar' "${PLIST}"
	${PLISTBUDDY} -c 'Add :Children:1:WebBookmarkType string WebBookmarkTypeList' "${PLIST}"
	${PLISTBUDDY} -c 'Add :Children:2 dict' "${PLIST}"
	${PLISTBUDDY} -c 'Add :Children:2:Title string BookmarksMenu' "${PLIST}"
	${PLISTBUDDY} -c 'Add :Children:2:WebBookmarkType string WebBookmarkTypeList' "${PLIST}"
}

# Main script logic
for PLIST in ${arrPlist[@]}; do	
	i=0
	echo '' && echo "Current PLIST: ${PLIST}"
	if [ ! -f "${PLIST}" ]; then
		createPlist
	fi
	for items in ${arrList}; do
		IFS=$',' read -r title url <<< "$items"
		addBookmarkItem ${i} "${title}" "${url}"
		(( i += 1 ))
	done
	/usr/bin/plutil -convert binary1 "${PLIST}"
	/bin/chmod 644 "${PLIST}"
done
IFS=${IFS_old}

defaults write /Users/$loggedInUser/Library/Containers/com.apple.Safari/Data/Library/Preferences/com.apple.Safari.plist ShowFavoritesBar-v2 -bool true
defaults write /Users/$loggedInUser/Library/Containers/com.apple.Safari/Data/Library/Preferences/com.apple.Safari NewWindowBehavior -int 0
killall cfprefsd

fi
EOF1
chown root:wheel "$scriptPath"
chmod 777 "$scriptPath"