Skip to content

Instantly share code, notes, and snippets.

@DanRathbun
Created January 13, 2020 18:43
Show Gist options
  • Save DanRathbun/20131795a0ddbd83a27f354e2dde59f3 to your computer and use it in GitHub Desktop.
Save DanRathbun/20131795a0ddbd83a27f354e2dde59f3 to your computer and use it in GitHub Desktop.
SketchUp Embedded Ruby System module
# encoding: UTF-8
#
# Boilerplate Ideas for SketchUp Embedded Ruby System module.
if (Sketchup.platform == :platform_win rescue RUBY_PLATFORM !~ /darwin/)
module System # for MS Windows
require 'win32ole'
extend self
def is_windows?
true
end
def is_macos?
false
end
WIN = true
MAC = false
# Request the value of an environment variable.
# @param var [String] The variable name requested.
# @return [String] On MS Windows this returns strings encoded as UTF-8.
# @return [nil] if the variable name does not exist.
def get_env(var)
unless var.respond_to?(:to_s)
fail(TypeError,'String (or coercible) argument expected.',caller)
end
WIN32OLE.new('WScript.Shell').ExpandEnvironmentStrings("%#{var}%")
end
# The computer or hostname assigned to the system.
def computer_name
get_env('ComputerName')
end
# The name of the current user.
def user_name
get_env('Username')
end
end # System
else
module System # for MacOS
extend self
def is_windows?
false
end
def is_macos?
true
end
WIN = false
MAC = true
# The computer or hostname assigned to the system.
def computer_name
ENV['HOST'] || ENV['HOSTNAME'] || `hostname`
end
# Request the value of an environment variable.
# @param var [String] The variable name requested.
# @return [String] On MacOS the ENV object returns strings encoded as UTF-8.
# @return [nil] if the variable name does not exist.
def get_env(var)
unless var.respond_to?(:to_s)
fail(TypeError,'String (or coercible) argument expected.',caller)
end
ENV["#{var}"]
end
# The name of the current user.
def user_name
ENV['USER']
end
end # System
end # platform conditional definitions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment