Trouble loading scripts outside Plugins directory
-
I'm trying to learn Ruby and stay a bit organized. So I wanted to place my personal ruby scripts under "Documents/Sketchup/Marc_Scripts" on my Mac. I tried following the Google Ruby API website and so I created a script called "myrubyscripts.rb" under the Sketchup Plugins directory. In it I put:
home = File.expand_path "-"
myrubyscripts = File.join home, "MyRubyScripts"
require_all("Documents/Sketchup/Marc_Scripts")But it doesn't work. What am I doing wrong?
Marc
-
Hi Marc,
I think you need to use the full path to your plugins:
require 'sketchup' require_all('/Users/YourUserName/Documents/SketchUp/Marc_Scripts')
-
@mgianzero said:
home = File.expand_path "-"
myrubyscripts = File.join home, "MyRubyScripts"
require_all("Documents/Sketchup/Marc_Scripts")You are not using your variable
myrubyscripts
for anything. Looks like you are passing only part of the path.
Use someputs
to investigate the data you pass around when you experience unexpected problems. That way you can verify that the data you think you pass is what you actually pass. -
@mgianzero said:
I tried following the Google Ruby API website and so .... But it doesn't work. What am I doing wrong?
The example assumes your script dir is a subdir of 'Plugins'.
It does not explain the "tricks" of having a dir elsewhere.@mgianzero said:
So I wanted to place my personal ruby scripts under "Documents/Sketchup/Marc_Scripts" on my Mac. I created a script ... In it I put:
home = File.expand_path "-"
(1) the HOME shortcut is a tilde char "~" not a dash.
(2) You dont need a "home" var because it's already defined in the Environment Variable Hash on the Mac. To access it from Ruby use:ENV['HOME']
So... the following:@mgianzero said:
myrubyscripts = File.join home, "MyRubyScripts" require_all('Documents/Sketchup/Marc_Scripts')
..becomes:
require 'sketchup' myscripts = File.join(ENV['HOME'],'Documents/Sketchup/My_Scripts')
require_all( myscripts )
This is the equivalent of what Jim showed with a literal argument.
The drawback is you wind up with all the script entries in the $" array being long absolute paths. You could save memory space by stealing and modifying the code block within sketchup.rb::require_all, like so:
begin # require('sketchup') ## not needed myscripts = ENV['HOME']+'/Documents/Sketchup/My_Scripts' $;.push( myscripts ) # add to $LOAD_PATH array old_wd = Dir.getwd # save old working dir wd = Dir.chdir( myscripts ) rbfiles = Dir["*.{rb,rbs}"] rbfiles.each {|f| if f.split('.').last=='rbs' Sketchup.require( f ) else require( f ) end } Dir.chdir(old_wd) # restore old working dir end
-
Thanks Dan. I think I'll stick with your simplified solution. Works perfectly!
Now I can keep my plugins separate from the ones I downloaded from other folks.
Marc
Advertisement