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

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

New Here ,
Aug 28, 2020 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);
            }
        }
    }
};

 

Views

181

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 , Aug 28, 2020 Aug 28, 2020

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.embedC
...

Votes

Translate

Translate
Adobe
Community Expert ,
Aug 28, 2020 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

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
New Here ,
Aug 28, 2020 Aug 28, 2020

Copy link to clipboard

Copied

LATEST

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

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