Reading the Finding Crash and Hang logs thread made me recognize that I almost never report an issue when it occurs (and most often fail to do it later). While this …
… of course is true, it always seemed too much interruption (and until yesterday I didn’t know that the log files exist, thank you very much @mwra!).
This AppleScript
- gets
- the
.crash
file - the
.ips
file - the
.hang
file - the
.diag
file
- the
- creates a sample
- creates a mail with a zip of all files
Now it’s just
- running the script (e.g. via Alfred or Keyboard Maestro)
- adding in the mail
- what happened before the issue occurred
- whether it’s reproducible
- the steps to reproduce
- sending the mail
That’s it!
-- Create mail with attached log files
use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
property theApp_Name : "Tinderbox"
property theAddress : "tinderbox@eastgate.com"
property launchAppToCreateSample : true -- If app isn't running (e.g. after force quit) launch it and create sample.
property theSeconds : 3600
------------------------------------------------------------- Get display name --------------------------------------------------------------
set theApp_DisplayName to do shell script "appPath=$(find \"/Applications\" -name \"" & theApp_Name & "*.app\" -maxdepth 1 | sort --version-sort | tail -1) ; mdls -name kMDItemDisplayName -raw \"${appPath}\""
------------------------------------------------------------------ Get paths -------------------------------------------------------------------
set thePaths to {}
set theLogCrash_Path to do shell script "find ${HOME}\"/Library/Logs/DiagnosticReports\" -name \"" & theApp_DisplayName & "_*.crash\" | sort | tail -1"
if theLogCrash_Path ≠ "" and my checkCreationDate(theLogCrash_Path, theSeconds) then set end of thePaths to theLogCrash_Path
set theLogIps_Path to do shell script "find ${HOME}\"/Library/Logs/DiagnosticReports\" -name \"" & theApp_DisplayName & "*.ips\" | sort | tail -1"
if theLogIps_Path ≠ "" and my checkCreationDate(theLogIps_Path, theSeconds) then set end of thePaths to theLogIps_Path
set theLogHang_Path to do shell script "find \"/Library/Logs/DiagnosticReports\" -name \"" & theApp_DisplayName & "_*.hang\" | sort | tail -1"
if theLogHang_Path ≠ "" and my checkCreationDate(theLogHang_Path, theSeconds) then set end of thePaths to theLogHang_Path
set theLogDiag_Path to do shell script "find \"/Library/Logs/DiagnosticReports\" -name \"" & theApp_DisplayName & "_*.diag\" | sort | tail -1"
if theLogDiag_Path ≠ "" and my checkCreationDate(theLogDiag_Path, theSeconds) then set end of thePaths to theLogDiag_Path
if thePaths ≠ {} then
--------------------------------------------------------------- Create sample ----------------------------------------------------------------
set theSample_ShellPath to "${HOME}/Desktop/" & theApp_DisplayName & " - Sample " & (do shell script "date \"+%Y-%m-%d% %H.%M.%S\"") & ".txt"
if (my isRunning(theApp_DisplayName)) then
display notification "Creating sample …"
set theSample_Path to do shell script "samplePath=\"" & theSample_ShellPath & "\" ; sample " & quoted form of theApp_DisplayName & " -file \"${samplePath}\" >/dev/null 2>&1 ; echo \"${samplePath}\""
set end of thePaths to theSample_Path
else if launchAppToCreateSample then
display notification "Creating sample …"
set theSample_Path to do shell script "samplePath=\"" & theSample_ShellPath & "\" ; open -a " & quoted form of theApp_DisplayName & " ; sample " & quoted form of theApp_DisplayName & " -wait -file \"${samplePath}\" ; echo \"${samplePath}\""
set end of thePaths to theSample_Path
end if
------------------------------------------------------------------ Create zip -------------------------------------------------------------------
set theName to theApp_DisplayName & " - Report - " & (do shell script "date \"+%Y-%m-%d% %H.%M.%S\"")
set theZIP_Path to (POSIX path of (path to desktop)) & theName & ".zip"
set thePaths_string to "'" & my tid(thePaths, "'" & space & "'") & "'"
do shell script "zip -j " & quoted form of theZIP_Path & space & thePaths_string
----------------------------------------------------------------- Create mail ------------------------------------------------------------------
tell application "Mail"
try
activate
set theMessage to make new outgoing message with properties {subject:theName, visible:true}
tell theMessage
make new to recipient at end of to recipients with properties {address:theAddress}
make new attachment with properties {file name:(POSIX file theZIP_Path as alias)}
end tell
on error error_message number error_number
if the error_number is not -128 then display alert "Mail" message error_message as warning
return
end try
end tell
else
tell application "SystemUIServer"
activate
display alert "No " & theApp_DisplayName & " logs found!" buttons {"Ok"} message "" as critical
return
end tell
end if
on isRunning(theProcessName)
try
tell application "System Events"
try
set theProcess to first application process whose name = theProcessName
return true
on error
return false
end try
end tell
on error error_message number error_number
activate
display alert "Error: Handler \"isRunning\"" message error_message as warning
error number -128
end try
end isRunning
on tid(theInput, theDelimiter)
set d to AppleScript's text item delimiters
set AppleScript's text item delimiters to theDelimiter
if class of theInput = text then
set theOutput to text items of theInput
else if class of theInput = list then
set theOutput to theInput as text
end if
set AppleScript's text item delimiters to d
return theOutput
end tid
on checkCreationDate(thePath, theSeconds)
try
set {success, theCreationDate, theError} to (current application's |NSURL|'s fileURLWithPath:thePath)'s getResourceValue:(reference) forKey:(current application's NSURLCreationDateKey) |error|:(reference)
if theError ≠ missing value then error (theError's localizedDescription() as string)
if (theCreationDate's compare:(current application's NSDate's dateWithTimeIntervalSinceNow:-theSeconds)) ≥ 0 then
true
else
false
end if
on error error_message number error_number
activate
if the error_number is not -128 then display alert "Error: Handler \"checkCreationDate\"" message error_message as warning
error number -128
end try
end checkCreationDate
(@eastgate, not sure whether all files are actually useful, especially as the script simply uses the newest files it finds, i.e. it will probably often send old files that are not related to the current issue. Also not sure about the sample, e.g. duration etc., never used that before.)