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

Accessing System Preferences from Ruby

Scheduled Pinned Locked Moved Developers' Forum
2 Posts 2 Posters 264 Views
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.
  • J Offline
    joshb
    last edited by 6 Jan 2012, 16:12

    I'm trying to access the default file paths in Ruby, that are set from the Files section of the Windows, Preferences dialog. I can't seem to find any way in the Ruby API to do this.

    I poked around the registry and noticed that these settings are stored in the HKCU\Software\Google\Sketchup6\FileLocations key. I though I'd be able to use the Sketchup.read_defaults methods to access these settings but the method seems to have trouble reading strings from the registry. If I call

    Sketchup.read_default "File Locations", "Models", "default"

    I get back nil. However, if I change one of the parameters to something that doesn't exist, such as

    Sketchup.read_default "File Locations X", "Models", "default"

    I get back "default". So the method is finding the entry in the registry, but I suppose it just doesn't know how to return REG_SZ values. This is probably fixed in a newer version but unfortunately I'm stuck with 6 for a while.

    Any suggestions on how to access the file path preference?

    Thanks,
    Josh

    1 Reply Last reply Reply Quote 0
    • T Offline
      TIG Moderator
      last edited by 6 Jan 2012, 19:41

      This PC only code returns the keys and values for any valid Registry entry.
      See the notes at the end for usage

      require 'sketchup.rb'
      module TIG ### for PC use only !
      def self.getregentries(key="")
        key.tr!("/","\\")
        cfile=File.join(ENV["TEMP"], "regentries.cmd")
        tfile=File.join(ENV["TEMP"], "regentries.tmp")
        f=File.open(cfile, "w")
         f.puts("reg query \""+key+"\" /f \"*\" /s > \""+tfile+"\"")
        f.close
        UI.openURL("file;///"+cfile)
        sleep(0.1) until File.exist?(tfile)
        sleep(0.1)
        lines=IO.readlines(tfile)
        hents={}
        lines.each{|line|
          line.chomp!
      	next if line.empty? or line=~/#{key}/ or line=~/^End/
      	ent=line.split("    ")
      	hents[ent[1]]=ent[3]
        }
        File.delete(tfile)if File.exist?(tfile)
        File.delete(cfile)if File.exist?(cfile)
        return hents
      end
      end
      ### Example;-
      ### ents=TIG.getregentries "HKEY_CURRENT_USER\\Software\\Google\\SketchUp8\\File Locations"
      ### Find the ents entry 'by name' thus;
      ### ents["Components"] >>> 'C;\Program Files\Google\Google SketchUp 8\Components\'
      ### Returns 'nil' if there is no key.
      ###
      

      Read the notes at the end on how to get a key/value.
      Copy/paste code into a file called say TIG-getregentries.rb in the Plugins folder, to use type the example in the Ruby Console OR inside a script...

      TIG

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

      Advertisement