sketchucation logo sketchucation
    • Login
    🤑 SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

    Accessing File Locations defined in Preferences from Ruby

    Scheduled Pinned Locked Moved Developers' Forum
    8 Posts 4 Posters 926 Views 4 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.
    • fredo6F Offline
      fredo6
      last edited by

      Is there a way to get the directories defined in Preferences > Files ?

      In particular, I am looking for the default directory for Models (since when a file model is New, model.path just returns "", with no indication of folder).

      Fredo

      1 Reply Last reply Reply Quote 0
      • TIGT Offline
        TIG Moderator
        last edited by

        On Windows it's in the Registry.
        HKEY_CURRENT_USER\SOFTWARE\SketchUp\SketchUp 2015\File Locations

        Under the value ' Models'
        With data like ' C:\Users\TIG\Desktop\'

        Trying to read it with Sketchup.read_default('key', 'value') returns a syntax-error - BUT maybe you could rescue the error's output and parse out the actual path string ?

        There are also PC tools to read from the Registry with >=v2014 win32...

        There are probably equivalent plist tools for MAC ?

        TIG

        1 Reply Last reply Reply Quote 0
        • fredo6F Offline
          fredo6
          last edited by

          TIG,

          Many thanks and interesting way to get the data.

          However, I did not find a way to rescue

          
          	begin
          		Sketchup.read_default('File Locations', 'Models')
          	rescue Exception => e
          		puts "****Rescue"
          		puts "****Msg = #{e.msg}"
          	end	
          
          

          Whatever is the exception, Exception, SyntaxError, SystemError, ..., the rescue section is never called and the error message is displayed in the console.
          Normally, it should be a SyntaxError exceoption, based on the message.

          Error: #<SyntaxError: <main>: syntax error, unexpected $undefined, expecting tSTRING_CONTENT or tSTRING_DBEG or tSTRING_DVAR or tSTRING_END

          Also, curious to know how this behaves on Mac, because somehow this could be a portable way to access the Registry.

          1 Reply Last reply Reply Quote 0
          • S Offline
            slbaumgartner
            last edited by

            The Mac does not have equivalent preference settings to the Windows file locations. That panel is completely missing from the preferences. I have no idea why...

            1 Reply Last reply Reply Quote 0
            • TIGT Offline
              TIG Moderator
              last edited by

              This works >=v2014, it makes a hash named 'kvs' and you can access any key/value of that Registry entry - as the final line example for the 'Models' string...

              require 'win32/registry'
              kvs={}
              Win32;;Registry;;HKEY_CURRENT_USER.open('SOFTWARE\SketchUp\SketchUp 2015\File Locations'){|reg|
              	reg.each{|k|
              		kvs[k] = reg[k]
              	}
              }
              p kvs['Models']
              

              Change path to suit the SketchUp version - e.g. '... 2014...'
              The MAC side of things is a mystery - perhaps @slbaumgartner, @driven et al know more ?
              How does the MAC decide on the default 'Models' folder ?
              Is it something that is accessible from within Ruby ??

              TIG

              1 Reply Last reply Reply Quote 0
              • fredo6F Offline
                fredo6
                last edited by

                Actually, the error is uncatchable because it simply deals with the fact that the file path contains '' (backslash). For whatever reasons, this make the internal eval call fail (I imagine this is to evaluate the Ruby value from the string value).

                Otherwise, the method you indicate (with read_default) should work when the value is a number of a normal string. Indeed, you have to know the key and value for the parameter you look for in the registry.

                Possibly, this can even work on Mac.

                Fredo

                1 Reply Last reply Reply Quote 0
                • S Offline
                  slbaumgartner
                  last edited by

                  On a Mac the default folders appear to be kept in ~/Library/Preferences/com.sketchup.SketchUp.VVVV.plist (where VVVV is the version number starting with 2014). This is also where items written and read as defaults using the SketchUp Ruby API methods are stored. However, I have never found a way to read them via the Ruby API. The relevant keys are at root level in this plist, and Sketchup.read_default requires a first-level dictionary name as its first argument. I suppose you could resort to a low-level direct read akin to what TIG describes for the Windows registry, but I haven't tried that.

                  Edit: The values can be accessed via a system command such as

                  defaults read com.sketchup.SketchUp.2015 Sketchup.DefaultFolder.Models

                  That's not as tidy as an in-Ruby solution, but it should work.

                  Edit#2: On the Mac, these are reset by SketchUp as most recently used. So far as I know, there is no way for a user to specify them or to keep them stable.

                  1 Reply Last reply Reply Quote 0
                  • D Offline
                    driven
                    last edited by

                    I would use something like steve suggests...

                    %x(defaults read ~/Library/Preferences/com.sketchup.SketchUp.20#{Sketchup.version.to_i} "SketchUp.DefaultFolder.Models")
                    

                    BUT, it always returns the last Folder used for a Save...

                    learn from the mistakes of others, you may not live long enough to make them all yourself...

                    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