Log files and aliases

I’m working with the below script, which I’ve largely learned AppleScript on with the gracious help of a fellow poster. It renames files according to a two-column Excel file (exported as a “tab delimited”), with the user setting the folder with which files will be renamed and then the Excel which will serve as the guide to the 1:1 rename.

One excellent flourish I’ve been trying to put in place is to have the script output a log file that identifies any file that appears in the list but not somewhere in the folder (essentially, all the “failures”). Just outputting them to a text file. I can’t seem to do this negatively in the form of a log file.

The other element to complete the self-diagnostic would be for the script to find any files in that designated folder that do not appear in the excel file and either make a text file list of them or, preferably, dump aliases of them into a new folder for easy viewing.

I’m still learning the scripting and I can’t find any standardized why to make this happen. Any ideas? I’d be very grateful for any help you might provide. Textbooks just do not have the human touch and institutional knowledge this forum seems to.

Thanks in advance.

The script:

set sourceFolder to (choose folder) as text
set textFile to (choose file of type "TEXT") as text

-- read in the list of names
set filenameList to paragraphs of (read file textFile)

-- in order to break out the two fields we need to play
-- with text item delimiters
set {oldDelims, text item delimiters} to {text item delimiters, tab}

set err to 0
set allFiles to count filenameList
repeat with aFile in filenameList
	set {codified_name, real_name} to text items of aFile
	set foundFile to do shell script "/usr/bin/find " & quoted form of POSIX path of sourceFolder & " -name " & quoted form of codified_name
	if foundFile is "" then
		set err to err + 1
	else
		tell application "Finder" to set name of (POSIX file foundFile as alias) to real_name
	end if
end repeat
set text item delimiters to oldDelims
set msg to (allFiles as text) & " files processed"
if err > 0 then set msg to return & (msg & err as text) & " files not found"
display dialog msg

My log file generator, evolved over the years and fine-tuned lately by folks here is listed below.

Set the g_debug property to turn logging on and off. This allows me to set-up debugging log entries but turn them off in the finished product without actually removing the code (thus for future debugging).

Provide a log file name that is appropriate in g_log_file_name

I put all that at the very top of my script for easy access.

Then I simply use:

my logMe(some_string,1)

You can either use directly quoted text, or a string variable. The number is the number of tab characters to use before logging the text, which allows for indenting and making detailed logs easier to read.

Mind you, I only use this logger for debugging and I keep my logs emptied and not permanently running, so this log generator has no safeties from bloated log files.

--
-- DECLARE PROPERTIES
--
-- debugging on?
property g_debug : false

--basic file path and names
property g_home_folder_path : path to home folder
property g_log_file_name : "ScriptName Log.txt"

--Log Entry Generation
-- With help from StephanK of MacScripter
-- http://bbs.applescript.net/viewtopic.php?pid=76607#p76607
--
on logMe(log_string, indent_level)
	if g_debug is true then --allows turning the debugger on and off so my logMe's can be left in final version
		set log_target to (g_home_folder_path & "Library:Logs:" & g_log_file_name) as text
		try
			set log_file_ref to open for access file log_target with write permission
			repeat indent_level times
				write tab to log_file_ref starting at eof
			end repeat
			write ((log_string as text) & return) to log_file_ref starting at eof
			close access log_file_ref
			return true
		on error
			try
				close access file log_target
			end try
			return false
		end try
	end if
end logMe

Is there a way to use this to generate a text file that just lists the failures in my script? Or is that essential what it would do?
I apologize if this is a rudimentary question.

I basically use it for progress marking to track down errors or log key events.

So for example, I will insert things like:

my logMe("Just finished DoThisThing handler",1)
my logMe("¢ Value of myVariable is: " & myVariable,2)

I just insert lines like this after key events I want a check-in from. Sometimes I find this easier even than using ScriptDebugger’s built-in tracing on complex scripts.

So you’d have to move the information you want reported to a variable and report it as it happens, or collect it for reporting at the end. If you are using try to catch errors, for example, then in the on error portion, you’d add a my logMe code line with whatever it should be reporting to the log.

For example, this script:

on open parseMe
	repeat with i from 1 to number of items of parseMe
		--utility variables
		set parseMeString to (item i of parseMe) as string
		set parseMeStringPOSIX to POSIX path of parseMeString
		set parseMeInfo to info for (item i of parseMe)
		
		--basic finder info
		logMe("FILE NAME: " & name of parseMeInfo, 0)
		logMe("FILE TYPE: " & file type of parseMeInfo, 1)
		logMe("FILE CREATOR: " & file creator of parseMeInfo, 1)
		tell application "Finder"
			set label_color to label index of item i of parseMe
		end tell
		logMe("LABEL COLOR: " & label_color, 1)
		
		--run "file" command variants
		set infoFileB to do shell script ("file -b " & quoted form of parseMeStringPOSIX)
		logMe("FILE -B: " & infoFileB, 1)
		
		set infoFileBI to do shell script ("file -bi " & quoted form of parseMeStringPOSIX)
		logMe("FILE -BI: " & infoFileBI, 1)
		logMe("--------------------------------------------------", 0)
	end repeat
end open

Will have output like this in a text file:

Does that help at all?

Thanks so much for your reply. This makes sense generally, but I guess I’m still having difficulty making it dovetail with the above script - which I should mention was largely Stefan K’s handiwork.

Basically, when this happens in the script –

if foundFile is "" then
		set err to err + 1

– I’d like to try and make it not just pop as a total number of failures in the dialog at the end, but also to generate the list of files for which it looked for but did not find.

What is the syntax for attaching an expression my above posted script that will recognize how to log this specific even - or I should say, lack therof? I’m sorry if I’ve failed to make an obvious connection here, but I’m at loggerheads on how to fuse the two.

What is the name of the variable you are storing the list of files it didn’t find?

Then you’d do something like…

my logMe("Number of files not found: " & err,0)

…then use a repeat loop to list one by one with logMe (my logMe("¢ " & item x of errList,1 or somesuch) the files that failed.

Sorry, short on time today to construct the actual loop…not quite experienced enough to just write it and know I gave you a valid repeat loop.

Thank you so much yet again. You’ve been exceedingly helpful and I think my understanding of Applescript has grown by leaps and bounds in the last week or so.

I’m piecing it together now, but - as you alluded to - I don’t seem to have the script looping so that will record each filename it fails to find in a variable and throw it into the log file. I’ve defined a variable called “notFound,” which I’m desperately trying to have take account of each file name not found and want it to run through the “logMe” expression.

Perhaps I’ve either left something vital undefined or do not have the loop set up correctly or both. I don’t think it’s as bad as it sounds, I’m just not connecting the dots as a veteran poster like yourself likely would.

Am I at least on the right track here? I suspect it might be painfully obvious what I’m missing from script laid bare below.

** I accidentally deleted the credit below, but I assure you it will appear in the final script:

set sourceFolder to (choose folder) as text
set textFile to (choose file of type "TEXT") as text

-- read in the list of names
set filenameList to paragraphs of (read file textFile)

-- in order to break out the two fields we need to play
-- with text item delimiters
set {oldDelims, text item delimiters} to {text item delimiters, tab}

set err to 0
set allFiles to count filenameList
repeat with aFile in filenameList
	
	set {codified_name, real_name} to text items of aFile
	set foundFile to do shell script "/usr/bin/find " & quoted form of POSIX path of sourceFolder & " -name " & quoted form of codified_name
	if foundFile is "" then
		set err to err + 1
		set notFound to quoted form of codified_name
		logMe("FILE NAME: " & text of notFound, 0)
	else
		tell application "Finder" to set name of (POSIX file foundFile as alias) to real_name
	end if
end repeat
set text item delimiters to oldDelims
set msg to (allFiles as text) & " files processed"
if err > 0 then set msg to return & (msg & err as text) & " files not found"
display dialog msg

--
-- DECLARE PROPERTIES
--
-- debugging on?
property g_debug : true

--basic file path and names
property g_home_folder_path : "Macintosh HD:Users:josh:Desktop:renamefiles_proj:test_folder"
property g_log_file_name : "Rename_Log.txt"

--Log Entry Generation
--
on logMe(log_string, indent_level)
	if g_debug is true then --allows turning the debugger on and off so my logMe's can be left in final version
		set log_target to ("Macintosh HD:Users:josh:Desktop:renamefiles_proj:test_folder" & "Library:Logs:" & "Rename_Log.txt") as text
		try
			set log_file_ref to open for access file log_target with write permission
			repeat indent_level times
				write tab to log_file_ref starting at eof
			end repeat
			write ((log_string as text) & return) to log_file_ref starting at eof
			close access log_file_ref
			return true
		on error
			try
				close access file log_target
			end try
			return false
		end try
	end if
end logMe

“Veteran” poster? Pfft…I’m a sad hack compared to others in this place…and I’m surprised none of them have chimed in. :wink:

As for credit on logMe, original credit goes to StefanK, solver of many of my esoteric AppleScript problems. Heck, he’d probably shorten your code if he was watching. :smiley:

-- Log Entry Generation
-- With help from StephanK of MacScripter
-- http://bbs.applescript.net/viewtopic.php?pid=76607#p76607

I make it a habit to credit in my script code when I get considerable help from others, along with the URL so if I pick it up months later I won’t have to ask again. :wink:

Anyway, back to your problem:

So you’re saying the current loop doesn’t work? What error you getting?

Try a “my” in front of logMe (assuming logMe isn’t running right), and try dropping notFound without the coersion. Near as I can tell your codified_name is already text or string, and I think logMe will blissfully even spit-out aliases and reference names last time I was sloppy enough to try anyway.

So try this minor change to the loop:

repeat with aFile in filenameList
	
	set {codified_name, real_name} to text items of aFile
	set foundFile to do shell script "/usr/bin/find " & quoted form of POSIX path of sourceFolder & " -name " & quoted form of codified_name
	if foundFile is "" then
		set err to err + 1
		my logMe("FILE NAME: " & codified_name, 0)
	else
		tell application "Finder" to set name of (POSIX file foundFile as alias) to real_name
	end if
end repeat

If logMe chokes on the codified_name try this instead:

my logMe("FILE NAME: " & codified_name as text, 0)

That worked! I also had to set the proper target for the log file so it would know where to go. It seems like a lot of the snags I’ve had with my scripting have been with paths - sometimes stupid mistakes.

Thank you so much for your help. Your responses have been exceedingly helpful and heightened my understanding.

Speaking of paths - and understanding - my renaming script works locally but when it’s moved it to an external firewire drive it throws an error. I posted a new thread about it… this seems to be a roadblock to my further understanding of scripting and certainly of this project.

Don’t feel bad, it’s a problem I still have too. Managing POSIX paths versus Apple aliases versus “references” is still confusing to me. Worse, some operations and applications require a varietous mix of the three to keep things interesting.

And text item delimiters still gives me a headache. :wink:

I’m glad I could help…it’s rare I can “give back” to this forum when so many people have been so gracious about helping me.

I had the same problem recently, and I’m sure StefanK will chime-in with the answer again unless I can find the thread I had with the same issue…

[searches]

Ah. here it is…think this might help:

http://macscripter.net/viewtopic.php?id=26699