Highlighted

wrapping a function in a function and passing arguments.

Explorer ,
Mar 12, 2020

Copy link to clipboard

Copied

I have a section of code that works but I need it to be run multiple times, seems like a perfect time for a function. But, I can't seem to wrap it in another function and pass arguments to the saveWeb3 function. I'd like to pass arguments for bookName, bookNameSuffix(_WEB), bookCode, bookcode suffix(_Web.jpg). Any advice?

thanks for your time and help.

 

 

bt.target = "photoshop";

function saveWeb3(){
var saveFileTif = new File("~/Desktop/"+bookName+"_WEB/"+bookCode+"_WEB.jpg");

var pdfOpenOpts = new PDFOpenOptions;
pdfOpenOpts.antiAlias = true;
pdfOpenOpts.bitsPerChannel = BitsPerChannelType.EIGHT;
pdfOpenOpts.cropPage = CropToType.TRIMBOX;
pdfOpenOpts.mode = OpenDocumentMode.RGB;
pdfOpenOpts.resolution = 72;
pdfOpenOpts.suppressWarnings = true;
pdfOpenOpts.usePageNumber  = true;

var theFile = (File("~/desktop/TempFile_01.pdf"));
app.open(theFile, pdfOpenOpts);

var currentWidth = app.activeDocument.width;
var currentHeight = null;
var currentResolution = 72 ;
app.activeDocument.changeMode(ChangeMode.RGB);

if(app.activeDocument.width.value>app.activeDocument.height.value){//Landscape
  app.activeDocument.resizeImage(
                 //currentHeight,
                  new UnitValue(800, "px"),
                  currentHeight,
                  currentResolution,
                  ResampleMethod.BILINEAR);
	}else
  {//Portrait
    app.activeDocument.resizeImage(
                   //currentHeight,
                    new UnitValue(600, "px"),
                    currentHeight,
                    currentResolution,
                    ResampleMethod.BILINEAR);
                  }

function SaveJPEG(saveFileTif, jpegQuality){
jpgSaveOptions = new JPEGSaveOptions();
jpgSaveOptions.embedColorProfile = true;
jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
jpgSaveOptions.matte = MatteType.NONE;
jpgSaveOptions.quality = jpegQuality;
activeDocument.saveAs(saveFileTif, jpgSaveOptions, true,Extension.LOWERCASE);
}
SaveJPEG(saveFileTif, 8);

//Close open Pshop doc
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}

//convert the function into a string
psScript = "saveWeb =" + saveWeb3.toString() + "\rsaveWeb();";
//After converting the function into a string you need to replace
//the word bookCode in that string with the actual content of that variable.
//replace the word bookCode in the string with the actual content of the variable
psScript = psScript.replace(/bookName/g, "\"" + bookName + "\"").replace(/bookCode/g, "\"" + bookCode + "\"");

bt.body = psScript;

bt.onResult = function(resObj)
    {
        var myResult = resObj.body;
    }

bt.send(100);

  

Views

93

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

wrapping a function in a function and passing arguments.

Explorer ,
Mar 12, 2020

Copy link to clipboard

Copied

I have a section of code that works but I need it to be run multiple times, seems like a perfect time for a function. But, I can't seem to wrap it in another function and pass arguments to the saveWeb3 function. I'd like to pass arguments for bookName, bookNameSuffix(_WEB), bookCode, bookcode suffix(_Web.jpg). Any advice?

thanks for your time and help.

 

 

bt.target = "photoshop";

function saveWeb3(){
var saveFileTif = new File("~/Desktop/"+bookName+"_WEB/"+bookCode+"_WEB.jpg");

var pdfOpenOpts = new PDFOpenOptions;
pdfOpenOpts.antiAlias = true;
pdfOpenOpts.bitsPerChannel = BitsPerChannelType.EIGHT;
pdfOpenOpts.cropPage = CropToType.TRIMBOX;
pdfOpenOpts.mode = OpenDocumentMode.RGB;
pdfOpenOpts.resolution = 72;
pdfOpenOpts.suppressWarnings = true;
pdfOpenOpts.usePageNumber  = true;

var theFile = (File("~/desktop/TempFile_01.pdf"));
app.open(theFile, pdfOpenOpts);

var currentWidth = app.activeDocument.width;
var currentHeight = null;
var currentResolution = 72 ;
app.activeDocument.changeMode(ChangeMode.RGB);

if(app.activeDocument.width.value>app.activeDocument.height.value){//Landscape
  app.activeDocument.resizeImage(
                 //currentHeight,
                  new UnitValue(800, "px"),
                  currentHeight,
                  currentResolution,
                  ResampleMethod.BILINEAR);
	}else
  {//Portrait
    app.activeDocument.resizeImage(
                   //currentHeight,
                    new UnitValue(600, "px"),
                    currentHeight,
                    currentResolution,
                    ResampleMethod.BILINEAR);
                  }

function SaveJPEG(saveFileTif, jpegQuality){
jpgSaveOptions = new JPEGSaveOptions();
jpgSaveOptions.embedColorProfile = true;
jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
jpgSaveOptions.matte = MatteType.NONE;
jpgSaveOptions.quality = jpegQuality;
activeDocument.saveAs(saveFileTif, jpgSaveOptions, true,Extension.LOWERCASE);
}
SaveJPEG(saveFileTif, 8);

//Close open Pshop doc
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}

//convert the function into a string
psScript = "saveWeb =" + saveWeb3.toString() + "\rsaveWeb();";
//After converting the function into a string you need to replace
//the word bookCode in that string with the actual content of that variable.
//replace the word bookCode in the string with the actual content of the variable
psScript = psScript.replace(/bookName/g, "\"" + bookName + "\"").replace(/bookCode/g, "\"" + bookCode + "\"");

bt.body = psScript;

bt.onResult = function(resObj)
    {
        var myResult = resObj.body;
    }

bt.send(100);

  

Views

94

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

Have something to add?

Join the conversation