Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Script to Copy preference file to all users profiles and to the default template

I have build a package using Apple PackageMaker, this will install an application and a preference (plist) file to the current logged in user. Now I wanted to build a postflight script, which can copy a plist (ex com.test.plist) form an current logged in user to other user account on a machine also to the default user template (/System/Library/user template/...../Library/preferences) so any new user account created will have same preference file copied.

Posted on Sep 10, 2013 8:06 AM

Reply
14 replies

Sep 10, 2013 8:43 AM in response to RahulYadav

also to the default user template (/System/Library/user template/...../Library/preferences) so any new user account created will have same preference file copied

This is a very BAD idea. Third party software should NEVER modify the operating system distribution. Code your application to create a default preference, if one is not already created.

Sep 10, 2013 8:57 AM in response to Mark Jalbert

Mark...thanks for your reply. I do agree's it's a bad idea to change the default template. But, I thought that could be the easied way to get the prefence copied to every new user.

I just created a snapshot based package using Apple packagemaker. This will install the applicationa at (/Applications) and the preference file to the current logged in user.

I am new to scripting, can you share some details to create a default preference for new user? I need to use the same preference file for all user accounts.

Sep 10, 2013 1:35 PM in response to naresh akula

Well, you can try this postinstall script. It assumes that the preference file is located in package.pkg/Content/. I did not test it. Use at your own risk.



#!/bin/sh

ls $3/Users/ | while read USERS; do
if [ -d $3/Users/$USERS/Library/Preferences/ ]; then
    cp $1/Contents/org.mypreference.plist $3/Users/$USERS/Library/Preferences/
    chown $USERS:Staff $3/Users/$USERS/Library/Preferences/org.mypreference.plist
done
exit 0

The package must be installed by an admin user.

Sep 13, 2013 8:04 AM in response to Mark Jalbert

Hi Mark...thanks for the script. My package place the script into the current logged in user library. So I made some change to the package, and now instead of putting the preference file in user profile, I put it /private/var and from here I would like to copy it to all the users on the machine.

So, I tried to change the location in your script as below, but it doesn't seems to be working as expected. It comes up with the following error

line 7: syntax error: near unexpected end token 'done'

line 7: 'done'



#!/bin/sh
ls $3/Users/ | while read USERS; do
if [ -d $3/Users/$USERS/Library/Preferences/ ]; then
cp $1/private/var/org.mypreference.plist $3/Users/$USERS/Library/Preferences/
chown $USERS:Staff $3/Users/$USERS/Library/Preferences/org.mypreference.plist
done
exit 0

Sep 13, 2013 9:29 AM in response to RahulYadav

Hello Rahul,

My bad, I forgot to close the conditional statement. The construct should look like this->


if test; then
 do something
fi

Here's a list of the shell's arguements in package


$0 - Script path
$1 - Package path
$2 - Target location
$3 - Target volume
$4 - /

It been a while since I created a package. I can't remember if you need the trailing "/" in the path with an arguement. If I find some time I'll test.

Scripts shouldn't be changing ownership according to Apple so let's modify what I gave you.


#!/bin/sh

ls $3/Users/ | while read USERS; do
     if [ -d $3/Users/$USERS/Library/Preferences/ ]; then
          /usr/bin/install -m 600 -u $USERS -g Staff $3/private/var/org.mypreference.plist $3/Users/$USERS/Library/Preferences/
     fi
done
exit 0

Again, the user who installs your package needs to be an admin user.

Sep 13, 2013 11:21 AM in response to Mark Jalbert

Hello Mark, Thanks again for your help. I am still unable to get it worked. When I ran the script it comes up with the following error:


/usr/bin/install: illegal option -- u

usage: install [-bCcpSsv] [-B suffix] [-f flags] [-g group] [-m mode]

[-o owner] file1 file2

install [-bCcpSsv] [-B suffix] [-f flags] [-g group] [-m mode]

[-o owner] file1 ... fileN directory

install -d [-v] [-g group] [-m mode] [-o owner] directory ...

/usr/bin/install: illegal option -- u

Sep 13, 2013 11:30 AM in response to RahulYadav

Once again, a typo-


/usr/bin/install -m 600 -u $USERS -g Staff $3/private/var/org.mypreference.plist $3/Users/$USERS/Library/Preferences/
should be
/usr/bin/install -m 600 -o $USERS -g Staff $3/private/var/org.mypreference.plist $3/Users/$USERS/Library/Preferences/

I made one test package and the postinstall script failed. Will try again later. Let us know if you have any luck.

Sep 14, 2013 4:30 AM in response to Mark Jalbert

Hello Mark..I tried it with the new script as you said, this time it comes with below syntax error :


line 4: syntax error near unexpected token `then'

line 4: ` if [ -d $3/Users/$USERS/Library/Preferences/ ]; then'


Below is the script

#!/bin/sh

ls $3/Users/ | while read USERS; do
     if [ -d $3/Users/$USERS/Library/Preferences/ ]; then
          /usr/bin/install -m 600 -o $USERS -g Staff $3/private/var/org.mypreference.plist $3/Users/$USERS/Library/Preferences/
     fi
done
exit 0

Sep 14, 2013 4:52 AM in response to RahulYadav

Hi Mark...here is another script which work well if I have to copy my preference file from an account named as "admin" or some other account. However, it not certain that all Mac machine will have a account named as "admin". This is why I am looking for a way to copy the preference file from /private/var location to all user accounts on a mac.



#!/bin/sh


localUsers=$( dscl . list /Users UniqueID | awk '$2 >= 501 {print $1}' | grep -v admin )


for userName in "$localUsers"; do
     cp /Users/admin/Library/Preferences/org.mypreference.plist /Users/$userName/Library/Preferences/
     chown $userName /Users/$userName/Library/Preferences/org.mypreference.plist
done


I tried to update it as suggest in some other forum like this, still no luck 😟



#!/bin/sh


UserAccounts=`dscl . list /Users UniqueID | awk '$2 > 500 { print $1 }'`
for userName in "$UserAccounts"; do
     cp /private/var/org.mypreference.plist /Users/$userName/Library/Preferences/
     chown $userName /Users/$userName/Library/Preferences/org.mypreference.plist
done


When I run the above script it comes up with the following error:


cp [-R [-H | -L | -P]] [-fi | -n] [-apvX] source_file target_file

cp [-R [-H | -L | -P]] [-fi | -n] [-apvX] source_file ... target_directory

chown: admin: No such file or directory

chown: admin/Library/Preferences/org.mypreference.plist: No such file or directory

Sep 14, 2013 6:00 AM in response to RahulYadav

I got the postinstall script to work in a package with one modification.


#!/bin/sh

ls $3/Users/ | while read USERS; do
     if [ -d $3/Users/$USERS/Library/Preferences ]; then
          /usr/bin/install -m 600 -o $USERS -g Staff $3/private/var/org.mypreference.plist $3/Users/$USERS/Library/Preferences/
     fi
done
exit 0

An admin users is any user who belongs to the group admin not necessarily a user named admin. I have one login account on my computer so I can't test whether the plist will install in other accounts. What version of OS X are you using to make your package? I'm using 10.6.

Sep 14, 2013 6:31 AM in response to Mark Jalbert

I just tried to build the packge with the above script, the installation of package fails, while executing the postflight script. The package installs the application to /Application and then copies the preference file to /private/var and then it fails while executnig the script. The error is same

line 4: syntax error near unexpected token `then'

line 4: ` if [ -d $3/Users/$USERS/Library/Preferences ]; then'


Currently I am testing it on 10.8 machine to build my package. On Monday, Once I get back to work I will give a try on 10.6 as well.

What does the command "

/usr/bin/install -m 600
" this performs??

Thank Again for all your efforts in helping me out...Have a great weekend 🙂

Sep 14, 2013 6:50 AM in response to RahulYadav

Try quoting like this


#!/bin/sh

ls "$3/Users/" | while read USERS; do
     if [ -d "$3/Users/$USERS/Library/Preferences" ]; then
          /usr/bin/install -m 600 -o $USERS -g Staff "$3/private/var/org.mypreference.plist" "$3/Users/$USERS/Library/Preferences/"
     fi
done
exit 0

install copies the target file to the destination directory, the -m sets the mode to rw for the owner, -o set the owner, -g sets the group

Script to Copy preference file to all users profiles and to the default template

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.