If my counter = 0 then display dialog yields error

Here’s what I want: A script that prompts the user to navigate to a folder. Then return a list of the files and folders that exist in that respective folder. I want this list to actually get pasted into a new TextEdit document.

So far, all of the above works A-Ok.

But at the end of the script I have a ‘display dialog’ that gives a quick summary of the findings. Says, “your folder holds X items. Y items are folders, Z items are files.” If the folder that was scanned doesn’t have a file OR if there are are no folders then the “counters” return a zero. This zero causes the script to spit out an error message. Which ever “counter” had a zero causes the script to say: “The variable ‘counter_1’ is not defined”

So it works fine IF there are is at least 1 folder and 1 file in the folder. And in fact, the TextEdit part still looks OK if there are no files or no folders. It’s the display dialog that chokes. However, I’d just want the dialog to simply TELL ME there are ‘zero files’ or ‘zero folders’ if that’s the case - instead of just stopping. I’m sure this is simple. Here’s the code as it stands now:


set my_folder to choose folder
tell application "Finder"
	set the_folders to the name of every folder in folder my_folder
	set the_files to the name of every file in folder my_folder
end tell

set folder_list to ""
repeat with counter_1 from 1 to count the_folders --This is the list produced above, so this script needs to be appended to the script above.
	set folder_list to folder_list & (item counter_1 of the_folders) & return
end repeat

set file_list to ""
repeat with counter_2 from 1 to count the_files --This is the list produced above, so this script needs to be appended to the script above.
	set file_list to file_list & (item counter_2 of the_files) & return
end repeat

tell application "TextEdit"
	activate
	make new document at the front
	set text of front document to "Folders:" & return & folder_list & return & "Files:" & return & file_list
end tell

tell application (path to frontmost application as string)
	display dialog "There is a total of " & counter_1 + counter_2 & " items in the folder:" & return & my_folder & return & return & "There are " & counter_2 & " file(s) and " & counter_1 & " folder(s)." buttons {"OK"} default button 1
end tell

The problem is that if you specify a loop counter from 1 to something, and that something is less than 1, the repeat doesn’t happen and the counter variable doesn’t get set.

The best bet seems to be to set variables to the number of files and folders in the lists, then use those variables in the ‘display dialog’ line.


set my_folder to choose folder
tell application "Finder"
	set the_folders to the name of every folder in folder my_folder
	set the_files to the name of every file in folder my_folder
end tell

set folder_list to ""
set folder_count to (count the_folders)
repeat with counter_1 from 1 to folder_count --This is the list produced above, so this script needs to be appended to the script above.
	set folder_list to folder_list & (item counter_1 of the_folders) & return
end repeat

set file_list to ""
set file_count to (count the_files)
repeat with counter_2 from 1 to file_count --This is the list produced above, so this script needs to be appended to the script above.
	set file_list to file_list & (item counter_2 of the_files) & return
end repeat

tell application "TextEdit"
	activate
	make new document at the front
	set text of front document to "Folders:" & return & folder_list & return & "Files:" & return & file_list
end tell

tell application (path to frontmost application as string)
	display dialog "There is a total of " & (folder_count + file_count) & " items in the folder:" & return & my_folder & return & return & "There are " & file_count & " file(s) and " & folder_count & " folder(s)." buttons {"OK"} default button 1
end tell

Brilliant! Works like a charm.

Thanks for your help!

Hello

The code below should do the trick for you. I see that Nigel Garvey was faster than me, but never mind. :slight_smile:


set the_folders to {}
set the_files to {}

set my_folder to choose folder
tell application "Finder"
	set my the_folders to the name of every folder in folder my_folder
	set my the_files to the name of every file in folder my_folder
end tell
set folder_count to (count my the_folders)
set file_count to (count my the_files)
set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, return}
set my the_files to my the_files as text
set my the_folders to my the_folders as text
set AppleScript's text item delimiters to tids
tell application "TextEdit"
	activate
	make new document at the front
	set text of front document to "Folders:" & return & my the_folders & return & "Files:" & return & my the_files
end tell
tell application (path to frontmost application as string)
	display dialog "There is a total of " & file_count + folder_count & " items in the folder:" & return & my_folder & return & return & "There are " & file_count & " file(s) and " & folder_count & " folder(s)." buttons {"OK"} default button 1
end tell


I cleaned up your code a little, since I wanted to show you a much faster way of achieving the same.
I use the lists that Finder already has taken its time to produce for us, and just converts them to text.

McUsr,

Thanks for the input. And I like the tidier code. However, I see an issue. If I run this script and navigate to the a folder that ONLY has folders in it, then the script returns a dialog that says: There are 10 files and 10 folders - although, in reality there are only 10 folders. Zero files.

Hello.

Oops typo.

I’ll correct it at once. → Done.

Best Regards

McUsr

That too, works sweetly!

One other thing, just for giggles. In the textedit doc, we’re “writing” the words “Files” and “Folders” manually. Is there any way to have these text elements BOLDED?

Thanks again folks!

Hello.

I hope this works too.

It is kind of ugly, but you should be able to see Files and Folders in bold.



set the_folders to {}
set the_files to {}

set my_folder to choose folder
tell application "Finder"
	set my the_folders to the name of every folder in folder my_folder
	set my the_files to the name of every file in folder my_folder
end tell
set folder_count to (count my the_folders)
set file_count to (count my the_files)
set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, return}
set my the_files to my the_files as text
set my the_folders to my the_folders as text
set AppleScript's text item delimiters to tids
tell application "TextEdit"
	activate
	make new document at the front
	set text of front document to "Folders:" & return & my the_folders & return & "Files:" & return & my the_files
	set myword to a reference to first word of text of document 1 
	set font of myword to "Helvetica-Bold"
	tell text of document 1
		set secword to a reference to item 1 of (every word where it is "Files")
	end tell
	set font of secword to "Helvetica-Bold"
end tell
tell application (path to frontmost application as string)
	display dialog "There is a total of " & file_count + folder_count & " items in the folder:" & return & my_folder & return & return & "There are " & file_count & " file(s) and " & folder_count & " folder(s)." buttons {"OK"} default button 1
end tell

Best Regards

McUsr

Excellent!! That’s a great bonus. Works perfectly.

Thanks so much for the additional effort!

All the best,
Kevin