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
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
next
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.
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
next
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
quit
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
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”)
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?
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
next
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