Adrian Nier Code

Back to overview

Unique path

Last modification: Friday, September 11, 2009 02:22 pm

Generates a unique path for a file with the specified name and suffix in a folder located at the specified path.

Required parameters
Parameter 1 (string)
HFS style path to the parent folder.

Parameter 2 (string)
The name of the file without the suffix.

Parameter 3 (string)
The suffix of the file without the dot.

Implementation

on pathForUniqueFileInFolderWithNameAndSuffix(_parentFolderPath, _fileName, _suffix)
   
   (*
   Generates a unique path for a file with the
   specified name and suffix in a folder located
   at the specified path.
   *)

   
   -- Make sure that all paramters are strings
   set _parentFolderPath to _parentFolderPath as string
   set _fileName to _fileName as string
   set _suffix to _suffix as string
   
   -- Make sure the file does not exist
   set _rNumber to 1
   
   repeat
      if _rNumber is 1 then
         set _tempFilePath to _parentFolderPath & _fileName & "." & _suffix
      else
         set _tempFilePath to _parentFolderPath & _fileName & " " & (_rNumber as string) & "." & _suffix
      end if
      
      tell application "System Events" to if (exists file _tempFilePath) is false then exit repeat
      set _rNumber to _rNumber + 1
   end repeat
   
   return _tempFilePath
   
end pathForUniqueFileInFolderWithNameAndSuffix