Loop through listbox to get count of specific file extension

Hi All,

I am trying to figure out how to loop through a listbox, without having to select any files, and determine how many files with a specific extension exist in the listbox.

I have tried: If Listbox1.ListIndex >= 0 And Listbox1.Cell(Listbox1.ListIndex, 0).Right(3) = “key” then
MsgBox “You have found all files that contain the .key extension.”
End If

But this requires a cell/cells to be selected.

Any help would be great.

dim iLC as Integer = listbox.listcount - 1
dim iKeyExtensionCount as Integer = 0
for i as integer = 0 to iLC
if listbox.cell(i, 0).right(3) = "key" then iKeyExtensionCount = iKeyExtensionCount + 1
MsgBox("There were " + str(iKeyExtensionCount) + " files with the extension .key")

Written in the reply box disclaimer and all that.

I would recommend storing the data elsewhere though, and using the listbox to only display the data.

Thanks Tim. Just so I understand, it appears that you have to set an integer and integer count and then point the (Listbox.ListIndex, 0) field to the integer in order to complete the count.

ListCount will give you the number of rows in the ListBox as 1 based. The array of data is zero based though, so we subtract one from the ListCount to line ourselves up with the array (and prevent an OutOfBoundsException.) I do it outside the for loop to prevent your built app from counting the rows in the ListBox on every iteration of the loop. It’s not much of an optimization with small amounts of data, but it’s a habit that helps out in the long run. It’s not entirely necessary.

Secondly, I declare the count variable outside the loop so it can maintain it’s scope once the loop has finished. Every time the cell ends with “key” we increase the count of the variable by one.

These links may help some more:

Some of the things that I wrote aren’t in the documentation and come from experience.


Thanks so much for the explanation. Really helps me understand what is being called.

Have a great day.

Hi Tim or Anyone,

I am now trying to open the files in the listbox by their extension and run them through an applescript to open each file and do a conversion of that file into a word format. I know this isn’t complete, but I can’t even get a file listed in the listbox to open. Can’t quite get this one figured out. This is what I have so far:

Dim convertKeynotes As New Shell

dim iWorkConversion as Integer = Listbox1.listcount - 1

// This executes the Keynote Conversion script

for i as integer = 0 to iWorkConversion
if Listbox1.cell(i, 0).right(3) = “key” OR Listbox1.cell(i, 0).right(7) = “keynote” then
convertKeynotes.Execute(“osascript -e ‘tell application ““Keynote”” to activate’ set current_file to " + Listbox1.Cell(i, 0).ToText + " ‘tell process ““Keynote”” open current_file set frontmost to true’ ‘tell application ““Keynote”” to quit’”)
end if

Any thoughts? Am I on the right track even?

I tried to do something like that a little over a year ago (I think it was using AppleScript to turn Pages documents to PDF)
I ran into a snag with Sandboxing entitlements because Pages is sandboxed and I had to bend over backwards to allow it access to the files in just the right way.

I love Xojo, but I ended up having to do the entire thing in AppleScript because of the Sandboxing requirements.

Gotcha. I was kinda worried that might be the case. I guess I will just update my Applescripts to work better. I just so badly wanted to put this into a user friendly GUI for my team.

I’d write it as s stand alone script you can add to a project
Something like

on Run(theFile, theDest)
	using terms from application "Keynote"
		set current_file to POSIX file theFile
		set the_dest to POSIX file theDest
		tell application "Keynote"
			open current_file
			export (document of window 0) to the_dest as Microsoft PowerPoint
		end tell
	end using terms from
end Run

And save it in a script named “DoConversion”
Then when you add this applescript to your project you call it like

DoConversion("/Users/npalardy/Desktop/Untitled.key", "/Users/npalardy/Desktop/test.ppt")

here’s a working sample I just threw together

and another sample of apple script additions in xojo


I will give it a try and see how it goes.


Wow! I hope I get somewhat as proficient as you are, in the foreseeable future.

That worked more or less the way you wanted ?

Won’t have time to test until tomorrow.

Hi Norman,

I tried the above method and I get the following when I try to execute the script:
This is not an array but you are using it as one -> keyConverter("/Volumes/Macintosh HD/Users/roger/Desktop/To_Convert/Test2.key", “/Volumes/Macintosh HD/Users/roger/Desktop/Test2.pptx”)

execute the script … how ?

the sample I posted has the applescript dragged into the project then you use it just as if it were a regular method you wrote in xojo code

if you dont have that set up that way then you will get errors

Ok. I dragged the script into the project and called it like you have listed above and it works. However, I hope to be able to loop through a listbox which contains the full file paths of the keynote files and do each one in the listbox. Is there a way to do it?

call the function for each item in the listbox as you loop through them

I am still learning, so please bear with me.

I think I am getting close to having the right code. Here is my code:

for i as integer = 0 to iWorkConversion
if Listbox1.cell(i, 0).right(3) = “key” OR Listbox1.cell(i, 0).right(7) = “keynote” then
keyConvert("" + Listbox1.Cell(i, 0) + “”, “/Volumes/Macintosh HD/Users/roger/Desktop/Converted Files/” + Listbox1.Cell(i, 0).right(5) = “.pptx”)
end if

This warning is:
iWork 2 Office Conversions Launched
6:22:47 PM
Error in script ‘keyConvert.scpt’: Keynote got an error: “False” could not be interpreted as a file URL.
6:22:53 PM
iWork 2 Office Conversions Ended

Any idea?


Listbox1.Cell(i, 0).right(5) = ".pptx"

instead be

Listbox1.Cell(i, 0).right(5) + ".pptx"