• Login
sketchucation logo sketchucation
  • Login
ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

How to store slected file directory name on a variable?

Scheduled Pinned Locked Moved Developers' Forum
6 Posts 3 Posters 287 Views 3 Watching
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    renderiza
    last edited by 21 Apr 2013, 23:02

    Hi, I want to have the user open a directory folder (expample C:\Users\name\Desktop\GOSUFiles)...then pick a file (example: sound.wav) and click ok.

    After user click ok the directory address of that file will be stored in a variable like this...

    @sound = C:\Users\name\Desktop\GOSUFiles\sound.wav

    How can I do this?

    Thanks in advance!!!

    [url=https://www.sketchupcode.com/:z3kqsidd]My Extensions ...[/url:z3kqsidd]

    1 Reply Last reply Reply Quote 0
    • D Offline
      Dan Rathbun
      last edited by 22 Apr 2013, 00:52

      Easy ..

      def get_sound_file(title = "Choose a sound file")
        filepath = UI.openpanel( title, "*.wav" )
        if filepath
          @sound = filepath.gsub("\\",'/')
        else
          return nil
        end
      end # method
      
      

      UI.openpanel( title, "*.wav" )
      On Windows the browser will open to the last path where the user selected a ".wav" file.
      Windows keeps track of MRU (Most Recently Used) paths for all file extensions.

      If you want to put the user in a specific directory, then:
      UI.openpanel( title, absolute_path, "*.wav" )

      If you do not want to filter by extension...
      UI.openpanel( title, absolute_path )

      The UI file browse methods are bugged and we cannot specify multiple file extensions.

      💭

      I'm not here much anymore.

      1 Reply Last reply Reply Quote 0
      • R Offline
        renderiza
        last edited by 22 Apr 2013, 06:20

        :: Dan Rathbun ::

        Thank you so much this was exactly what I wanted!

        I have another question... 😳

        Lets say I only want to choose folder and not a file...how can this be done?

        Again thanks!

        [url=https://www.sketchupcode.com/:z3kqsidd]My Extensions ...[/url:z3kqsidd]

        1 Reply Last reply Reply Quote 0
        • R Offline
          renderiza
          last edited by 22 Apr 2013, 07:22

          I kinda found a way, here it is...

          def get_folder(title = "Choose a folder")
            filepath = UI.openpanel( title, "" )	
            if filepath
              file_path = filepath.gsub("\\",'/')
              @folder = File.dirname(file_path) 
            else
              return nil
            end
          end # method
          

          Only draw back is that you need to pick file in other to get folder anyway...but its ok I guess.

          Cheers!

          [url=https://www.sketchupcode.com/:z3kqsidd]My Extensions ...[/url:z3kqsidd]

          1 Reply Last reply Reply Quote 0
          • T Offline
            thomthom
            last edited by 22 Apr 2013, 08:39

            @dan rathbun said:

            @sound = filepath.gsub("\\",'/')

            Since / as path separator isn't hard coded I tend to use:
            @sound = File.expand_path(filepath)

            One could also do:
            @sound = filepath.gsub("\\", File::SEPARATOR)

            But I prefer the first.

            Thomas Thomassen — SketchUp Monkey & Coding addict
            List of my plugins and link to the CookieWare fund

            1 Reply Last reply Reply Quote 0
            • D Offline
              Dan Rathbun
              last edited by 22 Apr 2013, 10:07

              @Rafael: The API UI module does not have a folderpicker, YET. We have filed Feature Requests for one, and fontpicker, and colorpicker, and etc., etc.

              For now users must pick a file within the target folder. (We are out of luck if the folder is empty.)

              @thomthom said:

              @dan rathbun said:

              @sound = filepath.gsub("\\",'/')

              Since / as path separator isn't hard coded I tend to use:
              @sound = File.expand_path(filepath)

              One could also do:
              @sound = filepath.gsub("\\", File::SEPARATOR)

              But I prefer the first.

              Your second one is safer. Yes I got lazy, but my purpose here is going from the OS to Ruby. I imagine that after tha statement the path will be used by Ruby's Dir and File methods.

              Later on... to go back to the OS I'd use filepath.gsub("\/", File::SEPARATOR) if it is even necessary.

              Be careful with expand_path() because it is DUMB!
              Your example will work only because the UI.openpanel method returns an absolute path.

              When the path is a relative path,... the method simply concatenates the current working directory path and the argument path, regardless if the result is really a valid path.

              I'm not here much anymore.

              1 Reply Last reply Reply Quote 0
              • 1 / 1
              1 / 1
              • First post
                1/6
                Last post
              Buy SketchPlus
              Buy SUbD
              Buy WrapR
              Buy eBook
              Buy Modelur
              Buy Vertex Tools
              Buy SketchCuisine
              Buy FormFonts

              Advertisement