Accessing File Locations defined in Preferences from Ruby
-
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
-
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,
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.
-
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...
-
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 ?? -
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
-
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.
-
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...
Advertisement