PDA

View Full Version : Random opening of files (how to)


grizedale
09-28-2002, 02:51 AM
Using a menu to open student assessments created as PDF documents. At present there is a button for each assessment (A1, A2, A3 etc), would it be possible, using just 1 button, to have the program generate a random opening from a selection of files?

Brett
09-28-2002, 08:47 AM
The best way to do that would be to create a Global List that contains all of the paths to the PDF files. Then when the button is pressed, use the Variable.SetRandomValue action to find a random index between 0 and number of global list elements-1. Now, use the GlobalList.GetItem action to get the path to the PDF file at the random location.

I made a sample project and have attached it to this post so that you can download it and try it out. The attached project works on text files, but the principle is the same. Note that the Project's On Initialize event dynamically reads the list of filenames from the Documents folder dynamically. This allows you to add more documents to the project without having to remember to add them to the Global List manually.

12228-RandomText.zip (http://www.indigorose.com/ubbthreads/uploads/12228-RandomText.zip)

grizedale
09-28-2002, 12:16 PM
Brett

WOW, works beautifully.

I have worked through the code you have given me and adapted it for my needs. Essentially I have 7 folders, 1 folder per assessment which contains up to 4 documents to be chosen from so, I have seven buttons depending on which module is being sat through.

Thank you very much Brett

Regards

TJ_Tigger
09-29-2002, 10:29 AM
I made a sample project and have attached it to this post so that you can download it and try it out. The attached project works on text files, but the principle is the same. Note that the Project's On Initialize event dynamically reads the list of filenames from the Documents folder dynamically. This allows you to add more documents to the project without having to remember to add them to the Global List manually.



If the list is going to be a static list, wouldn't you want to have the Global List be static as well? Or at least use the above quoted item when you build the project, that way the list is created when it is built rather than doing it everytime it is run. I am thinking about my target audience who might not have the fastest PCs and doing the search along with other things of detecting screen size might cause the Project to start rather slow.

Any comments?

Brett
09-29-2002, 01:59 PM
Sure. Depends on the situation. Do what works for you, as Dr. Phil would say.

grizedale
09-30-2002, 09:08 AM
Brett

Have put the menu into use and it works just how I want it to, there is one slight glitch in that, on odd occasions, when the "random" button is pressed the folder itself opens showing the files rather than the file itself. It is no great hardship to close this and click again but just wondered why!

Bruce
09-30-2002, 10:11 AM
Really... I can't seem to make it work! Nothing comes up! Hummm

Lorne
09-30-2002, 10:25 AM
Sounds like a folder path is being added to the global list (i.e. a path to a folder instead of to a file). You might want to try adding some temporary actions to output or display the contents of the global list after it's built...that could provide some important clues.

grizedale
09-30-2002, 10:36 AM
I could understand it being that if it happened constantly, but it happens only in every 1 in 5 clicks (approx). How do I display the contents of the global list after build?

Lorne
09-30-2002, 11:21 AM
Aren't you opening the files at random? That would be why it only happens intermittently. In other words: most of the items in the global list are fine, but there's one item that is a folder, which can be "opened" much like a file can.

To display the contents of a global list after build, add some actions to the action list to go through the global list and display the items in a message box. Or, grab all the items and output them to a text file, like so:

1. Set up two design-time constants, one called #ASC_CR# and another called #ASC_LF#. Assign "13" to #ASC_CR# and assign "10" to #ASC_LF#.

2. Add two actions to your project, occurring after the global list is built:

First, a "Global List - Get Item" action. Set it to get "All items" and change the delimiter from ";;" to "#ASC_CR##ASC_LF#" and store the result in a variable called %contents%.
Next, write the value in %contents% to a text file with a "Text File - Write" action.Something like this (only change the name of the global list, of course):

<IR_ACTIONS_LIST>
<Action name="Get Item">
<Type>112</Type>
<Function>0</Function>
<DTIndentLevel>0</DTIndentLevel>
<Enabled>1</Enabled>
<ErrorHandling>
<UserNotificationMode>2</UserNotificationMode>
<CustomErrorMessage/>
<OnErrorAction>0</OnErrorAction>
<JumpToLabel/>
</ErrorHandling>
<TargetList>Stuff</TargetList>
<GetType>1</GetType>
<Index/>
<IndexDelimiter>;;</IndexDelimiter>
<Variable>%contents%</Variable>
<VarDelimiter>#ASC_CR##ASC_LF#</VarDelimiter>
</Action>
<Action name="Write">
<Type>32</Type>
<Function>0</Function>
<DTIndentLevel>0</DTIndentLevel>
<Enabled>1</Enabled>
<ErrorHandling>
<UserNotificationMode>2</UserNotificationMode>
<CustomErrorMessage/>
<OnErrorAction>0</OnErrorAction>
<JumpToLabel/>
</ErrorHandling>
<FileName>%srcdir%\global_list_contents.txt</FileName>
<DataToWrite>%contents%</DataToWrite>
<IfFileExists>0</IfFileExists>
</Action>
</IR_ACTIONS_LIST>

grizedale
09-30-2002, 03:07 PM
Of course, I understand now that 1 of the random "files" is the containing "folder". How can I get around this?

Please bear in mind that I am new to this and that the your "code" at present meant very little, although I do intend to work through it at some point, thank you.

Lorne
09-30-2002, 03:19 PM
How do you get around it? Easily: just don't add that folder's path to the global list. The fact that the folder is there is indicative of a problem in how you've implemented Brett's example...so fix whatever it was that you broke. /ubbthreads/images/icons/smile.gif

Without seeing the specific combination of actions you're using to populate your global list, I can't be much more specific than that.

The code that I pasted was simply 2 actions that would output your global list to a text file, provided you set up a couple of design-time constants in your project (for the carriage return and linefeed characters needed to end each line in the text file). The whole point of that was to answer your other question, which was how you could know what the contents of a global list were at run time.

grizedale
09-30-2002, 03:29 PM
But, if I understand the reply to my original post from Brett correctly, the GlobalList entry needs to point to the folder to randomise the files therein. The code I have used is from the template from Brett attached in his message above.

Lorne
09-30-2002, 04:09 PM
The global list doesn't have to point to the folder. In fact, it shouldn't...all of its items should just be file paths.

Your folder wouldn't happen to have the string ".txt" in its name, would it? That might cause it to be returned by that "File - Search" action.

grizedale
09-30-2002, 05:22 PM
Got it, I had made the errror of pointing to the folder (unlike the test example), dont know where I got the idea from but removing the directory from the GlobalList solved the problem. (I had changed .txt for .pdf but worth the mention)

thank you for your time and assistance

regards

Stephen