Advertisement
rccharles

Create a log in a file

Dec 23rd, 2020
4,548
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (*
  2.    It is easier to diagnose problems with debug information. This applescript demonstaties how to create a log file.
  3.  
  4.     Author: rccharles
  5.    
  6.     Write debug text to the file debugLog.txt in your home folder ( ~/debugLog.txt ).
  7.    
  8.     example of the file ~/debugLog.txt:
  9.    
  10. New log created on Wednesday, May 15, 2013 2:12:41 PM
  11.    --- debug on Wednesday, May 15, 2013 2:12:41 PM   ---
  12.    --- current AppleScript is Ext-Remainder:Applications:applescriptFiles:demo:log user data.app: ---
  13. start program.  
  14. thePath = Ext-Remainder:
  15. End of program.  
  16.    --- debug on Wednesday, May 15, 2013 2:15:14 PM   ---
  17.    --- current AppleScript is Ext-Remainder:Applications:applescriptFiles:demo:log user data.app: ---
  18. start program.  
  19. thePath = Ext-Remainder:
  20. End of program.  
  21.  
  22.    
  23.     fyi:
  24.    
  25.     The applescript log statements are ignored when not running from the script editor.
  26.    
  27.     Not to be confused, this applescript contains log statements that have nothing to do with creating a log file.
  28.     They are to help the script author write the script.
  29.    
  30.  *)
  31.  
  32.  
  33.  
  34. on run
  35.     -- thePath points to the folder in which to create the debug log.
  36.     global thePath, firstRunning
  37.     (* Use the path to clause to create a generalized  path statements *)
  38.     set thePath to (path to home folder as string)
  39.     set firstRunning to ""
  40.    
  41.     -- Write a message into the applescript editor event log.
  42.     log "  --- Starting on " & ((current date) as string) & " --- "
  43.    
  44.     debug("start program.  ")
  45.    
  46.     (*
  47.    
  48.     Your program here...
  49.    
  50.     *)
  51.     debug("thePath = " & thePath)
  52.    
  53.     debug("End of program.  ")
  54.    
  55.    
  56.    
  57. end run
  58.  
  59. -- ----------------------------------------------------------
  60.  
  61. on appendToFile(fileId, theData)
  62.    
  63.     local theSize, writeWhere
  64.    
  65.     set theSize to (get eof fileId)
  66.     set writeWhere to theSize + 1 as integer
  67.     write theData to fileId starting at writeWhere
  68.    
  69. end appendToFile
  70.  
  71.  
  72. -- ----------------------------------------------------------
  73.  
  74. -- based on log by James Reynolds, 12.18.2000, University of Utah
  75.  
  76. on debug(theMessage)
  77.     global thePath, firstRunning
  78.     local theSize, startupDiskName, pathToLog, fileReference, appPath
  79.    
  80.     set pathToLog to (thePath & "debugLog.txt")
  81.     try
  82.        
  83.         set fileReference to (open for access file pathToLog ¬
  84.             with write permission)
  85.         log "firstRunning = " & firstRunning
  86.         set theSize to (get eof fileReference)
  87.         if firstRunning = "" then
  88.             set theSize to (get eof fileReference)
  89.             log "theSize = " & theSize
  90.             if theSize is equal to 0 then
  91.                 appendToFile(fileReference, "New log created on " & ((current date) as string) & " " & return)
  92.                 tell application "Finder"
  93.                     set the creator type of the file pathToLog ¬
  94.                         to "R*ch"
  95.                 end tell
  96.             end if
  97.             appendToFile(fileReference, "   --- debug on " & ((current date) as string) & "   --- " & return)
  98.             tell current application
  99.                 set appPath to path to current application
  100.             end tell
  101.             appendToFile(fileReference, "   --- current AppleScript is " & appPath & " ---" & return)
  102.            
  103.             set firstRunning to "running"
  104.         end if
  105.        
  106.        
  107.         appendToFile(fileReference, theMessage & return)
  108.        
  109.         close access fileReference
  110.        
  111.     on error mes
  112.         try
  113.             log "  We got an error when writing to  " & mes
  114.             close access fileReference
  115.         end try
  116.     end try
  117.    
  118. end debug
  119.  
  120.  
  121.  
  122.  
  123.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement