Example: Command to iterate a folder of Components and set their name property.
SketchUp version 2015+
RenameComponentsByFolder.rb (5.4 KB)
BACKUP YOUR FOLDERS BEFORE RUNNING THIS UTILITY!
THIS IS AN EXAMPLE. NO WARRANTY EXPRESSED OR IMPLIED.
Choose a folder of components and this utility will use the filename
to generate a component name property. If the name property is not
empty and you’ve chosen to manually edit names, then you’ll be
prompted whether you wish to replace it. If you choose No, then a
manual edit cycle starts which you can cancel to leave the name as is.
If you’ve decided not to manually edit, then files with non-empty
name properties will be skipped.
The manual edit cycle has two steps. The first step you’ll choose one
of two text strings to base your edit upon. The second step is the
actual edit using the string chosen in step 1. You can cancel the edit
at either step to leave the name property unedited.
If all of the components in the folder have empty name properties,
then you’ll not see any edit prompts the first time that folder is
processed. The same silent process also happens if the filenames and
name properties match, but you are just forcing names to titlecase.
If you have the Ruby Console open you will see a report listing of
components files processed and modified at the end of the run.
BACKUP YOUR FOLDERS BEFORE RUNNING THIS UTILITY!
# encoding: UTF-8
# Sketchup version 2015+
#
# RenameComponentsByFolder.rb
#
# BACKUP YOUR FOLDERS BEFORE RUNNING THIS UTILITY!
#
# THIS IS AN EXAMPLE. NO WARRANTY EXPRESSED OR IMPLIED.
#
# Choose a folder of components and this utility will use the filename
# to generate a component name property. If the name property is not
# empty and you've chosen to manually edit names, then you'll be
# prompted whether you wish to replace it. If you choose No, then a
# manual edit cycle starts which you can cancel to leave the name as is.
#
# If you've decided not to manually edit, then files with non-empty
# name properties will be skipped.
#
# The manual edit cycle has two steps. The first step you'll choose one
# of two text strings to base your edit upon. The second step is the
# actual edit using the string chosen in step 1. You can cancel the edit
# at either step to leave the name property unedited.
#
# If all of the components in the folder have empty name properties,
# then you'll not see any edit prompts the first time that folder is
# processed. The same silent process also happens if the filenames and
# name properties match, but you are just forcing names to titlecase.
#
# If you have the Ruby Console open you will see a report listing of
# components files processed and modified at the end of the run.
#
# BACKUP YOUR FOLDERS BEFORE RUNNING THIS UTILITY!
#
module Example
module ComponentRenamer
extend self
WIN =(Sketchup.platform == :platform_win rescue RUBY_PLATFORM !~ /darwin/)
@@loaded = false unless defined?(@@loaded)
def start(caps = true)
#
choice = UI.messagebox(
'REMINDER! Backup your folder before running this utility!',MB_OKCANCEL
)
return if choice != IDOK
#
dir = UI.select_directory(
title: "Select Directory for processing of Components ...",
directory: Sketchup.find_support_file("Components")
)
return unless dir
#
choice = UI.messagebox('Convert component names to titlecase?',MB_YESNO)
caps =( choice == IDYES ? true : false )
#
choice = UI.messagebox('Manually edit non-empty component names?',MB_YESNO)
edit =( choice == IDYES ? true : false )
#
# Causes file save prompt to appear if current model is modified:
Sketchup.file_new if WIN
#
result = IDOK
processed = []
modified = []
#
Dir.foreach(dir) { |filename|
next if filename == '.' || filename == '..' || File.extname(filename) !~ /skp/i
puts "Processing #{filename} ..."
edited = false
# Open the file:
Sketchup.open_file(File.join(dir,filename))
model = Sketchup.active_model
# If the name property is empty, rename it:
# (We'll also convert underscores to spaces.)
proposed = File.basename(filename,'.*').gsub(/(_)/,' ')
proposed = proposed.split.map(&:capitalize).join(' ') if caps
#
if model.name.empty?
model.name= proposed
edited = true
elsif caps && # and they're the same except their case differs ...
(model.name.downcase == proposed.downcase && model.name != proposed)
model.name= proposed
edited = true
elsif edit
# Notify user of current name and prompt to replace:
msg = "Name for \"#{filename}\" is not empty.\n\n"
msg<< "Current: \"#{model.name}\"\n\n"
msg<< "Replace with: \"#{proposed}\" ?"
choice = UI.messagebox(msg,MB_YESNOCANCEL)
if choice == IDCANCEL
UI.messagebox('Cancelling operation.')
break result = IDCANCEL
elsif choice == IDYES
model.name= proposed
edited = true
else
mname =( caps ? model.name.split.map(&:capitalize).join(' ') : model.name )
text = UI.inputbox(
['Edit based upon ...'],
["#{mname} "],
["#{mname} |#{proposed} "],
'Edit? (Cancel to skip this file)'
)
if text
newname = UI.inputbox(['Name'],[text.first],'Editing Name')
if newname
model.name= newname.first.rstrip
edited = true
end
end
end
end
processed << filename
if edited
modified << filename
model.save
# SU2016 and older will have saved an empty SKB file:
if Sketchup.version.to_i < 17
backup = File.join(dir,File.basename(filename,'.*')<<'.skb')
File.delete(backup) if File.exist?(backup)
model.save_copy(backup)
end
end
# We are done with this model file:
model.close if !WIN # only needed on Mac
}
model.close if !WIN && result == IDCANCEL
# We are done with the folder's files:
Sketchup.file_new if WIN
#
UI.messagebox("Files processed: #{processed.size}\nFiles modified: #{modified.size}")
puts
puts "Files processed:"
puts processed.inspect
puts
puts "Files modified:"
puts modified.inspect
puts
end
if !@@loaded
UI.menu('Plugins').add_item('Rename multiple Components...') { start() }
@@loaded = true
end
end # ComponentRenamer
end # Example