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

Activate a button when pressing Enter [Photoshop]

Participant ,
Oct 20, 2017 Oct 20, 2017

Copy link to clipboard

Copied

Hello!
I would like that when the user presses the Enter key, it presses the button called "Export" in my script.
Or if I could just know when the player presses Enter to call a function that would work too.

Here's the script I'm trying to use it for (javascript), it exports PNG's from all the groups in your active document. If someone's interested:

// for saving files

var doc = app.activeDocument;

var Path = doc.path;  // set the .png next to .psd

//Path = "~/Desktop/test"; // personal folder

Path = "D:\\GameDevelopment";

var Name = doc.name.replace(/\.[^\.]+$/, '');

var Prefix = "PROP_";

var Suffix = "";

var doc = app.activeDocument;

// AUTOMATIC OPTIONS <= CHANGE HERE

var x = 512;

var y = 512;

var ActivateBlur = true;

var BlurStrength = 10;

var saveFile = File(Path + "/" + Name + Suffix + ".jpg");

// AUTO OPTIONS

var dlg = new Window('dialog', 'PNG Batch Save');

dlg.onEnterKey = function() { Algorithm();};

var G1 = dlg.add('group', undefined); 

dlg.msgSt = G1.add('statictext', undefined,

'Path:');

dlg.addEventListener('enterKey', Algorithm);

dlg.msgSt.alignment = [ScriptUI.Alignment.LEFT,

ScriptUI.Alignment.TOP]

dlg.msgEt = G1.add('edittext', [0,0,400,20],

Path, {name: "path",multiline:false,noecho:false,readonly:false});

// Resolution UI

dlg.msgPnl = dlg.add('panel', undefined, 'Resolution');

dlg.msgPnl.orientation = "row";

dlg.titleSt = dlg.msgPnl.add('statictext', undefined,

'X:');

dlg.titleEt = dlg.msgPnl.add('edittext', undefined,

x, {name: "x"});

dlg.titleSt = dlg.msgPnl.add('statictext', undefined,

'Y:');

dlg.titleEt = dlg.msgPnl.add('edittext', undefined,

y, {name: "y"});

// Blur UI

dlg.msgPnl = dlg.add('panel', undefined, 'Blurred');

dlg.msgPnl.orientation = "row";

dlg.hasBtnsCb = dlg.msgPnl.add('checkbox', undefined,

'+ Blurred Version', {name:"isBlur"});

dlg.msgSt = dlg.msgPnl.add('edittext', undefined,

BlurStrength, {name:"blurStrength"});

dlg.hasBtnsCb.value = ActivateBlur;

ActivateBlur = dlg.hasBtnsCb.value;

// Buttons UI

dlg.btnPnl = dlg.add('panel', undefined, 'Save');

dlg.btnPnl.buildBtn = dlg.btnPnl.add('button', undefined, 'Export');

dlg.btnPnl.buildBtn.addEventListener('click', Algorithm);

dlg.btnPnl.onClick = Algorithm;

dlg.btnPnl.buildBtn = dlg.btnPnl.add('button', undefined, 'Cancel');

dlg.show();

// Algorithm must be called when clicking on Export button

function Algorithm()

{

    GetUIValues();   

   

    dlg.close();

   

    var doc = app.activeDocument;

    // need an array to know which grp was on or off

    var oldLayers = new Array();

    for (var i = 0; i < doc.layerSets.length; i++) {

        if (doc.layerSets.visible == true) {

            oldLayers.push(1);

        };

        if (doc.layerSets.visible == false) {

            oldLayers.push(0); // not visible

        };

    };

    // set all groups off

    for (var i = 0; i < doc.layerSets.length; i++) {

        doc.layerSets.visible = false;

    }

    for (var i = 0; i < doc.layerSets.length; i++) {

        // if the layer used to be visible

        if (oldLayers == 1) {

            //alert("Found a visible grp!");

            // we set it visible

            doc.layerSets.visible = true;

            TrimResizeAndSave(doc.layerSets);

           

            if (ActivateBlur) CreateBlurredVersion(doc.layerSets);

            // we set it invisible again

            doc.layerSets.visible = false;

        };

    }

    // set whichever groups were originally on visible again

    for (var i = 0; i < doc.layerSets.length; i++) {

        if (oldLayers == 1)

            doc.layerSets.visible = true;

    }

}

function GetUIValues() {

    //alert(dlg.findElement("x").text);

    x = dlg.findElement("x").text;

    //alert(dlg.findElement("y").text);

    y = dlg.findElement("y").text;

    //alert(dlg.findElement("blurStrength").text);

    BlurStrength = dlg.findElement("blurStrength").text;

    //alert(dlg.findElement("isBlur").value);

    ActivateBlur = dlg.findElement("isBlur").value;

    Path = dlg.findElement("path").text;

   

    var c = /\\/g;

    if (Path.match(c)) {

//~         alert("found \\");

Path = Path.replace(c, "/");

    };

    c = ':';

    if (Path.match(c )) {

//~         alert("found :");

        Path = Path.replace(c , '');

    };

    Path = "/" + Path;

}

function SaveJPEG(saveFile, jpegQuality) {

    jpgSaveOptions = new JPEGSaveOptions();

    jpgSaveOptions.embedColorProfile = true;

    jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;

    jpgSaveOptions.matte = MatteType.NONE;

    jpgSaveOptions.quality = jpegQuality;

    activeDocument.saveAs(saveFile, jpgSaveOptions, true, Extension.LOWERCASE);

}

function SavePNG(saveFile) {

    pngSaveOptions = new PNGSaveOptions(9, false);

    activeDocument.saveAs(saveFile, pngSaveOptions, true, Extension.LOWERCASE);

}

function TrimResizeAndSave(group) {

    var doc = app.activeDocument;

    var savedState = doc.activeHistoryState;

    // trim

    doc.trim(TrimType.TRANSPARENT, true, true, true, true);

    // resize algorithm

    if (doc.height > doc.width) {

        doc.resizeImage(null, UnitValue(y, "px"), null, ResampleMethod.AUTOMATIC);

    }

    else {

        doc.resizeImage(UnitValue(x, "px"), null, null, ResampleMethod.AUTOMATIC);

    }

    // we save the ouput

    saveFile = File(Path + "/" + Prefix + group.name + Suffix + ".png");

    SavePNG(saveFile);

   

    doc.activeHistoryState = savedState;

}

function CreateBlurredVersion(group)

{

    var doc = app.activeDocument;

    var savedState = doc.activeHistoryState;

    // trim

    doc.trim(TrimType.TRANSPARENT, true, true, true, true);

    // resize algorithm

    if (doc.height > doc.width) {

        doc.resizeImage(null, UnitValue(y, "px"), null, ResampleMethod.AUTOMATIC);

    }

    else {

        doc.resizeImage(UnitValue(x, "px"), null, null, ResampleMethod.AUTOMATIC);

    }

   

    // blurred output

    var blurredPath = File(Path + "/" + "BLURRED_" + Prefix + group.name + Suffix + ".png");

   

    // transform document to smart object

     var result = createSmartObject(group);

     result.applyGaussianBlur(BlurStrength, BlurStrength);

   

    // save

    SavePNG(blurredPath);

   

    doc.activeHistoryState = savedState;

}

// create smartobject from specified layer or layerSet/group

function createSmartObject(layer)

{

   var doc = app.activeDocument;

   var layer = layer != undefined ? layer : doc.activeLayer;

   if(doc.activeLayer != layer) doc.activeLayer = layer;

   try

   {

      var idnewPlacedLayer = stringIDToTypeID( "newPlacedLayer" );

      executeAction( idnewPlacedLayer, undefined, DialogModes.NO );

      return doc.activeLayer;

   }

   catch(e)

   {

      return undefined;

   }

}// JavaScript Document

TOPICS
Actions and scripting

Views

836

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

People's Champ , Oct 20, 2017 Oct 20, 2017

you need to activate some element in dialog.

insert one more line like this

dlg.msgEt.active = true;

Votes

Translate

Translate
Adobe
People's Champ ,
Oct 20, 2017 Oct 20, 2017

Copy link to clipboard

Copied

before line with dlg.show() insert such code

function key_handle(e)

    {

    switch (e.keyIdentifier)

        {

        case "Enter":

            dlg.btnPnl.buildBtn.notify();

            break;

        }

    }

dlg.addEventListener ("keydown", key_handle, false); 

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
Participant ,
Oct 20, 2017 Oct 20, 2017

Copy link to clipboard

Copied

Thanks for helping me out r-bin!

I did as you suggested but it doesn't work, am I doing it wrong?

// Buttons UI 

dlg.btnPnl = dlg.add('panel', undefined, 'Save'); 

dlg.btnPnl.buildBtn = dlg.btnPnl.add('button', undefined, 'Export'); 

dlg.btnPnl.buildBtn.addEventListener('click', Algorithm); 

dlg.btnPnl.onClick = Algorithm; 

dlg.btnPnl.buildBtn = dlg.btnPnl.add('button', undefined, 'Cancel'); 

 

function key_handle(e)

    {

    switch (e.keyIdentifier)

        {

        case "Enter":

            dlg.btnPnl.buildBtn.notify();

            break;

        }

    } 

 

dlg.addEventListener ("keydown", key_handle, false);

 

dlg.show(); 

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
People's Champ ,
Oct 20, 2017 Oct 20, 2017

Copy link to clipboard

Copied

you need to activate some element in dialog.

insert one more line like this

dlg.msgEt.active = true;

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
Participant ,
Oct 20, 2017 Oct 20, 2017

Copy link to clipboard

Copied

LATEST

Thanks that did it! Have a good day and thanks again!

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