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

New __SaveOptions() instance in function crashes Photoshop

Community Beginner ,
Mar 17, 2017 Mar 17, 2017

Copy link to clipboard

Copied

Greetings! I previously wrote this script​ for incrementing file names in Photoshop quickly. It's sloppy code, but it works as expected.

Today I decided to clean the code up and move some lengthy code that gets used twice into a separate function. The new code is below.

The new JPEGSaveOptions(), new PNGSaveOptions(), new PhotoshopSaveOptions(), and new TiffSaveOptions() commands were moved from the main body of the code to the function saveFile at the bottom of the code. When Photoshop executes these lines (in red below) to instantiate new SaveOptions objects, it crashes without error message. There is no error noted in ExtendScript ToolKit, which continues running just fine.

Any ideas why this would happen? I'm guessing it's related to the scope of the document, but I'm not sure what the remedy would be aside from reverting back to the unabstracted code.

Thank you all for your time and help! :]

-----------------------------------------------------------------------------------------------------

// Script by Vince Petaccio of PetRok Studios. | www.PetRokStudios.com

// Ensure a document is open

if ( documents.length > 0 )

{

    // Change dialog settings

  var originalDialogMode = app.displayDialogs;

  app.displayDialogs = DialogModes.ERROR;

    try

    {

    // Get the document

        var docRef = activeDocument;

        // Get the path for the document

        var filePath = docRef.path + '/';

  

        // Get the filename of the document

        var fileNameNoExtension = docRef.name;

       

        // Split the filename at the dots

        fileNameNoExtension = fileNameNoExtension.split( "." );

       

        // If there's a dot, remove everything after the last dot

        if ( fileNameNoExtension.length > 1 ) {

            // Get the file type

            fileType = fileNameNoExtension[fileNameNoExtension.length-1];

           

            // Decrement the filename object by removing the last element (the stuff after the last .)

          fileNameNoExtension.length--;

           

            // Reconnect everything BEFORE the first .

            fileNameNoExtension = fileNameNoExtension.join(".");

        }

        // Get the last five characters of the filename

        lastFive = fileNameNoExtension.slice(-5);

        allButFive = fileNameNoExtension.slice(0,-6);

        // Check whether the last five characters are digits

        newDoc = isNaN(parseInt(lastFive))

        // If this is a new document, then add _00000 to the file name

        if (newDoc) {

            // Make the new file name

            newFileName = fileNameNoExtension + '_00000';

           

            // Do the saving

            saveFile(fileType.toLowerCase(), docRef, newFileName)

           

        }

        // Otherwise, find the lowest number of the file name that does not exist, starting at the index of the file name's digits

        else {

            // Create the needName switch

            needName = 1

            // Get the file number

            fileNumber = Number(lastFive);

            while (needName) {

                // Increment the file number

                fileNumber = fileNumber + 1;

           

                // Make the file number 4 digits with zeros

                switch ((fileNumber.toString()).length) {

                    case 1:

                        newFileName = allButFive + '_0000' + fileNumber.toString();

                        break;

                    case 2:

                        newFileName = allButFive + '_000' + fileNumber.toString();

                        break;

                    case 3:

                        newFileName = allButFive + '_00' + fileNumber.toString();

                        break;

                    case 4:

                        newFileName = allButFive + '_0' + fileNumber.toString();

                        break;

                    case 5:

                        newFileName = allButFive + "_" + fileNumber.toString();

                        break;

                }

                // Do the saving

                needName = saveFile(fileType.toLowerCase(), docRef, newFileName)    

            }

        }

    }

    catch(e)

    {

        // An error occured; restore dialog mode and display the error

        app.displayDialogs = originalDialogMode;

        throw e;

    }

}

else

{

    alert( "There's no file open! GET TO WORK!!!!");

}

// Function for saving the file

function saveFile(fileType, docRef, newFileName) {

    needName = 1;

    switch (fileType.toLowerCase()) {

        case 'psd': // Photoshop file

            thisSameFile = docRef.path + '/' + newFileName + '.psd';

            saveFile = new File(thisSameFile);

            if (saveFile.exists) {

                break;

            }

            else {

                saveOptions = new PhotoshopSaveOptions();

                saveOpt.embedColorProfile = true;

                saveOpt.layers = true;

                app.activeDocument.saveAs(saveFile,saveOpt,true,Extension.LOWERCASE);

                needName = 0;

                app.activeDocument.close(saveOpt.DONOTSAVECHANGES);

                open(saveFile);

                break;

            }

        case 'jpg': // JPEG file

            thisSameFile = docRef.path + '/' + newFileName + '.jpg';

            saveFile = new File(thisSameFile);

            if (saveFile.exists) {

                break;

            }

            else {

                saveOpt = new JPEGSaveOptions();

                saveOpt.embedColorProfile = true;

                saveOpt.formatOptions = FormatOptions.STANDARDBASELINE;

                saveOpt.matte = MatteType.NONE;

                saveOpt.quality = 12;

                app.activeDocument.saveAs(saveFile,saveOpt,true,Extension.LOWERCASE);

                needName = 0;

                app.activeDocument.close(saveOpt.DONOTSAVECHANGES);

                open(saveFile);

                break;

            }

        case 'png': // PNG file

            thisSameFile = docRef.path + '/' + newFileName + '.png';

            saveFile = new File(thisSameFile);

            if (saveFile.exists) {

                break;

            }

            else {

                saveOpt = new PNGSaveOptions();

                saveOpt.compression = 0;

                app.activeDocument.saveAs(saveFile,saveOpt,true,Extension.LOWERCASE);

                needName = 0;

                app.activeDocument.close(saveOpt.DONOTSAVECHANGES);

                open(saveFile);

                break;

            }

        case 'tif': // TIFF file

            thisSameFile = docRef.path + '/' + newFileName + '.tif';

            saveFile = new File(thisSameFile);

            if (saveFile.exists) {

                break;

            }

            else {

                saveOpt = new TiffSaveOptions();

                saveOpt.embedColorProfile = true;

                saveOpt.layers = false;

                saveOpt.transparency = true;

                app.activeDocument.saveAs(saveFile,saveOpt,true,Extension.LOWERCASE);

                needName = 0;

                app.activeDocument.close(saveOpt.DONOTSAVECHANGES);

                open(saveFile);

                break;

            }

    }

    return needName;

}

TOPICS
Actions and scripting

Views

502

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 18, 2017 Mar 18, 2017

The problem seems to be is that you seems to be using "saveFile" as both a Function Name and a Variable. I changed the function name to saveFileF

// Script by Vince Petaccio of PetRok Studios. | www.PetRokStudios.com

// Ensure a document is open

if ( documents.length > 0 )

{

    // Change dialog settings

  var originalDialogMode = app.displayDialogs;

  app.displayDialogs = DialogModes.ERROR;

    try

    {

    // Get the document

        var docRef = activeDocument;

        // Get the path for the document

  

...

Votes

Translate

Translate
Adobe
Community Expert ,
Mar 17, 2017 Mar 17, 2017

Copy link to clipboard

Copied

What crashes? Photoshop?  Have you tried running the script in Photoshop not using ExtendScript ToolKit run it from Photoshop menu File>Scripts>Script File. Was a crash report generated? Running your script that way on my system the Photoshop crash is in extendscript.dll.... Even when the save option code is commented out it crashes so it not the statement in red causing the crash...

// Script by Vince Petaccio of PetRok Studios. | www.PetRokStudios.com

// Ensure a document is open

if ( documents.length > 0 )

{

    // Change dialog settings

  var originalDialogMode = app.displayDialogs;

  app.displayDialogs = DialogModes.ERROR;

    try

    {

    // Get the document

        var docRef = activeDocument;

        // Get the path for the document

        var filePath = docRef.path + '/';

 

        // Get the filename of the document

        var fileNameNoExtension = docRef.name;

      

        // Split the filename at the dots

        fileNameNoExtension = fileNameNoExtension.split( "." );

      

        // If there's a dot, remove everything after the last dot

        if ( fileNameNoExtension.length > 1 ) {

            // Get the file type

            fileType = fileNameNoExtension[fileNameNoExtension.length-1];

          

            // Decrement the filename object by removing the last element (the stuff after the last .)

          fileNameNoExtension.length--;

          

            // Reconnect everything BEFORE the first .

            fileNameNoExtension = fileNameNoExtension.join(".");

        }

        // Get the last five characters of the filename

        lastFive = fileNameNoExtension.slice(-5);

        allButFive = fileNameNoExtension.slice(0,-6);

        // Check whether the last five characters are digits

        newDoc = isNaN(parseInt(lastFive))

        // If this is a new document, then add _00000 to the file name

        if (newDoc) {

            // Make the new file name

            newFileName = fileNameNoExtension + '_00000';

          

            // Do the saving

            saveFile(fileType.toLowerCase(), docRef, newFileName)

          

        }

        // Otherwise, find the lowest number of the file name that does not exist, starting at the index of the file name's digits

        else {

            // Create the needName switch

            needName = 1

            // Get the file number

            fileNumber = Number(lastFive);

            while (needName) {

                // Increment the file number

                fileNumber = fileNumber + 1;

          

                // Make the file number 4 digits with zeros

                switch ((fileNumber.toString()).length) {

                    case 1:

                        newFileName = allButFive + '_0000' + fileNumber.toString();

                        break;

                    case 2:

                        newFileName = allButFive + '_000' + fileNumber.toString();

                        break;

                    case 3:

                        newFileName = allButFive + '_00' + fileNumber.toString();

                        break;

                    case 4:

                        newFileName = allButFive + '_0' + fileNumber.toString();

                        break;

                    case 5:

                        newFileName = allButFive + "_" + fileNumber.toString();

                        break;

                }

                // Do the saving

                needName = saveFile(fileType.toLowerCase(), docRef, newFileName)   

            }

        }

    }

    catch(e)

    {

        // An error occured; restore dialog mode and display the error

        app.displayDialogs = originalDialogMode;

        throw e;

    }

}

else

{

    alert( "There's no file open! GET TO WORK!!!!");

}

// Function for saving the file

function saveFile(fileType, docRef, newFileName) {

    needName = 1;

    switch (fileType.toLowerCase()) {

        case 'psd': // Photoshop file

            thisSameFile = docRef.path + '/' + newFileName + '.psd';

            saveFile = new File(thisSameFile);

            if (saveFile.exists) {

                break;

            }

            else {

  /*

                saveOptions = new PhotoshopSaveOptions();

                saveOpt.embedColorProfile = true;

                saveOpt.layers = true;

                app.activeDocument.saveAs(saveFile,saveOpt,true,Extension.LOWERCASE);

                needName = 0;

                app.activeDocument.close(saveOpt.DONOTSAVECHANGES);

                open(saveFile);

  */

                break;

            }

        case 'jpg': // JPEG file

            thisSameFile = docRef.path + '/' + newFileName + '.jpg';

            saveFile = new File(thisSameFile);

            if (saveFile.exists) {

                break;

            }

            else {

  /*

                saveOpt = new JPEGSaveOptions();

                saveOpt.embedColorProfile = true;

                saveOpt.formatOptions = FormatOptions.STANDARDBASELINE;

                saveOpt.matte = MatteType.NONE;

                saveOpt.quality = 12;

                app.activeDocument.saveAs(saveFile,saveOpt,true,Extension.LOWERCASE);

                needName = 0;

                app.activeDocument.close(saveOpt.DONOTSAVECHANGES);

                open(saveFile);

  */

                break;

            }

        case 'png': // PNG file

            thisSameFile = docRef.path + '/' + newFileName + '.png';

            saveFile = new File(thisSameFile);

            if (saveFile.exists) {

                break;

            }

            else {

  /*

                saveOpt = new PNGSaveOptions();

                saveOpt.compression = 0;

                app.activeDocument.saveAs(saveFile,saveOpt,true,Extension.LOWERCASE);

                needName = 0;

                app.activeDocument.close(saveOpt.DONOTSAVECHANGES);

                open(saveFile);

  */

                break;

            }

        case 'tif': // TIFF file

            thisSameFile = docRef.path + '/' + newFileName + '.tif';

            saveFile = new File(thisSameFile);

            if (saveFile.exists) {

                break;

            }

            else {

  /*

                saveOpt = new TiffSaveOptions();

                saveOpt.embedColorProfile = true;

                saveOpt.layers = false;

                saveOpt.transparency = true;

                app.activeDocument.saveAs(saveFile,saveOpt,true,Extension.LOWERCASE);

                needName = 0;

                app.activeDocument.close(saveOpt.DONOTSAVECHANGES);

                open(saveFile);

  */

                break;

            }

    }

    return needName;

}

Capture.jpg

JJMack

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 Beginner ,
Mar 18, 2017 Mar 18, 2017

Copy link to clipboard

Copied

Hi JJMack, thanks so much for taking the time to help me out!

The crash does indeed occur when I run the script this way, in both CC 2015 and in CC 2017.

The mode of failure is a "Photoshop has stopped working..." error, depicted below.

Curious that you've encountered the error with that line commented out- when I run the code in debug mode, it runs perfectly until it crashes as those lines. This is what lead me to the conclusion that the problem was related to them. Though I wouldn't expect the code to run with those lines commented out, since they define variables used later in the code.

crash.jpg

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 18, 2017 Mar 18, 2017

Copy link to clipboard

Copied

The problem seems to be is that you seems to be using "saveFile" as both a Function Name and a Variable. I changed the function name to saveFileF

// Script by Vince Petaccio of PetRok Studios. | www.PetRokStudios.com

// Ensure a document is open

if ( documents.length > 0 )

{

    // Change dialog settings

  var originalDialogMode = app.displayDialogs;

  app.displayDialogs = DialogModes.ERROR;

    try

    {

    // Get the document

        var docRef = activeDocument;

        // Get the path for the document

        var filePath = docRef.path + '/';

 

        // Get the filename of the document

        var fileNameNoExtension = docRef.name;

      

        // Split the filename at the dots

        fileNameNoExtension = fileNameNoExtension.split( "." );

      

        // If there's a dot, remove everything after the last dot

        if ( fileNameNoExtension.length > 1 ) {

            // Get the file type

            fileType = fileNameNoExtension[fileNameNoExtension.length-1];

          

            // Decrement the filename object by removing the last element (the stuff after the last .)

          fileNameNoExtension.length--;

          

            // Reconnect everything BEFORE the first .

            fileNameNoExtension = fileNameNoExtension.join(".");

        }

        // Get the last five characters of the filename

        lastFive = fileNameNoExtension.slice(-5);

        allButFive = fileNameNoExtension.slice(0,-6);

        // Check whether the last five characters are digits

        newDoc = isNaN(parseInt(lastFive))

        // If this is a new document, then add _00000 to the file name

        if (newDoc) {

            // Make the new file name

            newFileName = fileNameNoExtension + '_00000';

          

            // Do the saving

            saveFileF(fileType.toLowerCase(), docRef, newFileName)

          

        }

        // Otherwise, find the lowest number of the file name that does not exist, starting at the index of the file name's digits

        else {

            // Create the needName switch

            needName = 1

            // Get the file number

            fileNumber = Number(lastFive);

            while (needName) {

                // Increment the file number

                fileNumber = fileNumber + 1;

          

                // Make the file number 4 digits with zeros

                switch ((fileNumber.toString()).length) {

                    case 1:

                        newFileName = allButFive + '_0000' + fileNumber.toString();

                        break;

                    case 2:

                        newFileName = allButFive + '_000' + fileNumber.toString();

                        break;

                    case 3:

                        newFileName = allButFive + '_00' + fileNumber.toString();

                        break;

                    case 4:

                        newFileName = allButFive + '_0' + fileNumber.toString();

                        break;

                    case 5:

                        newFileName = allButFive + "_" + fileNumber.toString();

                        break;

                }

                // Do the saving

                needName = saveFileF(fileType.toLowerCase(), docRef, newFileName)   

            }

        }

    }

    catch(e)

    {

        // An error occured; restore dialog mode and display the error

        app.displayDialogs = originalDialogMode;

        throw e;

    }

}

else

{

    alert( "There's no file open! GET TO WORK!!!!");

}

// Function for saving the file

function saveFileF(fileType, docRef, newFileName) {

    needName = 1;

    switch (fileType.toLowerCase()) {

        case 'psd': // Photoshop file

            thisSameFile = docRef.path + '/' + newFileName + '.psd';

            saveFile = new File(thisSameFile);

            if (saveFile.exists) {

                break;

            }

            else {

                saveOptions = new PhotoshopSaveOptions();

                saveOpt.embedColorProfile = true;

                saveOpt.layers = true;

                app.activeDocument.saveAs(saveFile,saveOpt,true,Extension.LOWERCASE);

                needName = 0;

                app.activeDocument.close(saveOpt.DONOTSAVECHANGES);

                open(saveFile);

                break;

            }

        case 'jpg': // JPEG file

            thisSameFile = docRef.path + '/' + newFileName + '.jpg';

            saveFile = new File(thisSameFile);

            if (saveFile.exists) {

                break;

            }

            else {

                saveOpt = new JPEGSaveOptions();

                saveOpt.embedColorProfile = true;

                saveOpt.formatOptions = FormatOptions.STANDARDBASELINE;

                saveOpt.matte = MatteType.NONE;

                saveOpt.quality = 12;

                app.activeDocument.saveAs(saveFile,saveOpt,true,Extension.LOWERCASE);

                needName = 0;

                app.activeDocument.close(saveOpt.DONOTSAVECHANGES);

                open(saveFile);

                break;

            }

        case 'png': // PNG file

            thisSameFile = docRef.path + '/' + newFileName + '.png';

            saveFile = new File(thisSameFile);

            if (saveFile.exists) {

                break;

            }

            else {

                saveOpt = new PNGSaveOptions();

                saveOpt.compression = 0;

                app.activeDocument.saveAs(saveFile,saveOpt,true,Extension.LOWERCASE);

                needName = 0;

                app.activeDocument.close(saveOpt.DONOTSAVECHANGES);

                open(saveFile);

                break;

            }

        case 'tif': // TIFF file

            thisSameFile = docRef.path + '/' + newFileName + '.tif';

            saveFile = new File(thisSameFile);

            if (saveFile.exists) {

                break;

            }

            else {

                saveOpt = new TiffSaveOptions();

                saveOpt.embedColorProfile = true;

                saveOpt.layers = false;

                saveOpt.transparency = true;

                app.activeDocument.saveAs(saveFile,saveOpt,true,Extension.LOWERCASE);

                needName = 0;

                app.activeDocument.close(saveOpt.DONOTSAVECHANGES);

                open(saveFile);

                break;

            }

    }

    return needName;

}

JJMack

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 Beginner ,
Mar 20, 2017 Mar 20, 2017

Copy link to clipboard

Copied

LATEST

WOW do I feel silly. Amateur hour in here, eh?

Thanks so much for your help! Everything is working as expected now with that change.

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