Skip navigation

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

785 Views 14 Replies Latest reply: Sep 14, 2013 6:57 AM by RahulYadav RSS
RahulYadav Calculating status...
Currently Being Moderated
Sep 10, 2013 8:06 AM

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.

  • Mark Jalbert Level 5 Level 5 (4,385 points)

    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.

  • naresh akula Calculating status...

    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.

  • Mark Jalbert Level 5 Level 5 (4,385 points)

    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.

  • Mark Jalbert Level 5 Level 5 (4,385 points)

    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.

  • Mark Jalbert Level 5 Level 5 (4,385 points)

    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.

  • Mark Jalbert Level 5 Level 5 (4,385 points)

    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.

  • Mark Jalbert Level 5 Level 5 (4,385 points)

    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

Actions

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.