Script to "save as" to overwrite a file

Explorer ,
Feb 15, 2022 Feb 15, 2022

Copy link to clipboard

Copied

I was wondering if it is possible to write a script to do a save as that overwrites a file without having to deal with the warning dialog boxes e.g "...the file already existi. Do you want to replace it etc." 

 

The reason I want this;

I just found out that say you had a resource-heavy file with a bunch of images etc that was say 50MB, even if you delete all the content so it's a blank document and do a regular save, the file will still be 50MB. But if you did a save as and overwrote it it would go down to say 1MB. So doing regular saves causes a lot of file size bloating. If I could do save as's this would keep all my file sizes way down. I just dont like having to deal with the warning dialog boxes when doing so. 

 

Thanks!

TOPICS
Scripting

Views

189

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Advocate , Feb 15, 2022 Feb 15, 2022
Hello @danielw42205661, Give this a try, it will save over the open document when you execute the script even if no changes were made ...doc = app.documents[0]; myDocFolder = doc.filePath; doc.save(File(myDocFolder.fsName + "/" + doc.name)); Regards,Mike

Likes

Translate

Translate
Adobe Community Professional ,
Feb 15, 2022 Feb 15, 2022

Copy link to clipboard

Copied

Hi,

Try the following

// app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;
var doc = app.activeDocument;
var file = File(Folder.desktop + '/test.ai');
doc.saveAs(file);
// app.userInteractionLevel = UserInteractionLevel.DISPLAYALERTS;

 

I run it multiple times on same document, and it does not show any alert that file already exists. You can try and let us know if that works for you.

Best regards

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 15, 2022 Feb 15, 2022

Copy link to clipboard

Copied

Thanks for your reply.

 

When I try to run the script I get the error below. Do you know what I would be doing wrong?

danielw42205661_1-1644966336070.png

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Feb 15, 2022 Feb 15, 2022

Copy link to clipboard

Copied

My mistake. I have written that for Illustrator.

Best regards

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Feb 15, 2022 Feb 15, 2022

Copy link to clipboard

Copied

Hi,

Could you please the following version. I could not find direct methof saveAS but when I use stationary flag as true(second parameter), it keeps file size small

var doc = app.activeDocument;
var file = File(Folder.desktop + '/test.indd', true);
doc.save(file);

 

Also, I would like to have an expert's comment what does exactly stationary flag means here.

 

 

Best regards

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 15, 2022 Feb 15, 2022

Copy link to clipboard

Copied

Thanks again.

 

This doesnt overwrite the existing file I have open. It creates a new file on the desktop called "new", but it does indeed reduce the filesize down alot which is good. Is there a way to make it overwrite the initial file thats open and being worked on and not create a copy on the desktop? 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Feb 15, 2022 Feb 15, 2022

Copy link to clipboard

Copied

Did you try to change the path of the file?
I am using the hard code path, that's why it is saving at Desktop. Try following version assuming your file is saved once. 

 

 

var doc = app.activeDocument;
var _path = doc.fullName;
var file = File(_path);
doc.save(file, true);

 

 

Best regards

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 15, 2022 Feb 15, 2022

Copy link to clipboard

Copied

No sorry I hadn't.  I'm not very knowledgeable with code.

 

It seems like it has issues if the folder has spaces in it I think since I get the below error message.

 

I tested on an indesign document placed on the desktop and it ran without giving me the error. However, it created a copy on the desktop and replaced the spaces in the filename with %s.

danielw42205661_0-1644969914068.png

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Feb 15, 2022 Feb 15, 2022

Copy link to clipboard

Copied

Hello @danielw42205661,

 

Give this a try, it will save over the open document when you execute the script even if no changes were made ...

doc = app.documents[0];
myDocFolder = doc.filePath;
doc.save(File(myDocFolder.fsName + "/" + doc.name));

 

Regards,

Mike

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 15, 2022 Feb 15, 2022

Copy link to clipboard

Copied

Thank you so much! This worked perfectly. I really appreciate your help, this will be a lifesaver. 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Feb 19, 2022 Feb 19, 2022

Copy link to clipboard

Copied

LATEST

And the short version of Mike's script is

app.documents[0].save (app.documents[0].fullName)

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines