Adrian Nier Code

Back to overview

Read from file

Last modification: Monday, August 25, 2008 12:47 am

Reads contents of a file using AppleScript’s own ‘read’ command. Takes a single record as argument.

Required parameters
__hfsPath (string)
HFS style path to the file that needs to be read.

__contentType (class)
Class name for the content type to be read [e.g. string, record, etc.].

Optional parameters
__silenceErrors (boolean)
Do not raise AppleScript errors, fail silently. Default: false

__logFilePath (string)
HFS style path to log file. Overrides gLOG_FILE_PATH. If only a file name is specified the default Logs folder inside the user’s Library is used. Default: false

__debugMode (boolean)
Log debug messages. Requires __logFilePath to be set. Overrides gDEBUG_MODE. Default: false

Optional Globals
gLOG_FILE_PATH (string)
If set, the value will be used for the path to the log file.

gDEBUG_MODE (boolean)
If set, the value will be used to enable debug messages.

gLAST_ERROR_MESSAGE (string)
Variable in which error message is stored.

gLAST_ERROR_NUMBER (integer)
Variable in which error number is stored.
 

Function Calls

-- Basic call, provide path and content type
readFromFile({__hfsPath:_filePath, __contentType:string})

-- Don't raise AppleScript errors, just return negative result (i.e. "", {}, ...)
readFromFile({__hfsPath:_filePath, __contentType:string, __silenceErrors:true})

Implementation

on readFromFile(_args)
   try
      set _functionName to "readFromFile"
      
      -- Find out if debug mode needs to be enabled
      try
         set _debugMode to (__debugMode of _args)
      on error
         try
            set _debugMode to gDEBUG_MODE
         on error
            set _debugMode to false
         end try
      end try
      
      -- Set the path to the log file
      try
         set _logFilePath to (__logFilePath of _args)
         if _logFilePath does not contain ":" then
            set _logFilePath to (path to home folder as string) & "Library:Logs:" & _logFilePath
         end if
      on error
         try
            set _logFilePath to gLOG_FILE_PATH
         on error
            set _logFilePath to false
         end try
      end try
      
      
      -- Setup groups of various content types
      set _stringTypes to {string, text, Unicode text, «class utf8»}
      set _listTypes to {list, record}
      set _numericTypes to {integer, real}
      
      -- Debug message
      if _debugMode and (_logFilePath is not false) then do shell script "echo " & ¬
         quoted form of ((do shell script "date +\"%F %T %Z\"") & ¬
         tab & "[Debug] " & _functionName & "(): Checking arguments") & ¬
         " >> " & quoted form of (POSIX path of _logFilePath) & " 2>/dev/null &"
      
      
      -- Check arguments
      try
         set _silenceErrors to (__silenceErrors of _args)
      on error
         set _silenceErrors to false
      end try
      
      try
         set _hfsPath to (__hfsPath of _args)
      on error
         set _hfsPath to ""
         error "No path to read from specified."
      end try
      
      if class of _hfsPath is not in {string, text, Unicode text, «class utf8»} then error "Wrong data type for path to read from."
      if _hfsPath is "" then error "Empty string for path to file to read from."
      if _hfsPath does not contain ":" then error "No valid path to read from specified."
      
      
      try
         set _contentType to (__contentType of _args)
      on error
         set _contentType to "undefined"
         error "No content type specified."
      end try
      
      -- Debug message
      if _debugMode and (_logFilePath is not false) then do shell script "echo " & ¬
         quoted form of ((do shell script "date +\"%F %T %Z\"") & ¬
         tab & "[Debug] " & _functionName & "(\"" & _hfsPath & "\"): Content type: " & ¬
         (_contentType as string)) & " >> " & quoted form of (POSIX path of _logFilePath) & ¬
         " 2>/dev/null &"
      
      -- Debug message
      if _debugMode and (_logFilePath is not false) then do shell script "echo " & ¬
         quoted form of ((do shell script "date +\"%F %T %Z\"") & ¬
         tab & "[Debug] " & _functionName & "(\"" & _hfsPath & "\"): Checking if file exists") & ¬
         " >> " & quoted form of (POSIX path of _logFilePath) & " 2>/dev/null &"
      
      -- Check if the file exists
      try
         tell application "System Events"
            set _originalFile to file _hfsPath
            if (exists _originalFile) is false then error 1
         end tell
      on error
         error "No file exists at the specified path."
      end try
      
      
      -- Debug message
      if _debugMode and (_logFilePath is not false) then do shell script "echo " & ¬
         quoted form of ((do shell script "date +\"%F %T %Z\"") & ¬
         tab & "[Debug] " & _functionName & "(\"" & _hfsPath & "\"): Opening file") & ¬
         " >> " & quoted form of (POSIX path of _logFilePath) & " 2>/dev/null &"
      
      -- Open file for reading
      try
         open for access file _hfsPath
      on error _eMessage number _eNumber
         error "Could not open file: " & _eMessage number _eNumber
      end try
      
      -- Debug message
      if _debugMode and (_logFilePath is not false) then do shell script "echo " & ¬
         quoted form of ((do shell script "date +\"%F %T %Z\"") & ¬
         tab & "[Debug] " & _functionName & "(\"" & _hfsPath & "\"): Reading from file") & ¬
         " >> " & quoted form of (POSIX path of _logFilePath) & " 2>/dev/null &"
      
      try
         set _fileContents to read file _hfsPath as _contentType
      on error _eMessage number _eNumber
         try
            close access file _hfsPath
         end try
         error "Error while trying to read file: " & _eMessage number _eNumber
      end try
      
      -- Debug message
      if _debugMode and (_logFilePath is not false) then
         if (_contentType is in _stringTypes) then
            set _readInfo to "Read characters: " & ((count of characters in _fileContents) as string)
         else if (_contentType is in _listTypes) then
            set _readInfo to "Read items: " & ((count of _fileContents) as string)
         else
            set _readInfo to "Read value: " & (_fileContents as string)
         end if
         
         do shell script "echo " & ¬
            quoted form of ((do shell script "date +\"%F %T %Z\"") & ¬
            tab & "[Debug] " & _functionName & "(\"" & _hfsPath & "\"): " & _readInfo) & ¬
            " >> " & quoted form of (POSIX path of _logFilePath) & " 2>/dev/null &"
      end if
      
      -- Debug message
      if _debugMode and (_logFilePath is not false) then do shell script "echo " & ¬
         quoted form of ((do shell script "date +\"%F %T %Z\"") & ¬
         tab & "[Debug] " & _functionName & "(\"" & _hfsPath & "\"): Closing file") & ¬
         " >> " & quoted form of (POSIX path of _logFilePath) & " 2>/dev/null &"
      
      try
         close access file _hfsPath
      end try
      
      return _fileContents
      
      
   on error _eMessage number _eNumber
      
      set gLAST_ERROR_MESSAGE to _eMessage
      set gLAST_ERROR_NUMBER to _eNumber
      
      -- Log to file if a path has been specified
      if _logFilePath is not false then
         try
            set _logMessage to "[Error] " & _functionName & "(\"" & _hfsPath & "\"): " & ¬
               _eMessage & " (" & (_eNumber as string) & ")"
            do shell script "echo " & quoted form of ((do shell script "date +\"%F %T %Z\"") & ¬
               tab & _logMessage) & " >> " & quoted form of (POSIX path of _logFilePath) & ¬
               " 2>/dev/null &"
         end try
      end if
      
      -- Raise an error if silencing errors is disabled
      if (_silenceErrors is false) then
         set _eMessage to _functionName & "(\"" & _hfsPath & "\"): " & ¬
            _eMessage & " (" & (_eNumber as string) & ")"
         error _eMessage number _eNumber
      end if
      
      -- Return an empty/negative value that fits the content type that was specified     
      if (_contentType is in _stringTypes) then
         return ""
      else if (_contentType is in _listTypes) then
         return {}
      else if (_contentType is in _numericTypes) then
         return 0
      else
         return false
      end if
      
   end try
   
end readFromFile