Adrian Nier Code

Back to overview

Path for temporary file

Last modification: Wednesday, February 03, 2010 03:10 pm

Generates a unique path for a file in the current user's temporary items folder. Takes a single argument that can be set to the name of a subfolder or false to create no subfolder.

Required parameters
Parameter 1 (string)
The name of a subfolder in the temporary items folder. Pass false to create no subfolder.

Implementation

on pathForTemporaryFile(_folderName)
   
   (*
   Generates a unique path for a file in the
   current user's temporary items folder.
   Takes a single argument that can be set
   to the name of a subfolder or false to
   create no subfolder.
   *)

   
   -- Create timestamp
   set _date to current date
   
   set _month to month of _date as integer
   set _day to day of _date
   set _year to year of _date as string
   
   set _time to (time of _date)
   set _hour to _time div (60 * 60)
   set _minute to _time mod (60 * 60) div 60
   set _second to _time mod 60
   
   if _month is less than 10 then
      set _month to "0" & (_month as string)
   else
      set _month to _month as string
   end if
   
   if _day is less than 10 then
      set _day to "0" & (_day as string)
   else
      set _day to _day as string
   end if
   
   if _hour is less than 10 then
      set _hour to "0" & (_hour as string)
   else
      set _hour to _hour as string
   end if
   
   if _minute is less than 10 then
      set _minute to "0" & (_minute as string)
   else
      set _minute to _minute as string
   end if
   
   if _second is less than 10 then
      set _second to "0" & (_second as string)
   else
      set _second to _second as string
   end if
   
   
   set _timestamp to _year & "-" & _month & "-" & _day & "_" & _hour & "-" & _minute & "-" & _second
   
   
   -- Generate pseudorandom numbers
   set _rand1 to (round (random number from 100 to 999)) as string
   set _rand2 to (round (random number from 100 to 999)) as string
   set _rand3 to (round (random number from 100 to 999)) as string
   set _rand4 to (round (random number from 100 to 999)) as string
   set _randomString to _rand1 & "-" & _rand2 & "-" & _rand3 & "-" & _rand4
   
   -- Create file name
   set _fileName to ((_timestamp & "_" & _randomString) as string)
   
   -- Get the path to the parent folder
   set _temporaryFolderPath to path to temporary items folder from user domain as string
   if _folderName is false then
      set _parentFolderPath to _temporaryFolderPath
   else
      set _parentFolderPath to _temporaryFolderPath & _folderName & ":"
      tell application "System Events" to if (exists folder _parentFolderPath) is false then make new folder at the end of folders of folder _temporaryFolderPath with properties {name:_folderName}
   end if
   
   -- Make sure the file does not exist
   set _rNumber to 1
   
   repeat
      if _rNumber is 1 then
         set _tempFilePath to _parentFolderPath & _fileName
      else
         set _tempFilePath to _parentFolderPath & _fileName & "_" & (_rNumber as string)
      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 pathForTemporaryFile