Take Away: ObjectPAL example that demonstrates listing a set of files and allowing the user to launch any one of them by double clicking.
KB101148
Suppose that you want to create a form that lists all the files in a directory and enables the user to launch a file by double-clicking its filename. This next step by step example uses the FileSystem methods to search your Windows directory for .EXE files and puts them in a list field. When the user double clicks on a filename, the .EXE file is launched.
Demo Files: FINDFILE.FSL.
Step By Step
1. Create a new form with two undefined fields. Label the first undefined field Search For, and change its name to SearchForField.
Set up form
2. Make the second undefined field a list field; change its name to fldResult and its list to FileList. Figure 4 shows the Object Tree of the list field. The list object is inside the field object.
The Object Tree of the list field
3. Place a text object above the fldResult field with the text Double click to execute.
4. Add line 3 to the Var window of the Search for field.
1: ;SearchForField :: Var
2: Var
3: fs FileSystem
4: endVar
5. Add lines 3 and 4 to the open event of the Search for field.
1: ;SearchForField :: open
2: method open(var eventInfo Event)
3: doDefault
4: self.value = windowsDir() + "\\*.EXE"
5: endMethod
6. Add lines 3--17 to the newValue event of the Search for field.
15: fldResult.FileList.list.value = "File not found"
16: endIf
17: endIf
18: endMethod
7. Add lines 3--7 to the mouseDouble event of the fldResult field.
1: ;fldResult :: mouseDouble
2: method mouseDouble(var eventInfo MouseEvent)
3: try
4: execute(fldResult.value)
5: onFail
6: msgStop("Warning", "Could not launch file")
7: endTry
8: endMethod
Check the syntax and save the form. Run the form. All the .EXE files from your Windows directory are listed. If you double-click a file, it will execute. Change the extension to .HLP. If .HLP is associated with WINHELP.EXE like the Windows default, you can double-click any Help file to launch it. Now, type in an invalid extension, such as .XYZ. The words File not found appear.
In step 3, line 3 sets up fs as a FileSystem variable. It's used later in step 5.
In step 4, line 4 populates the Search for field after invoking the default behavior.
In step 5, line 3 checks the value, and line 9 uses it and the fs System variable to find the first occurrence of the search criteria. If nothing is found, the execution skips to line 14. Lines 14--16 clear the list and set the first value to File not found. If line 3 finds a file, lines 5--7 clear the list and set the initial value, and lines 8--12 loop through and populate the rest of the list.
In step 6, lines 3--7 attempt to launch the value that you double-click. These lines use a try block. If you don't use a try block, ObjectPAL will display abstract errors when the file isn't found. In other words, many ObjectPAL errors are meant for the programmer. You might want to use a try structure to test your code. If it fails, display a more meaningful message to the end user.
This works fine, but if I change *.EXE to *.PDF, I get a list of PDF-files allright, but it fails by doubleclick - Could not launch file. Have tried with execute("explorer fldResult.value"), but that only opens the explorer, and not the pdf-file in a pdf-reader. I have also tried to change from windowsdir () to a defined alias - self.value = :myalias: + \\*.EXE. That works fine with the list, but not the execute.