AppleScript Log-Funktion

Ich habe oft Applescript, die sehr lange laufen und keine GUI haben, daher habe ich mir eine kleine Funktion geschrieben, die mir eine Logdatei im Log-Verzeichnis meines aktuell angemeldeten Benutzers schreibt (~/Library/Logs/).

Aufgerufen wird die Funktion einfach mit “my logger(my_log, myFile)” sobei my_log der String ist, der in die Logdatei geschrieben werden soll und myFile, der Dateiname des Logfiles. Die Funktion erstellt automatische eine entsprechende Datei im Log-Verzeichnis und fügt auch automatisch das aktuelle Datum inklusive Uhrzeit bei jedem Log-Befehl hinzu. Die Logsfiles können mit jedem Textedior betrachtet werden oder natürlich auch Konsole.app.

on logger(my_log, myFile)
	set myPath to ((path to home folder as string) & "Library:Logs")
	set myUnixPath to POSIX path of myPath
	try
		do shell script "touch " & myUnixPath & "/" & myFile
	on error errMsg number errNum
		do shell script "mkdir " & myUnixPath
		do shell script "echo ' ' > " & myUnixPath & "/" & myFile
	end try
	set my_log_file to open for access file (myPath & ":" & myFile) with write permission -- this opens the file
	write (((current date) as string) & " " & my_log) to my_log_file starting at eof -- append to the end
	close access my_log_file -- close the file when you're done
end logger
-- Dies ist ein Beispielaufruf
my logger("Ich habe grade blogadresse.de besucht", "Weblog.log")

2 Gedanken zu “AppleScript Log-Funktion

Hinterlasse eine Antwort

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind markiert *

*

Du kannst folgende HTML-Tags benutzen: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Kommentarlinks könnten nofollow frei sein.