Announcement

Collapse
No announcement yet.

KB: Making a Document Browser

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • KB: Making a Document Browser

    AutoPlay Media Studio 5.0 Knowledge Base

    Making a Document Browser

    Document ID: IR10050
    The information in this article applies to:
    • AutoPlay Media Studio 5.0 Professional Edition

    SUMMARY

    This article describes how to make a document browser

    DISCUSSION

    As an example, we will create an application that has the user select a folder on his drive, and then populates a listbox object with all of the *.doc files within that directory. The user clicks on a file in the listbox object, and clicks the "Open" button to open the document.

    1. Create a project with one listbox object, and two button objects.

    2. Label Button1 "Load" and Button2 "Open".

    3. Insert the following code into the On Click event for Button1:
      --Disable listbox Updating  ListBox.SetUpdate("ListBox1", false);



      --Get the desired folder to browse

      folder = Dialog.FolderBrowse("Open Folder", "C:\\");



      --populate a table with all the .doc files

      file = File.Find(folder, "*.doc", false, false, nil);



      --do the following for each file:

      for j,file_path in file do

      --add the item to the listbox, with the name visible and path as data

      ListBox.AddItem("ListBox1", String.SplitPath(file_path).Filename, file_path);

      end



      --Allow the listbox to display the updated content ListBox.SetUpdate("ListBox1", true);
    4. Insert the following code into the On Click event for Button2:
      selected = ListBox.GetSelected("ListBox1");

      for j,k in selected do

      File.Open(ListBox.GetItemData("ListBox1", k),"", SW_SHOWNORMAL);

      end

    MORE INFORMATION

    KEYWORDS: AutoPlay Media Studio 5.0, Document, Browser


    Last reviewed: September 26, 2003
    Copyright © 2003 Indigo Rose Corporation. All rights reserved.

  • #2
    Error Message Pops Up If No File Exist

    Hi,

    I tested this example.

    The FILE BROWSER OPENS. Good.

    However, when I attempted to LOAD a FOLDER that contains NONE of the .doc files, this error message appears: ON CLICK, Line 11: attempt to call nil value.

    How do I get rid of this error message? Better still, a message will appear saying that no such files, and tell us to try another folder.

    Thanks

    Azman
    Newbie Examples
    ------> AMS 7.5 : amstudio.azman.info
    ----> AMS 6 & 5: www.azman.info/ams/
    ----> FB: facebook.com/GuideToWealth

    ----> Content Development Blog: www.AZMAN.asia

    Comment


    • #3
      In the section where you enumerate the files.
      Code:
      for j,file_path in file do
            --add the item to the listbox, with the name visible and path as data
            ListBox.AddItem("ListBox1", String.SplitPath(file_path).Filename, file_path);
      end
      encapsulate that bit of code in an if statement that checks to make sure the table file contains data.

      Code:
      if file then
            for j,file_path in file do
                  --add the item to the listbox, with the name visible and path as data
                  ListBox.AddItem("ListBox1", String.SplitPath(file_path).Filename, file_path);
            end
      end
      That way you wont run the code unless you actually find files.

      Tigg
      TJ-Tigger
      "A common mistake that people make when trying to design something completely foolproof was to underestimate the ingenuity of complete fools."
      "Draco dormiens nunquam titillandus."
      Map of IR Forum Users - IR Project CodeViewer - Online Help - TiggTV - QuizEngine

      Comment


      • #4
        Another way

        Hi Tigg,

        And this is what Adam suggested and works as well.

        Code:
        if file ~= nil then
        
        --do the following for each file:
        for j,file_path in file do
        --add the item to the listbox, with the name visible and path as data
        ListBox.AddItem("ListBox1", String.SplitPath(file_path).Filename, file_path);
        end
        
        --Allow the listbox to display the updated content
        ListBox.SetUpdate("ListBox1", true); 
        
        else
        Dialog.Message("Notice","No Documents were found in that directory");
        end
        Thanks Tigg.

        Azman
        Newbie Examples
        ------> AMS 7.5 : amstudio.azman.info
        ----> AMS 6 & 5: www.azman.info/ams/
        ----> FB: facebook.com/GuideToWealth

        ----> Content Development Blog: www.AZMAN.asia

        Comment


        • #5
          More File Types

          Hi,

          How would look for more than 1 file type. Say, I would like to search for .doc and .ppt at the same time from the same folder?

          So, if this is the case, how could I actually display even the file extensions to differentiate between the 2 types.

          Any ideas?

          I'm not a programmer , so I need some practical ideas as a kick-start.

          Thanks

          Azman
          Newbie Examples
          ------> AMS 7.5 : amstudio.azman.info
          ----> AMS 6 & 5: www.azman.info/ams/
          ----> FB: facebook.com/GuideToWealth

          ----> Content Development Blog: www.AZMAN.asia

          Comment


          • #6
            Here is an example of what I have done in the past

            Code:
            --Get the desired folder to browse
            folder = Dialog.FolderBrowse("Open Folder", "C:\\");
            tbSearchFiles = {"*.doc", "*.ppt"};
            
            for index,type in tbSearchFiles do
                   file = File.Find(folder, type, false, false, nil);
                   
                   if file ~= nil then
            
                          --do the following for each file:
                          for j,file_path in file do
                                 --add the item to the listbox, with the name visible and path as data
                                 ListBox.AddItem("ListBox1", String.SplitPath(file_path).Filename, file_path);
                          end
            
                   else
                          Dialog.Message("Notice","No Documents were found in that directory");
                   end
            end
            
            --Allow the listbox to display the updated content
            ListBox.SetUpdate("ListBox1", true);
            Just add the file for which you want to search to the tbSearchFiles table and let 'er rip, so to speak.

            Hope this helps.

            If I can find it again, this is also on the forums somewhere out there.

            Tigg
            TJ-Tigger
            "A common mistake that people make when trying to design something completely foolproof was to underestimate the ingenuity of complete fools."
            "Draco dormiens nunquam titillandus."
            Map of IR Forum Users - IR Project CodeViewer - Online Help - TiggTV - QuizEngine

            Comment


            • #7
              Here is another version of the above code that I posted on the forums before

              http://www.indigorose.com/forums/showthread.php?t=6864
              TJ-Tigger
              "A common mistake that people make when trying to design something completely foolproof was to underestimate the ingenuity of complete fools."
              "Draco dormiens nunquam titillandus."
              Map of IR Forum Users - IR Project CodeViewer - Online Help - TiggTV - QuizEngine

              Comment


              • #8
                Both Worked Well

                Hi Tigg,

                Both scripts worked very well.

                One item is missing, i.e. The File Extension.

                When we list in the ListBox only the filenames can be seen. What do we need to do to show the File Extensions?

                Thanks

                Azman
                Newbie Examples
                ------> AMS 7.5 : amstudio.azman.info
                ----> AMS 6 & 5: www.azman.info/ams/
                ----> FB: facebook.com/GuideToWealth

                ----> Content Development Blog: www.AZMAN.asia

                Comment


                • #9
                  The splitpath command was used when added to a listbox and it did not include the extension. Try this.

                  Code:
                  --Get the desired folder to browse
                  folder = Dialog.FolderBrowse("Open Folder", "C:\\");
                  tbSearchFiles = {"*.doc", "*.ppt"};
                  
                  for index,type in tbSearchFiles do
                         file = File.Find(folder, type, false, false, nil);
                         
                         if file ~= nil then
                  
                                --do the following for each file:
                                for j,file_path in file do
                                       --add the item to the listbox, with the name visible and path as data
                                       tbFilePath = String.SplitPath(file_path);
                                       ListBox.AddItem("ListBox1", tbFilePath.Filename..tbFilePath.Extension, file_path);
                                end
                  
                         else
                                Dialog.Message("Notice","No Documents were found in that directory");
                         end
                  end
                  
                  --Allow the listbox to display the updated content
                  ListBox.SetUpdate("ListBox1", true);
                  TJ-Tigger
                  "A common mistake that people make when trying to design something completely foolproof was to underestimate the ingenuity of complete fools."
                  "Draco dormiens nunquam titillandus."
                  Map of IR Forum Users - IR Project CodeViewer - Online Help - TiggTV - QuizEngine

                  Comment


                  • #10
                    Aloha

                    Working with listbox in a DEMO (before I shell out my money). I understand the script - but - how do I make it look at multiple drives for a specific folder. Not knowing which drive the CD will be in on different machines - but knowing the folder name, how do I do this?

                    I tried
                    result = Dialog.FolderBrowse("Please select a folder:", "AutoPlay\\MyFolderName");
                    and modified this line to look for .ZIP files instead of .doc files
                    file = File.Find(folder, "*.zip", false, false, nil);

                    When I click "Load" - it opens a pop-up that lists my "C:/My Documents" instead of going to the Folder that I specified. Any ideas? Or is this just because it is in Preview mode and only looking at what I created and saved on my "C" drive.

                    Also, is it possible to open a .zip and then open a .pdf or .doc or .whatever after - all in one smooth move?

                    Please bear with me - I am still learning.

                    Thanks in advance

                    Lar

                    Comment

                    Working...
                    X