Adrian Nier Code

Back to overview

Create new file in current folder

Last modification: Tuesday, April 13, 2010 02:17 pm

Creates a new file in the current folder as shown by the frontmost Finder window. If no Finder window exists then the desktop is used as the parent for the new file. By editing the script and specifying custom values for pSUFFIX and pAPPLICATION, you can predetermine which suffix is used or which application opens the new file.

Implementation

property pSUFFIX : false -- suffix of the file to be created, or false
property pAPPLICATION : false -- name of the application to open the file with, or false

on run
   
   -- Get the path to the folder shown in the frontmost Finder window
   set _folderPath to pathToFrontmostFinderDirectory()
   if _folderPath is false then return false
   
   -- Ask for the file name
   set _fileName to askForFilename(localizedString("file_name_prompt", {pSUFFIX}), localizedString("untitled_file_name", {pSUFFIX}))
   if _fileName is false then return false
   
   -- Make sure the file does not exist
   set _filePath to uniquePathForFileWithName_andSuffix_inFolderAtPath(_fileName, pSUFFIX, _folderPath)
   if _filePath is false then return false
   
   -- Create an empty file in the folder
   set _filePath to createEmptyFileAtPath(_filePath)
   if _filePath is false then return false
   
   if pAPPLICATION is false then
      -- Open the file with the default application
      tell application "Finder" to open file _filePath
   else
      -- Open the new file in the specified application
      tell application pAPPLICATION
         activate
         open file _filePath
      end tell
   end if
   
   -- Reveal the file in the Finder
   tell application "Finder" to reveal file _filePath
   
   return true
   
end run

on createEmptyFileAtPath(_filePath)
   
   -- Open the file
   try
      open for access file _filePath with write permission
   on error _eMsg number _eNum
      beep
      display alert localizedString("could_not_create_file", null) message _eMsg & " (" & (_eNum as string) & ")" buttons {"OK"} as warning
      return false
   end try
   
   -- Write to the file
   try
      write "" to file _filePath
   on error _eMsg number _eNum
      close access file _filePath
      beep
      display alert localizedString("could_not_create_file", null) message _eMsg & " (" & (_eNum as string) & ")" buttons {"OK"} as warning
      return false
   end try
   
   -- Close the file
   close access file _filePath
   
   return _filePath
   
end createEmptyFileAtPath

on uniquePathForFileWithName_andSuffix_inFolderAtPath(_originalName, _suffix, _folderPath)
   
   -- Get the suffix from the filename
   if _suffix is false then
      if _originalName contains "." then
         set _prvDlmt to text item delimiters
         set text item delimiters to "."
         set _suffix to text item -1 of _originalName
         if _suffix contains " " then
            set _suffix to false
         else
            set _originalName to text items 1 thru -2 of _originalName as string
         end if
         set text item delimiters to "."
      end if
   end if
   
   -- Create an unique version of the file name
   set _uniqueFileName to nameForUniqueFileInFolderAtPath_withName_andSuffix(_folderPath, _originalName, _suffix)
   
   
   repeat until _uniqueFileName is _originalName
      -- Ask for a different filename
      set _originalName to askForFilename(localizedString("taken_file_name_prompt", null), _uniqueFileName)
      if _originalName is false then return false
      
      -- Check if the name is unique this time
      set _uniqueFileName to nameForUniqueFileInFolderAtPath_withName_andSuffix(_folderPath, _originalName, _suffix)
      
   end repeat
   
   if _suffix is false then
      return _folderPath & _uniqueFileName
   else
      return _folderPath & _uniqueFileName & "." & _suffix
   end if
   
end uniquePathForFileWithName_andSuffix_inFolderAtPath

on nameForUniqueFileInFolderAtPath_withName_andSuffix(_parentFolderPath, _fileName, _suffix)
   
   
   -- Make sure the file does not exist
   set _rNumber to 1
   repeat
      
      -- Generate file name
      if _rNumber is 1 then
         set _uniqueFileName to _fileName
      else
         set _uniqueFileName to _fileName & "_" & (_rNumber as string)
      end if
      
      if _suffix is false then
         set _tempFilePath to _parentFolderPath & _uniqueFileName
      else
         set _tempFilePath to _parentFolderPath & _uniqueFileName & "." & _suffix
      end if
      
      -- Check if the file exists
      tell application "System Events" to if (exists file _tempFilePath) is false then exit repeat
      
      set _rNumber to _rNumber + 1
   end repeat
   
   return _uniqueFileName
   
end nameForUniqueFileInFolderAtPath_withName_andSuffix

on askForFilename(_prompt, _suggestion)
   
   -- Display a dialog asking the user for a filename
   activate
   set _fileName to ""
   repeat until _fileName is not ""
      try
         set _fileName to text returned of (display dialog _prompt default answer _suggestion buttons {localizedString("cancel_button_name", null), localizedString("create_button_name", null)} default button 2)
      on error _eMsg number _eNum
         return false
      end try
   end repeat
   
   return _fileName
   
end askForFilename

on pathToFrontmostFinderDirectory()
   -- Get the path to the folder shown in the frontmost Finder window
   try
      
      -- Check if the Finder is running
      tell application "System Events" to set _running to exists process "Finder"
      if _running is false then
         error localizedString("finder_not_running", null)
      end if
      
      
      
      -- Check if there’s a Finder window
      tell application "Finder" to set _windowExists to exists Finder window 1
      
      if _windowExists then
         
         -- Check the class of what is displayed in the Finder window
         tell application "Finder" to set _targetClass to class of (target of Finder window 1) as string
         
         
         if _targetClass is "computer-object" then
            -- Cannot create files in the computer view
            error localizedString("cant_create_error", null)
            
         else
            -- Return the path for the folder displayed in the Finder window
            try
               tell application "Finder" to return (target of Finder window 1) as string
            on error _eMsg number _eNum
               error localizedString("could_not_determine_folder", null) number _eNum
            end try
         end if
      else
         
         -- There’s no Finder window, return the path to the desktop folder
         return (path to desktop folder from user domain) as string
         
      end if
      
      
   on error _eMsg number _eNum
      beep
      display alert localizedString("error_message_title", null) message _eMsg & " (" & (_eNum as string) & ")" buttons {"OK"} as warning
      return false
   end try
   
end pathToFrontmostFinderDirectory

on localizedString(_key, _vars)
   -- Given a particular key, return the corresponding language specific string
   set _lang to systemLanguage()
   
   if _key is "cant_create_error" then
      if _lang is "de" then
         return "Sie können in dem aktuellen Finder Fenster keine neuen Dateien anlegen."
      else
         return "You cannot create new files in the current Finder window."
      end if
   end if
   
   if _key is "error_message_title" then
      if _lang is "de" then
         return "Ein Fehler ist aufgetreten:"
      else
         return "An error occurred:"
      end if
   end if
   
   if _key is "could_not_determine_folder" then
      if _lang is "de" then
         return "Das Finder Fenster scheint keinen Ordner darzustellen."
      else
         return "The Finder window does not seem to show a folder."
      end if
   end if
   
   if _key is "finder_not_running" then
      if _lang is "de" then
         return "Das Programm Finder ist beendet."
      else
         return "The Finder is not running."
      end if
   end if
   
   if _key is "file_name_prompt" then
      if (item 1 of _vars) is false then
         if _lang is "de" then
            return "Geben Sie den Namen und die Endung für die Datei ein:"
         else
            return "Enter the name and suffix for the file:"
         end if
      else
         if _lang is "de" then
            return "Geben Sie den Namen für die ." & (item 1 of _vars) & " Datei ein:"
         else
            return "Enter the name for the ." & (item 1 of _vars) & " file:"
         end if
      end if
   end if
   
   if _key is "taken_file_name_prompt" then
      if _lang is "de" then
         return "Der von Ihnen angegebene Dateiname wird bereits verwendet. Bitte geben Sie einen anderen ein:"
      else
         return "The filename you specified is already taken. Please enter a different name:"
      end if
   end if
   
   if _key is "cancel_button_name" then
      if _lang is "de" then
         return "Abbruch"
      else
         return "Cancel"
      end if
   end if
   
   
   if _key is "create_button_name" then
      if _lang is "de" then
         return "Erstellen"
      else
         return "Create"
      end if
   end if
   
   
   if _key is "untitled_file_name" then
      if (item 1 of _vars) is false then
         if _lang is "de" then
            return "Unbenannt.txt"
         else
            return "Untitled.txt"
         end if
      else
         if _lang is "de" then
            return "Unbenannt"
         else
            return "Untitled"
         end if
      end if
   end if
   
   if _key is "finder_not_running" then
      if _lang is "de" then
         return "Das Programm Finder ist beendet."
      else
         return "The Finder is not running."
      end if
   end if
   
   if _key is "could_not_create_file" then
      if _lang is "de" then
         return "Die Datei konnte nicht angelegt werden."
      else
         return "Could not create the file."
      end if
   end if
   
   return _key
end localizedString

on systemLanguage()
   try
      return first word of (do shell script "defaults read NSGlobalDomain AppleLanguages")
   on error
      return "en"
   end try
end systemLanguage