Accessing System Preferences from Ruby
-
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 -
This PC only code returns the keys and values for any valid Registry entry.
See the notes at the end for usagerequire '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 sayTIG-getregentries.rb
in the Plugins folder, to use type the example in the Ruby Console OR inside a script...
Advertisement