Save a file with a predefined name

Hi everyone, I’m trying to save a file in Sketchup with a predefined name.
The name will be saved previously doing this:

model.set_attribute "test", "filename", "test.skp"

and to get the filename:

model.get_attribute("test", "filename")

But the question is, I want that when I open the ‘File --> Save As’ , the test.skp appears automatically in the File name box. How can I do it in Ruby?

The Ruby API focuses on the model/scene graph datastructures, not on fully scripting the UI (when automating things, you would usually want to automate the actions and bypass the slow UI). You cannot set the default file name in the save dialog under File → Save as….

You can however set a suggested file name when you trigger a custom file dialog. You can then either add it to the menu or create a toolbar button for it.
Something like (untested):

UI.menu("File").add_item("SuperSave©") {
  begin
    filepath = UI.savepanel("Save as…", File.expand_path("~"))
    unless filepath.nil? # User cancelled file dialog.
      Sketchup.active_model.save(filepath)
    end
  rescue StandardError => e
    UI.messagebox("Failed to save", MB_MULTILINE, e.message)
  end
}

However consider it bad practice to save environment-specific data, like operating system specifics, device specifics, in document files (that are not configuration files).

For example if you save absolute file paths, they will file on systems with other directory layout (especially if you work on a non-unix system like Windows where drive letters can differ from one installation to another). Instead, you can savely save paths relative to the current document’s path, the current working directory or the user’s home directory (but ensure that any subdirectories exist).

1 Like

This conclusion is not correct for MS Windows.
On Windows platform we CAN insert a default filename into the “Save As” dialog “filename” input field.

The code snippet in the API documentation for UI.savepanel is valid for use on MS Windows …

path_to_save_to = UI.savepanel("Save Image File", "c:\\", "Shapes.jpg")

… or more specifically to this thread question …

filepath = UI.savepanel("Save As",File.join(ENV['USERPROFILE'],'Documents'),"test.skp")

Shows (on my machine using Win10 darktheme) …

image

Perhaps Apple Mac ignores the 3rd argument ?

But it creates a custom file dialog instance (as I wrote we can do), but it does not permanently manipulate the behavior of the native menu item File → Save as…

I guess I thought that is what the OP was asking for.

True but on MS Windows the common Save As dialog will automatically stuff the previous saved name into the Filename field. This is a OS common behavior.

If the file has never been saved then “Untitled.skp” is stuffed into the filename field.

1 Like