Adrian Nier Code

Back to overview

Copy file to user template

Last modification: Friday, May 01, 2009 10:04 am

Copies a specified file from the current user’s home folder to each localization in /System/Library/User Template. Takes two strings as parameters.

Required parameters
Parameter 1 (string)
HFS style relative path to the file that needs to be copied to the user template

Parameter 2 (string)
POSIX style relative path to the folder inside each user template localization

Implementation

on copyFileToUserTemplate(_relativeFilePath, _relativeFolderPPath)
   
   (*
   Copies a specified file from the current user’s home folder
   to each localization in /System/Library/User Template.
   Takes two strings as parameters:
   Parameter 1 (string)
   HFS style relative path to the file that needs to be copied to the user template
   Parameter 2 (string)
   POSIX style relative path to the folder inside each user template localization
   *)

   
   -- Get the file name from the relative file path
   set _prvDlmt to AppleScript's text item delimiters
   set AppleScript's text item delimiters to ":"
   set _fileName to text item -1 of _relativeFilePath
   set AppleScript's text item delimiters to _prvDlmt
   
   -- Get the quoted posix path to the file
   set _homeFolder to (path to home folder as string)
   set _filePath to _homeFolder & _relativeFilePath
   set _fileQPPath to quoted form of (POSIX path of _filePath)
   
   -- Get a list of posix paths for the target folders in the user template localizations
   set _cmd to "find /System/Library/User\\ Template/* -path '*/" & _relativeFolderPPath & "'"
   set _cmdResult to do shell script _cmd with administrator privileges
   set _foundPrefFoldersPPath to paragraphs of _cmdResult
   
   -- Create the target folder if necessary
   if _foundPrefFoldersPPath is {} then
      
      -- Get the path components from the relative folder path
      set _prvDlmt to AppleScript's text item delimiters
      set AppleScript's text item delimiters to "/"
      set _pathComponents to text items of _relativeFolderPPath
      set AppleScript's text item delimiters to _prvDlmt
      
      -- Make sure each path components exists as a folder
      set _currentFolder to ""
      repeat with _i from 1 to count of _pathComponents
         
         if _i is 1 then
            set _cmd to "find /System/Library/User\\ Template/* "
            set _cmd to _cmd & "-depth 0 "
            set _cmd to _cmd & "-exec mkdir {}/" & item _i of _pathComponents & " \\;"
         else
            set _cmd to "find /System/Library/User\\ Template/* "
            set _cmd to _cmd & "-path '*" & _currentFolder & "' "
            set _cmd to _cmd & "-exec mkdir {}/" & item _i of _pathComponents & " \\;"
         end if
         
         set _cmdResult to do shell script _cmd with administrator privileges
         
         set _currentFolder to _currentFolder & "/" & item _i of _pathComponents
      end repeat
      
      -- Get a list of posix paths for the target folders in the user template localizations
      set _cmd to "find /System/Library/User\\ Template/* -path '*/" & _relativeFolderPPath & "'"
      set _cmdResult to do shell script _cmd with administrator privileges
      set _foundPrefFoldersPPath to paragraphs of _cmdResult
      
   end if
   
   -- Copy the preference file to each Preferences folder
   repeat with _i from 1 to count of _foundPrefFoldersPPath
      
      set _targetFilePPath to item _i of _foundPrefFoldersPPath & "/" & _fileName
      set _targetFileQPPath to quoted form of _targetFilePPath
      
      set _cmd to "ditto " & _fileQPPath & " " & _targetFileQPPath
      do shell script _cmd with administrator privileges
      
   end repeat
   
end copyFileToUserTemplate