sketchucation logo sketchucation
    • Login
    Oops, your profile's looking a bit empty! To help us tailor your experience, please fill in key details like your SketchUp version, skill level, operating system, and more. Update and save your info on your profile page today!
    ⚠️ Important | Libfredo 15.6b introduces important bugfixes for Fredo's Extensions Update

    How to store slected file directory name on a variable?

    Scheduled Pinned Locked Moved Developers' Forum
    6 Posts 3 Posters 334 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.
    • renderizaR Offline
      renderiza
      last edited by

      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
      • Dan RathbunD Offline
        Dan Rathbun
        last edited by

        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
        • renderizaR Offline
          renderiza
          last edited by

          :: 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
          • renderizaR Offline
            renderiza
            last edited by

            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
            • thomthomT Offline
              thomthom
              last edited by

              @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
              • Dan RathbunD Offline
                Dan Rathbun
                last edited by

                @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
                • First post
                  Last post
                Buy SketchPlus
                Buy SUbD
                Buy WrapR
                Buy eBook
                Buy Modelur
                Buy Vertex Tools
                Buy SketchCuisine
                Buy FormFonts

                Advertisement