• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Indesign Javascript - Save and overwrite file

Explorer ,
Mar 06, 2021 Mar 06, 2021

Copy link to clipboard

Copied

Hi I am writing a javascript script that will save the file to the users desktop. It has to work on both Mac and Windows environments. At the moment it is working perfectly on the Mac but on the PC I am geting an error.

The way I want it to work is:

If the file doesn't already exists on the desktop - create the file and save it.

If the file does already exists - then overwrite the exisitng file.

 

When I am running it on the PC and there is no file on the desktop with the specified name, it will save the new file to the desktop - which is good. I then run more scripts to make changes in the document and then run the same function to save and overwrite the file and it is fine.

I am getting and error if the file already exists on the desktop and I open it, make some changes and run the save function, it errors with: 

Error Numer 3591

Error String: Cannot save "TopStudentsScript1_Finished.indd" under a new name.

(Error code: 1)

 

Here is the code I am using:

 

saveDesktop();    
function saveDesktop() {

    //The desktop location on a Mac    
    var pathMac = "~/Desktop/";
    //get the users username on a PC
    var userNamePC = $.getenv("USERNAME");
    //put the PC username into the file path    
    var pathPC = "C:\\Documents and Settings\\"+userNamePC+"\\Desktop\\";
    //Returns what operating stsyem is running
    var whatOS = $.os;
    var isMacOS = whatOS.search( /Macintosh/i );
        // If isMacOS is 0 it made a match and it's a Mac. If it returns -1 it doesn't match and it is a PC (We don't have UNIX machines).
        if (isMacOS == 0) {
            //Save to Mac desktop
            app.activeDocument.save(new File(pathMac + "TopStudentsScript1_Finished.indd"));
        } else {
            //Save to PC desktop
            app.activeDocument.save(new File(pathPC+"TopStudentsScript1_Finished.indd"));
        }
}

 

 I thave tried a number of variations but nothing seems to let me overwrite the file. I am guessing that there is a difference if I have opened the file vs if I have created the file in the script but I am unsure what to try next.

Any help much appreciated.

TOPICS
How to , Scripting

Views

2.3K

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

Community Expert , Mar 06, 2021 Mar 06, 2021

Remove "new" from "new File(..." in your save function. If that doesn't work check: 

if (theFile.exists) before saving it with the path again. If it does exist, just call app.activeDocument.save() with no arguments, assuming app.activeDocument.name matches.

 

Votes

Translate

Translate
Explorer ,
Mar 06, 2021 Mar 06, 2021

Copy link to clipboard

Copied

To follow up, I only get that error if I have a file on the PC desktop with the file name "TopStudentsScript1_Finished.indd". 

I open that file - I run the save function and it errors.

If I have a file on the PC desktop with the file name "TopStudentsScript1_Finished.indd" but I don't open it, I create a new, unsaved Indesign file and then run the save function, it will overwrite the unopened file on the desktop with the same file name - which is good.

Votes

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
Community Expert ,
Mar 06, 2021 Mar 06, 2021

Copy link to clipboard

Copied

Remove "new" from "new File(..." in your save function. If that doesn't work check: 

if (theFile.exists) before saving it with the path again. If it does exist, just call app.activeDocument.save() with no arguments, assuming app.activeDocument.name matches.

 

Votes

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 ,
Mar 07, 2021 Mar 07, 2021

Copy link to clipboard

Copied

Thanks @brianp311 

Removing the new didn't do it but putting in the if/else statement testing for the existance of the file did the trick!

For reference:

saveDesktop();    
function saveDesktop() {

    //The desktop location on a Mac    
    var pathMac = "~/Desktop/";
    //get the users username on a PC
    var userNamePC = $.getenv("USERNAME");
    //put the PC username into the file path    
    var pathPC = "C:\\Documents and Settings\\"+userNamePC+"\\Desktop\\";
    //Name of the Indesign file
    var fileName = "TS-Script1_Finished.indd"
    //Returns what operating stsyem is running
    var whatOS = $.os;
    var isMacOS = whatOS.search( /Macintosh/i );
        // If isMacOS is 0 it made a match and it's a Mac. If it returns -1 it doesn't match and it is a PC (We don't have UNIX machines).
        if (isMacOS == 0) {
            //Save to Mac desktop
            app.activeDocument.save(File(pathMac+fileName));
        } else {
            //Save to PC desktop
            if(File(pathPC+fileName).exists === true) {
                app.activeDocument.save();
            } else {
            app.activeDocument.save(File(pathPC+fileName));
        }
}
}

Votes

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
Community Expert ,
Mar 07, 2021 Mar 07, 2021

Copy link to clipboard

Copied

I don’t think you have to test for the OS—look at the ESTK folder class:

http://jongware.mit.edu/Js/pc_Folder.html

 

 

 

 

var thePath = Folder.desktop + "/TopStudentsScript1_Finished.indd"
$.writeln(thePath)
//On OSX Returns: ~/Desktop/TopStudentsScript1_Finished.indd
//Windows returns: C:\\Documents and Settings\\username\\Desktop\\TopStudentsScript1_Finished.indd






//other folders:
$.writeln("System Application Support Folder: " + Folder.appData)
//In Windows, the value of %APPDATA% (by default, C:\\Documents and Settings\\All Users\\Application Data) In Mac OS, /Library/Application Support 

$.writeln("User Application Support Folder: " + Folder.userData)
//In Windows, the value of %USERDATA% (by default, C:\\Documents and Settings\\username\\Application Data) In Mac OS, ~/Library/Application Support. 

$.writeln("Documents Folder: " + Folder.myDocuments)
//In Windows, C:\\Documents and Settings\\username\\My Documents In Mac OS, ~/Documents 

$.writeln("System Folder: " + Folder.system)
//In Windows, the value of %windir% (by default, C:\\Windows) In Mac OS, /System 

$.writeln("Temp Folder: " + Folder.temp)
//The OS’s Temp Folder: /private/var/folders/w2/s1lk7d6d3m95v4pq7m4flscw0000gn/T/TemporaryItems

$.writeln("User Folder: " + Folder.trash)
//Trash Folder: ~/.Trash

 

 

 

Votes

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 ,
Mar 08, 2021 Mar 08, 2021

Copy link to clipboard

Copied

Thanks for the tip @rob day I will give that a try. It does look to be more efficient

Votes

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
Community Expert ,
Mar 08, 2021 Mar 08, 2021

Copy link to clipboard

Copied

Hi roryt91880286,

is this line really working on a Mac?

var userNamePC = $.getenv("USERNAME");

I can confirm that it is working on Windows 10, but currently I cannot test on a Mac.

Always thought that on a Mac it should be:

$.getenv("USER");

 

Or in short, if we'd like to test for the operating system:

$.getenv($.os[0] == "M" ? "USER" : "username");

 

Regards,
Uwe Laubender

( ACP )

Votes

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
Community Expert ,
Mar 08, 2021 Mar 08, 2021

Copy link to clipboard

Copied

Hi Uwe,

Wouldn’t  Folder.desktop + "/TopStudentsScript_Finished.indd" work on both OSX and Windows?

 

 

 

Votes

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 ,
Mar 08, 2021 Mar 08, 2021

Copy link to clipboard

Copied

Hi @Laubender I don't think that line

var userNamePC = $.getenv("USERNAME");

will work on a Mac but I am only using that variable for the PC path at the moment. Like you mentioned , "USER" is the syntax for the Mac and I can confirm that works on the Mac.

Based on what @rob day  and you have suggested I should be able to write the file to the destop in a system independant way.

Votes

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
Community Expert ,
Mar 08, 2021 Mar 08, 2021

Copy link to clipboard

Copied

Hi Rob,

"~/Desktop/"

is always the current user's desktop. Mac or PC.

So this would get all folders and files on the active user's desktop:

Folder( "~/Desktop/" ).getFiles()

Also this one, that you already mentioned:

Folder.desktop.getFiles()

 

And that means, if the goal is the current user's desktop there is no need to know the user name.

 

Regards,
Uwe Laubender

( ACP )

Votes

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 ,
Mar 08, 2021 Mar 08, 2021

Copy link to clipboard

Copied

LATEST

So based on all of the above recommendations I have simplified and tested a new script that will save and overwrite the file on both Mac and PC without having to test for the OS

saveDesktop("TS-Script1_Finished.indd");    
function saveDesktop(fileName) {
    var thePath = Folder.desktop + "/" + fileName;
    app.activeDocument.save(File(thePath));
}

Thanks @rob day@Laubender@brianp311   for your suggestions and help 

Votes

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