Scripts, tutorials, and general Desktop X help
Beginner-Intermediate
Published on February 8, 2007 By sViz In DesktopX Tutorials


UPDATE 11/26/2007: See The WinCustomize Wiki Tutorial for an updated, efficient slideshow tutorial.

PICTURE SLIDESHOW Part 2: Drag and Drop Folders
Beginner-Intermediate


It’s part 2 of this 3 part tutorial. If you haven’t already, check out part 1 here– Link

When I made the first part of this series I hadn’t intended for it to be a series. It was just going to be drag and drop files and that’s it. Then I asked myself, ‘I wonder how they use folders to make slideshows?’. I couldn’t resist finding out, I had to learn how to do it. So I went at it. It was easier than I thought with vbscript Folder examples all about the internet. I spent a couple of hours on it and I made a workable script. Well after I posted the first tutorial PICTURE SLIDESHOW Part 1: Drag and Drop Files,
along came Dragonmage. He joked “What happens when I drop a folder onto it?” That got me thinking: What happens if? What happens if the user drags and drops invalid file types? What happens if the user drags and drops a folder that contains both valid images and other file types? Contingencies! You always have to think about all the ‘what ifs’ that can arise on the user’s end. After hours of ironing out as many contingencies as I could think of, my simple script exploded with fool-proof devices. I tried to keep it as streamlined as possible.

We’re still dealing with the same object which is 200 in width and 200 in height. I’ll be breaking up the script to explain it better.
Let’s take a look at the script:


Dim files, picscount, grpofpics, numofpics, validpics
Dim foldercheck, fs, f, f1, fc

'Called when the script is executed
Sub Object_OnScriptEnter
picscount = 0
End Sub

Once again, we start with the setup. Declare the variables and set the picscount to 0.




Sub Object_OnDropFiles(files)
object.KillTimer 1
grpofpics = ""


Now we start the section for what happens OnDropFiles. Contingency #1: kill the timer every time files are dropped. You don’t want the timer to continue running when a user goes from multiple files to just a single file. We also clear the variable grpofpics for good measure. The following pieces of codes will also be going into OnDropFiles.



'Files have extensions (.bmp), a folder does not
'We search the string to see if it contains a period
foldercheck = Instr(1, files, ".")


Here we find out whether the user drops a file or a folder. A file will have an extension-- mypic.bmp—thus, it will have a period in the filename and ‘foldercheck’ will return 1. A folder does not have an extension so there will not be a period in the folder name and ‘foldercheck’ will return 0. Once we search the string (the filename) for a period, we can facilitate the following process.



'If user drops a folder, get files within folder
If foldercheck = 0 Then
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(files)
Set fc = f.Files


Remember how we checked the string (filename) for period? If there is a period then it’s a file and ‘foldercheck’ will return 1. If there is no period then it’s a folder and ‘foldercheck’ will return 0. Here we define what to do if ‘foldercheck’ returns 0. First we create a FileSystemObject, then we get the folder the user dropped, then we get the files within that folder.



For Each f1 In fc
'Check file extensions for valid images
checkext = Split(f1.name,".")
extension = LCase(checkext(1))
'Create a variable, listing only valid image files in folder
If extension = "bmp" Or extension = "png" _
Or extension = "ico" Or extension = "jpg" _
Or extension = "tga" Then
grpofpics= grpofpics & f & "\" & f1.name & "|"
End If
Next

In the second part we have a loop (For..Next) that will go about the task of getting the fullpath & file name to each of the files within the folder. As a contingency we check the file extension of each file. After weeding out the ‘bad files’, in the end, the variable ‘grpofpics’ will be a string (list of filenames) made up of the fullpaths and filenames to only the valid image files.



'If there are images found, create array and count images
If grpofpics <> "" Then
grpofpics = Left(grpofpics, Len(grpofpics)-1)
grpofpics = Split(grpofpics, "|")
numofpics = UBound(grpofpics)
End If

In the third part we have another contingency: “What to do if the user hasn’t dropped any valid image files?” Well, we create an If statement. We simply tell DX to give us the final information (list of filenames and number of files) only if ‘grpofpics’ contains the information. In other words, if ‘grpofpics’ is not blank split ‘grpofpics’ into an array (list of filenames) and get the number of pictures in that array.

(You might be wondering what this line does-- grpofpics = Left(grpofpics, Len(grpofpics)-1). Well, the way I built the string of filenames (‘grpofpics’) it looks like this : file1|file2|file3| You’ll notice the extra pipeline character (“|”) on the end. When we split the string up along the pipelines to create an array you’ll end up with this “file1”, “file2”, “file3”, and “ “—an extra blank filename. What the aforementioned line of code does is remove that extra pipeline so you don’t get an extra blank filename. It took me hours to figure out why my slideshow was displaying a blank when it came to the end of the show.)



'If user drops files
ElseIf foldercheck > 0 Then
grpofpics= Split(files, "|")
For Each elem In grpofpics
'Check file extensions for valid images
checkext = Instr(f1.name, ".")
If checkext > 0 Then
checkext = Split(f1.name,".")
extension = LCase(checkext(1))
End If
'Create a variable, listing only valid image files in folder
If extension = "bmp" Or extension = "png" _
Or extension = "ico" Or extension = "jpg" _
Or extension = "tga" Then
validpics= validpics & elem & "|"
End If
Next
'If there are images found, create array and count images
If validpics <> "" Then
validpics = Left(validpics, Len(validpics)-1)
grpofpics = Split(validpics, "|")
numofpics = UBound(grpofpics)
Else
grpofpics = ""
End If
End If

Finally, in the 4th part of OnDropFiles, we define what to do when ‘foldercheck’ returns 1 (i.e. if the user drops files). As you can see it’s pretty much the exact same procedure as I explained for folders. We use a loop to get the filenames and fullpaths then we define what to do if there are valid images or not. Only I used a different variable—‘validpics’—to create the string (list) of filenames. Long story, short: it’s because the variable ‘grpofpics’ is already in use. Again, we tell DX to get the final information (list of filenames and number of files) only if ‘validpics’ is not blank. This time, we specifically define what to do when there are no valid images: we clear the variable ‘grpofpics’. I’ll show you why next.



'If grpofpics contains images, set first picture on drop and add to picscount
If IsArray(grpofpics) = True Then
Object.Picture = grpofpics(0)
picscount = 1
Else
msgbox "No images found"
End If
'If there is more than one image start timer on drop
If numofpics > 0 Then object.SetTimer 1, 2000

End Sub

The final piece of code in Sub Object_OnDropFiles is where we define what to do if we have all the necessary info to create a slide show (list of filenames and number of files) or if we don’t. We do this by checking to see if ‘grpofpics’ is an array. If ‘grpofpics’ contained valid filenames it would have been split into an array. If it didn’t it would be blank, therefore it would not be an array. If ‘grpofpics’ is an array we can set the picture, but if it isn’t we put up a message box telling the user “No images found”. Lastly, we only set the timer if the user drops more than one image file.



Sub Object_Ontimer1
'If count is higher than number of pics then reset count
If picscount > numofpics Then picscount = 0
'Set picture
Object.Picture = grpofpics(picscount)
'Add to count
picscount = picscount + 1
End Sub


Now that we’ve done all that we can get on with this slideshow! Nothing’s changed in this piece of code since the last tutorial. We still check if the count is higher than the number of pictures and reset if it is. Then, set the picture, and add to the count. Whew!

That’s it. Thanks for reading. Next, Picture Slideshow Part 3: Subfolders!



Synergy:
DesktopX Wiki- Link
DX Tutorials- Link
My Tutorials- Link
Assorted Tutorials- Link
Me- Link



Comments
No one has commented on this article. Be the first!