# Save a file with a predefined name

**URL:** <https://forums.sketchup.com/t/save-a-file-with-a-predefined-name/93597>\
**Category:** Ruby API\
**Tags:** sketchup\
**Created:** [April 18, 2019, 1:44pm UTC](https://forums.sketchup.com/t/save-a-file-with-a-predefined-name/93597 "2019-04-18T13:44:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nmiro](https://avatars.discourse-cdn.com/v4/letter/n/8e8cbc/32.png) [@nmiro](https://forums.sketchup.com/u/nmiro)\
**Post date:** [April 18, 2019, 1:44pm UTC](https://forums.sketchup.com/t/save-a-file-with-a-predefined-name/93597/1 "2019-04-18T13:44:12Z")

</div>

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

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

```

and to get the filename:

```auto
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?

---

<div class="post-metadata">

**Author:** ![Aerilius](https://avatars.discourse-cdn.com/v4/letter/a/e19b73/32.png) [@Aerilius](https://forums.sketchup.com/u/Aerilius)\
**Post date:** [April 18, 2019, 2:25pm UTC](https://forums.sketchup.com/t/save-a-file-with-a-predefined-name/93597/2 "2019-04-18T14:25:24Z")

</div>

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):

```auto
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).

---

<div class="post-metadata">

**Author:** ![DanRathbun](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/danrathbun/32/500_2.png) [@DanRathbun](https://forums.sketchup.com/u/DanRathbun)\
**Post date:** [April 18, 2019, 3:28pm UTC](https://forums.sketchup.com/t/save-a-file-with-a-predefined-name/93597/3 "2019-04-18T15:28:55Z")

</div>

> [@Aerilius](#):
>
> You cannot set the default file name in the save dialog under _File → Save as…_ .

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`](http://ruby.sketchup.com/UI.html#savepanel-class_method) is valid for use on MS Windows …

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

```

… or more specifically to this thread question …

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

```

Shows (on my machine using Win10 darktheme) …

![image](https://global.discourse-cdn.com/sketchup/original/3X/a/e/ae970c9427a77a39077d248cee44a754a546cb4e.png)

Perhaps Apple Mac ignores the 3rd argument ?

---

<div class="post-metadata">

**Author:** ![Aerilius](https://avatars.discourse-cdn.com/v4/letter/a/e19b73/32.png) [@Aerilius](https://forums.sketchup.com/u/Aerilius)\
**Post date:** [April 18, 2019, 9:07pm UTC](https://forums.sketchup.com/t/save-a-file-with-a-predefined-name/93597/4 "2019-04-18T21:07:10Z")

</div>

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…_

---

<div class="post-metadata">

**Author:** ![DanRathbun](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/danrathbun/32/500_2.png) [@DanRathbun](https://forums.sketchup.com/u/DanRathbun)\
**Post date:** [April 19, 2019, 3:11pm UTC](https://forums.sketchup.com/t/save-a-file-with-a-predefined-name/93597/5 "2019-04-19T15:11:08Z")

</div>

> [@Aerilius](#):
>
> But it creates a custom file dialog instance (as I wrote we can do), …

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

> [@Aerilius](#):
>
> … but it does not permanently manipulate the behavior of the native menu item _File → Save as…_

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.
