How do I generet a log that keeps track of everything my script does?

i’m sure it is a stupid question, that was already posted and answered, but with the restrictin of the 4 letters in the search engine, i didn’t know how to look for it, sorry.
if it wasn’t replyed, here i leave the code i made, that works fine, and i would need to at least know how to start ( i’m a total noob ). i know its messy, but its the way i could write it…

i want the log to include the paths of the images moved (origin and destination), the previews and thumbnails created, the errors that occured, and anything else…

thanks guys.


property pwg_foldername : "pwg_high"
property tn_foldername : "thumbnail"
--thumbnails and preview are created in .jpg
property low_ext : "jpg"
property Xtype : "EPS, PCD"
property Xext : "eps, pcd"
global name_increment


on adding folder items to this_folder after receiving these_items
	try
		tell application "Finder"
			set this_item to item 1 of these_items
			set relative_path to text 29 thru end of (container of this_item as string)
			set that_folder to "Marto:imagenesALFA:galerias:" & relative_path
			set error_folder to "Marto:imagenesERROR:galerias:" & relative_path
			set test_pwg to (that_folder & pwg_foldername)
			set test_tn to (that_folder & tn_foldername)
			if not (exists folder test_pwg) then
				make new folder at that_folder with properties {name:pwg_foldername}
			end if
			set pwg_folder to test_pwg as alias
			if not (exists folder test_tn) then
				make new folder at that_folder with properties {name:tn_foldername}
			end if
			set tn_folder to test_tn as alias
		end tell
	on error error_message
		tell application "Finder"
			activate
			display dialog error_message buttons {"Cancel"} default button 1 giving up after 120
		end tell
	end try
	try
		repeat with i from 1 to number of items in these_items
			tell application "Finder"
				set name_increment to 0
				set this_item to item i of these_items
				my change_ext(this_item, "", pwg_folder)
				set the file_name to my change_ext(this_item, low_ext, pwg_folder)
				if (name_increment ≥ 1) then
					try
						tell application "Finder"
							set the file_name1 to the name of this_item
							set file_extension1 to the name extension of this_item
							if the file_extension1 is "" then
								set the trimmed_name to the file_name1
							else
								set the trimmed_name to text 1 thru -((length of file_extension1) + 2) of the file_name1
							end if
							set the new_name to (the trimmed_name & "-" & (name_increment as string) & "." & file_extension1) as string
						end tell
					on error error_msg
						tell application "Finder" to display dialog error_msg buttons {"ERROR"} default button 1
					end try
					set the name of this_item to new_name
				end if
				set the source_file to (this_item) as alias
			end tell
			try
				set the item_info to the info for this_item
				if (alias of the item_info is false and the file type of the item_info is in the Xtype) or (the name extension of the item_info is in the Xext) then
					tell application "Adobe Photoshop CS2"
						launch
						open this_item
						do action "temp" from "test"
					end tell
					set tempFile to "Marto:imagenesTEMP:" & file_name as alias
					process_pwg(tempFile, file_name, pwg_folder, tn_folder)
					tell application "Finder" to delete tempFile
				else
					process_pwg(source_file, file_name, pwg_folder, tn_folder)
				end if
				tell application "Finder" to move this_item to the that_folder
			on error error_process
				tell application "Finder"
					move this_item to the error_folder
					display dialog error_process buttons {"Error process"}
				end tell
			end try
		end repeat
	on error error_message
		tell application "Finder"
			activate
			display dialog error_message buttons {"Cancel"} default button 1 giving up after 30
		end tell
	end try
end adding folder items to


on change_ext(this_item, new_extension, target_folder)
	tell application "Finder"
		set the file_name to the name of this_item
		set file_extension to the name extension of this_item
		if the file_extension is "" then
			set the trimmed_name to the file_name
		else
			set the trimmed_name to text 1 thru -((length of file_extension) + 2) of the file_name
		end if
		if the new_extension is "" then
			set target_name to file_name
			set target_extension to file_extension
		else
			set target_extension to new_extension
			set target_name to (the trimmed_name & "." & target_extension) as string
		end if
		if (exists document file target_name of target_folder) then
			set the name_increment to 1
			repeat
				set the new_name to (the trimmed_name & "-" & (name_increment as string) & "." & target_extension) as string
				if not (exists document file new_name of the target_folder) then
					-- rename to conflicting file
					set target_name to the new_name
					exit repeat
				else
					set the name_increment to the name_increment + 1
				end if
			end repeat
		end if
	end tell
	return the target_name
end change_ext


on change_name(this_item)
	
end change_name


on process_pwg(source_file, file_name, pwg_folder, tn_folder)
	try
		set the pwg_path to ((pwg_folder as string) & file_name) as string
		set the tn_path to ((tn_folder as string) & "tn_" & file_name) as string
		with timeout of 900 seconds
			tell application "Image Events"
				launch
				set this_image to open file (source_file as string)
				scale this_image to size 400
				save this_image as JPEG in file pwg_path with icon
				close this_image
				set this_image to open file (pwg_path as string)
				scale this_image to size 120
				save this_image as JPEG in file tn_path with icon
				close this_image
			end tell
		end timeout
	end try
end process_pwg

As a troubleshooting tool, all of my scripts have always included some form of “logMe” handler that has evolved over time. this is the latest version of logMe I’ve been using:


on logMe(log_string, indent_level)
	if g_debug is "true" then --allows turning the debugger on and off so logMe's can be left in final version
		set log_file_name to "LogFileName.txt"
		set home_folder_path to path to home folder
		set log_file_path to (home_folder_path & "Library:Logs:" & log_file_name) as text
		
		try
			writeEntry(log_string, indent_level, log_file_path)
		on error
			tell application "Finder"
				make new file at log_file_path with properties {name:log_file_name}
				close access (log_file_path as alias)
			end tell
			writeEntry(log_string, indent_level, log_file_path)
		end try
	end if
end logMe

on writeEntry(log_string, indent_level, log_file_path)
	repeat indent_level times
		write tab to (log_file_path as alias) starting at eof
	end repeat
	write (log_string & return) to (log_file_path as alias) starting at eof
end writeEntry

To turn it on I set “g_debug” to “true” at the top of the script. To make an entry anywhere in the script I simply call the handler:

logMe(“Log this text”,0)

The second variable is for indent level, since I like to keep my logs more human-readable, gives me a chance to indent certain sections. I want to work on a way to have more states than on and off (“true” and “false”) which is why I use a string instead of boolean. Was tinkering with being able to have “verbose” and “concise” states so some basic log entries are made even once I deploy the script, rather than the all-or-nothing approach I use now.

I’m always looking to improve this routine, if anyone has any suggestions. I already made it alot shorter last week based on the latest MacScripter tutorials about read/writes.

I started doing this because I write alot of droplets, which make troubleshooting more of a pain since I can’t seem to use more standard debugging tools. I end-up with logMe entries all over the place. :wink:

Maybe you can adapt this technique?