Highlighted

Use a document location instead of a dialog to open a file in a script

New Here ,
Aug 28, 2020

Copy link to clipboard

Copied

Hello, I'm trying to use a smartObject replace script found on the web, but I don't want to use the Dialog to select the files I would like to use an hardcoded location in the script.

 

I tried everything and have no idea how to use the file as a hardcoded location

 

if (app.documents.length > 0) {
    var myDocument = app.activeDocument;
    var theName = myDocument.name.match(/(.*)\.[^\.]+$/)[1];
    var thePath = myDocument.path;
    var theLayer = myDocument.activeLayer;

    // JPG Options;
    jpgSaveOptions = new JPEGSaveOptions();  
    jpgSaveOptions.embedColorProfile = true;  
    jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;  
    jpgSaveOptions.matte = MatteType.NONE;  
    jpgSaveOptions.quality = 12;   

    // Check if layer is SmartObject;
    if (theLayer.kind != "LayerKind.SMARTOBJECT") {
        alert("selected layer is not a smart object")
    } else {
        // Select Files;
        if ($.os.search(/windows/i) != -1) {
//          var theFiles = File.openDialog("please select files", "*.psd;*.tif;*.jpg;*.png;*.pdf", true)
            var theFiles = File("directory/smartObjectFile.jpg")
        } else {
            var theFiles = File.openDialog("please select files", getFiles, true)
        };
        if (theFiles) {
            for (var m = 0; m < theFiles.length; m++) {
                // Replace SmartObject
                theLayer = replaceContents(theFiles[m], theLayer);
                // Save JPG
                myDocument.saveAs((new File("NewMadeFile.jpg")), jpgSaveOptions, true,Extension.LOWERCASE);
            }
        }
    }
};

 

Adobe Community Professional
Correct answer by Manan Joshi | Adobe Community Professional

Your code is not complete so i could not test it, the definition for replaceContents method is missing. Try the following, change the path of the file that you want to replace with, in the code.

if (app.documents.length > 0) 
{
    var myDocument = app.activeDocument;
    var theName = myDocument.name.match(/(.*)\.[^\.]+$/)[1];
    var thePath = myDocument.path;
    var theLayer = myDocument.activeLayer;

    // JPG Options;
    jpgSaveOptions = new JPEGSaveOptions();  
    jpgSaveOptions.embedColorProfile = true;  
    jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;  
    jpgSaveOptions.matte = MatteType.NONE;  
    jpgSaveOptions.quality = 12;   

    // Check if layer is SmartObject;
    if (theLayer.kind != "LayerKind.SMARTOBJECT")
        alert("selected layer is not a smart object")
    else 
	{
        var file = File("/Users/manan/Downloads/383822.pdf")
        theLayer = replaceContents(file, theLayer);
        myDocument.saveAs((new File("NewMadeFile.jpg")), jpgSaveOptions, true,Extension.LOWERCASE);
     }
};

-Manan

TOPICS
How to, Problem or error

Views

40

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

Use a document location instead of a dialog to open a file in a script

New Here ,
Aug 28, 2020

Copy link to clipboard

Copied

Hello, I'm trying to use a smartObject replace script found on the web, but I don't want to use the Dialog to select the files I would like to use an hardcoded location in the script.

 

I tried everything and have no idea how to use the file as a hardcoded location

 

if (app.documents.length > 0) {
    var myDocument = app.activeDocument;
    var theName = myDocument.name.match(/(.*)\.[^\.]+$/)[1];
    var thePath = myDocument.path;
    var theLayer = myDocument.activeLayer;

    // JPG Options;
    jpgSaveOptions = new JPEGSaveOptions();  
    jpgSaveOptions.embedColorProfile = true;  
    jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;  
    jpgSaveOptions.matte = MatteType.NONE;  
    jpgSaveOptions.quality = 12;   

    // Check if layer is SmartObject;
    if (theLayer.kind != "LayerKind.SMARTOBJECT") {
        alert("selected layer is not a smart object")
    } else {
        // Select Files;
        if ($.os.search(/windows/i) != -1) {
//          var theFiles = File.openDialog("please select files", "*.psd;*.tif;*.jpg;*.png;*.pdf", true)
            var theFiles = File("directory/smartObjectFile.jpg")
        } else {
            var theFiles = File.openDialog("please select files", getFiles, true)
        };
        if (theFiles) {
            for (var m = 0; m < theFiles.length; m++) {
                // Replace SmartObject
                theLayer = replaceContents(theFiles[m], theLayer);
                // Save JPG
                myDocument.saveAs((new File("NewMadeFile.jpg")), jpgSaveOptions, true,Extension.LOWERCASE);
            }
        }
    }
};

 

Adobe Community Professional
Correct answer by Manan Joshi | Adobe Community Professional

Your code is not complete so i could not test it, the definition for replaceContents method is missing. Try the following, change the path of the file that you want to replace with, in the code.

if (app.documents.length > 0) 
{
    var myDocument = app.activeDocument;
    var theName = myDocument.name.match(/(.*)\.[^\.]+$/)[1];
    var thePath = myDocument.path;
    var theLayer = myDocument.activeLayer;

    // JPG Options;
    jpgSaveOptions = new JPEGSaveOptions();  
    jpgSaveOptions.embedColorProfile = true;  
    jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;  
    jpgSaveOptions.matte = MatteType.NONE;  
    jpgSaveOptions.quality = 12;   

    // Check if layer is SmartObject;
    if (theLayer.kind != "LayerKind.SMARTOBJECT")
        alert("selected layer is not a smart object")
    else 
	{
        var file = File("/Users/manan/Downloads/383822.pdf")
        theLayer = replaceContents(file, theLayer);
        myDocument.saveAs((new File("NewMadeFile.jpg")), jpgSaveOptions, true,Extension.LOWERCASE);
     }
};

-Manan

TOPICS
How to, Problem or error

Views

41

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
Aug 28, 2020 0
Adobe Community Professional ,
Aug 28, 2020

Copy link to clipboard

Copied

Your code is not complete so i could not test it, the definition for replaceContents method is missing. Try the following, change the path of the file that you want to replace with, in the code.

if (app.documents.length > 0) 
{
    var myDocument = app.activeDocument;
    var theName = myDocument.name.match(/(.*)\.[^\.]+$/)[1];
    var thePath = myDocument.path;
    var theLayer = myDocument.activeLayer;

    // JPG Options;
    jpgSaveOptions = new JPEGSaveOptions();  
    jpgSaveOptions.embedColorProfile = true;  
    jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;  
    jpgSaveOptions.matte = MatteType.NONE;  
    jpgSaveOptions.quality = 12;   

    // Check if layer is SmartObject;
    if (theLayer.kind != "LayerKind.SMARTOBJECT")
        alert("selected layer is not a smart object")
    else 
	{
        var file = File("/Users/manan/Downloads/383822.pdf")
        theLayer = replaceContents(file, theLayer);
        myDocument.saveAs((new File("NewMadeFile.jpg")), jpgSaveOptions, true,Extension.LOWERCASE);
     }
};

-Manan

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
Reply
Loading...
Aug 28, 2020 1
New Here ,
Aug 28, 2020

Copy link to clipboard

Copied

Great, that worked and I see what I was doing wrong, I kept expecting an Array of files

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
Reply
Loading...
Aug 28, 2020 0