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

UI: Remember last path entered

Participant ,
Jan 08, 2018 Jan 08, 2018

I have a simple UI for my artists that does some modifications to his files then saves it to the folder of his choice.
However it gets quickly tedious to re-enter the path every time he uses the script.

I would like to remember the path he previously entered or use the path that Photoshop uses as a default.
How can that be done?


Here's the script if someone is interested, it allows you to save out your groups as PNG's with possible additional blurred versions:
Photoshop PNG Blur Save - Pastebin.com

Message was edited by: Jeremy Vansnick

TOPICS
Actions and scripting
11.9K
Translate
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

LEGEND , Jan 08, 2018 Jan 08, 2018

Change beginning of your script to:

PTH = ""; displayDialogs = DialogModes.NO

var Path = $.getenv('pth') || PTH || "d/";

// for saving files

var doc = app.activeDocument;

And in the middle where you last time insterted new part, edit that to:

dlg.show();

$.setenv('pth', Path.slice(1))

if (PTH != ge = $.getenv('pth')) {

     (fle = File($.fileName)).open('r'), jsxFile = fle.read()

     .replace(/"(.*)"(?=;)/, '"' + ge + '"'), fle.close()

     fle.open('w'), fle.write(jsxFile), fle.close()

}

Additionally what

...
Translate
Adobe
LEGEND ,
Jan 08, 2018 Jan 08, 2018

There are 4 mistakes in code. Actually one four-fold, and honestly two double. Script can't save (at least for me) as far as there is AUTOMATIC set for ResampleMethod. Correctly there should be ResampleMethod.BICUBIC. in all four places

To answer your question:

  • change var Path = "D:\\GameDevelopment"; to var Path = $.getenv('pth') || "d/";
  • and right after dlg.show(); insert another line: $.setenv('pth', Path.slice(1))
Translate
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 ,
Jan 08, 2018 Jan 08, 2018

That's exactly it!

I don't know why but my artist keeps having the popup about trimming with the options despite using BICUBIC like you said.
On my Photoshop, it doesn't ask for that, there is no popup showing during the script. However he has a popup for every time my script calls trimming.

Is this something you're familiar with? Thanks a lot for the help already, it's working great except for the popup.

Translate
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
LEGEND ,
Jan 08, 2018 Jan 08, 2018

Put on top of the script: displayDialogs = DialogModes.NO

Translate
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 ,
Jan 08, 2018 Jan 08, 2018

Thanks

I realized that upon quitting Photoshop the path is lost, is there nothing I can do about that?

Translate
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
LEGEND ,
Jan 08, 2018 Jan 08, 2018

Change beginning of your script to:

PTH = ""; displayDialogs = DialogModes.NO

var Path = $.getenv('pth') || PTH || "d/";

// for saving files

var doc = app.activeDocument;

And in the middle where you last time insterted new part, edit that to:

dlg.show();

$.setenv('pth', Path.slice(1))

if (PTH != ge = $.getenv('pth')) {

     (fle = File($.fileName)).open('r'), jsxFile = fle.read()

     .replace(/"(.*)"(?=;)/, '"' + ge + '"'), fle.close()

     fle.open('w'), fle.write(jsxFile), fle.close()

}

Additionally what is important change two your trim methods from doc.trim(TrimType.TRANSPARENT, true, true, true, true); to just doc.trim(). This way your images will be trimmed whether they are on transparent or one coloured or else coloured background. If you won't do this then your script will stop working (of course in specific situation).

As to beginning and middle of script now when PTH is false, and environment pth is undefined it will display 'd\', however it will happen only once, at first time of running script. During using Ps it will always refer to environmental path. Every time it is changed your .jsx script is going to update itself of new path (first line of code). If you choose few times in a row the same path, then PTH variable will keep last different path, if you change it to other in dialog and click 'Export' button it's going to be changed also in your script, but will be used only at every next Photoshop session.

Translate
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 ,
Jan 08, 2018 Jan 08, 2018

Great that works perfectly!

I reuploaded the script to pastebin for anyone interested:
PngGroupSave - Pastebin.com

I realized a little something: some of my path's first characters seem to disappear over time, I think it might due to Path.slice(1)?
in:

  1. $.setenv('pth', Path.slice(1))
Translate
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
LEGEND ,
Jan 08, 2018 Jan 08, 2018

Yes it has to be cut each time, otherwise with every next script performance you'd be getting slashes concatenation. If that occured (even with one extra slash) it would prevent chance to save your files, as there must be only one preceding slash at the beginning of path. Actually there always is one. You can not see it but script is adding it each time for savings (that was existing functionallity).

Translate
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 ,
Jan 08, 2018 Jan 08, 2018

I don't know how exactly it happens but more characters than the slash is being cut out, I think replacing the string's first '/' by an empty character might alleviate that error, I'll try to do that tomorrow because I never used javascript before this script ^^.

Oh actually I think it might have happened when I didn't allow the script's modification upon it being written into, so it's actually not a problem nevermind that.

Thanks a lot Kukurykus, you're the man!

Translate
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
LEGEND ,
Jan 08, 2018 Jan 08, 2018

Yes, if the script has to overtitten itself then Adobe folder must be unlocked. Path = "/" + Path; stays in 240 (of updated code) line. I saw it before but didn't want to change it as had to check wont it wrongly influence on other parts of script it might be connected to. If there is problem with slicing (as I didn't test it) check first administrative rights for yout Ps folder, and if that is fine then try to modify script. Perhaps if you have posted some of paths you are getting error with and those you do not I could compare them and logically find a reason of this occurance.

Translate
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 ,
Jan 10, 2018 Jan 10, 2018

It wasn't that after all, it writes the path to the script properly, but it's when clicking on "cancel" that it deletes a character from the path.


So I added the path reading only when clicking on "Export" and it's now fine. Thanks!

Translate
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
LEGEND ,
Jan 10, 2018 Jan 10, 2018

Yes, that had to be reason as I didn't try it with cancel button. Btw update your pastebin of changed code

Translate
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 ,
Jan 10, 2018 Jan 10, 2018

Here's the updated code, everything should be fine now: PngGroupSave - Pastebin.com

Translate
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 ,
Jan 16, 2018 Jan 16, 2018

Hey Kukurykus, I don't know if you got some more time to help me out some more but I realized I had 2 big problems left with this script.

I'd like my artist's props to stay at a consistent amount of blur despite him using different resolutions for the props he creates.

1) how can I blur images with the same intensity/percentage even though they have a different resolution?
(for example           512x512 and 512x930    or...        512x512 and 1024x890).

2) I tried with different kinds of image resize but it won't properly discover which pixels are fully transparent and which aren't so it ends up cutting some pixels of the image and there is an abrupt cut in the image. I tested Trim alone and it works fine, it's during the resizeImage operation that some pixels are cut away.

Here's an example of that:

https://i.imgur.com/Aof9agN.png

Here's the folder with the sharp and blurred versions of that example if you want to see in more detail:
SpriteCutoff.rar - Google Drive

The script pastebin link: PngGroupSave - Pastebin.com

Translate
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
LEGEND ,
Jan 16, 2018 Jan 16, 2018

If your base image is 512*512 with any px/in resololution to use 1px of Guassian Blur for it apply this pattern for other images:

Math.blur = function(v1, v2) {with(this) {with(activeDocument) {activeLayer.applyGaussianBlur(v1 * ((sqrt = sqrt(width * height / pow(v2, 2))) + sqrt) / (pi = PI) * pi / 2)}}}

You call it for ex. by Math.blur(5, 512)​, where 5 is Radius in Pixels, while 512 is side length of square document. If you want to refer to rectangle, then you have to obtain square of width multiplied by height. So if your prefered dimension is 512*512, you're sure that 1 px Gaussian Blur radius applied fits your goal, then all other documents with bigger or smaller dimension when you use Math.blur(5, 512) function will get appriopate "intensity/percentage". So when there is document 512*512 it'll get 5 Radius value, so exactly that you would manually enter into a Gaussain Blur box. If that will be 6000 * 4500 document then 5 Radius value will be changed by the function to 50.7 value, the same you had to enter manually into blur blank to get exactly same intensity you do entereing 5 for 512 * 512. And if that was 400 * 600 document then 5 value will be changed to 4.8 as 240000 is smaller than 262144. For those who want the pattern other way wrote than used in JavaScript code it's:

radius * (2 * (sqaure of (width * height) / (average length of prefered document side) to power of 2)) / PI * (PI / 2)

so basicly the same what (as I just found it calculating it again to make it simpler 😞

radius * (sqaure of (width * height) / (average length of prefered document side) to power of 2)

finally you can change a code from a top of this post to shortened version that gives the same result:

Math.blur = function(v1, v2) {with(Math) {with(activeDocument) {activeLayer.applyGaussianBlur(v1 * sqrt(width * height / pow(v2, 2)))}}}

Translate
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 ,
Jan 17, 2018 Jan 17, 2018

Waw that must've taken time to figure out, many thanks!
It works almost perfectly, I think I can notice small differences between resolutions. But it's definitely good enough as it is for what I need.

  1. Math.blur = function(v1, v2) {with(Math) {with(activeDocument) {activeLayer.applyGaussianBlur(v1 * sqrt(width * height / pow(v2, 2)))}}} 


There's one thing I don't understand in the last function: when you divide by pow(v2,2).

I used v2 = doc.width but I don't understand why we're using doc.width and not doc.height for example, is that the small imperfection in the calculation?

By the way the problem #2 has been fixed somehow.

Translate
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
LEGEND ,
Jan 17, 2018 Jan 17, 2018

Well I explained it in my post, but probably you missed it. I don't use neither width nor height, however I do when they are equal to each other. If not then I am looking for average of them. So when document is 512*930 or 1024*890 I take square of multiplication result, so accordingly Math.sqrt(476160) and Math.sqrt(911360) what gives 690 and 954,7 average side length.

Maybe because you used width instead of average for width and height you got not perfect result ?

If you just want to check what Radius value you'll get for specific dimension (without applying Guasian blur yet) use this:

Math.blur = function(v1, v2) {with(Math) {with(activeDocument) {return v1 * sqrt(width * height / pow(v2, 2))}}}

Call it with for ex. Math.blur(5, 512) // alerted when you do it via Photoshop (so not ExtendScript ToolKit with targeted Ps)

If you created 3 documents: 512*512, 512*930 and 1024*890, you'll get 5, 6.7 and 9.3 pixels

I had no time for Item 2, but I understand you didn't do anything else than using my function to solve item 1; and that affected positively for trimming as well?

Ps. I'm really weak mathematician, but the algorithm for your task came to my head in no time, I created it in just 5 minutes (and couldn't believe I was able to find it out so fast, especially that I realised I don't understand working of that pattern. It's why originally that was longer than at the end. As to PI number I used it because there was used 'Radius' word in gausain blur dialog, so I thought that has to have something to do with...

Translate
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 ,
Jan 17, 2018 Jan 17, 2018

Ok I see, yes the problem 2 has been fixed by trimming twice, I'm not sure why exactly that works but I'll upload the script when it's good for sure. No I read your post carefully but now I understand what you meant, thanks. I'll post again when the script is finalized.

Translate
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
LEGEND ,
Jan 17, 2018 Jan 17, 2018

If you still had problems with trimming try this:

with(Math) with(activeDocument)

displayDialogs = DialogModes.NO,

preferences.rulerUnits = Units.PIXELS,

gaussianBlur = function(v1, v2) {

     return (lyr = activeLayer).applyGaussianBlur

     (b = v1 * sqrt(width * height / pow(v2, 2))), b

},

executeAction(stringIDToTypeID('newPlacedLayer')), gaussianBlur(21, 512),

d = function(v) {return v + (v / 100 * b)}, resizeCanvas(d(width), d(height)),

trim(TrimType.TRANSPARENT), lyr.rasterize(RasterizeType.ENTIRELAYER)

It's going to make smart object of a layer and apply a blur. Then the canvas of a document will be resized of a width and height percent you're getting using convereted guassian blur radius value for current document dimension. This way you're sure dimension won't be increased unnecessarily too much as well as not enough what could cut off edges of blured shape during next step that is trimming. At the end a layer is getting resterized, however there is no difference it was resterized in first instance and then trimmed as normal layer. (21 value is random, but huge enough to show it works with intense blur)

If smart object wasn't used then blured shape of layer couldn't keep information of its enlarged size, so after resizing blured edges of artistic shape would sharply finish at layer borders from resizing.

Translate
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 ,
Jan 17, 2018 Jan 17, 2018

I had problems with trimming because of smart objects I think. I ended up using merge() to merge the group into a single layer. But looking back at it, I think that wasn't the reason. The big problem surely was that I wasn't using the correct Blurred Trimmed Resolution to resize the image.

But merging down groups has fixed the problem along with proper calculations, if you're curious you can look at the script. I made it some more readable: https://pastebin.com/edit/2ZNxsLgU

Btw, if you're a weak mathematician and you figure that out in 5mins, I'm the worst mathematician alive ;D.
I have one little qwirk, when running the script it unfolds all the folders and I have to close them up again. Using a savestate to go back doesn't save that apparently. What can I do to close the folders back down or better yet keep the hierarchy exactly as it was before running the script?
https://i.imgur.com/joFffwi.png  (these kind of folders get opened up during the script)

And I've got another question, it's unrelated but it's become a real curiosity to me: I wonder what motivates you to help people on this forum?

Thanks again for helping me out, it's really great. I changed the formula you gave me to find the proper blur amount because it didn't give me the blur I wanted, through testing I found that this is what I needed (I only found this thanks to you though).

var v2= DocTrimmedWidth * DocTrimmedHeight;

Math.blur = function(v1, v2) {with(Math) {with(activeDocument) {return v1 / sqrt(5000* 5000 / v2)}}}

result.applyGaussianBlur(Math.blur(blurStrength, v2));

Translate
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
LEGEND ,
Jan 18, 2018 Jan 18, 2018

1) The function you modified can be simplified to:

function blr(v1, v2) {with(Math) return v1 / sqrt(pow(5000, 2) / v2)}

as with this construction you don't use with statement with activeDocument.

Anyway I compared both, yours and mine where for example there are 512*490 document with 5px gaussian blur radius.

mine:

Math.blur = function(v1, v2) {with(this) with(activeDocument) return v1 * sqrt(width * height / pow(v2, 2))} Math.blur(5, 512)

yours:

function blr(v1, v2) {with(Math) return v1 / sqrt(pow(5000, 2) / v2)} blr(5, (aD = activeDocument).width * aD.height)

Mine gives 4.89139 while yours 0.50087. If you used 5120, then your result was 0.48913. So 10 times smaller than mine.

But if you used 512 instead of 5120 then, both yours and mine function gave the same result, so 4.89139.

Of course if I did appriopate changes in my function ie. changed base square of width * height, so 512 to 500 multiplied by 10, by Math.blur(5, 5000), it was 0.50087.

That you use 5000, not 5120 is probably because there is not big difference between these numbers, so you just 'rounded' it.

Now you can use:

function blr(v1, v2) {with(Math) return v1 / sqrt(pow(5120, 2) / v2)}

(aD = activeDocument).activeLayer.applyGaussianBlur(blr(blurStrength, aD.width * aD.height))

or still:

Math.blur = function(v1, v2) {with(this) with(activeDocument) activeLayer.applyGaussianBlur(v1 * sqrt(width * height / pow(v2, 2)))}

Math.blur(blurStrength, 5000)     //     where you see you didn't need new function, since you may simply replace 512 with 5000

The only thing I dont understand why you used 10 times bigger base value? Now each time you enter blurStrength Radius value to your function you can't get enough strong blur with little 'handy' amount if you didn't concatenated 0 at the end?

2) I do all for myself, there is no motivation in my doings, especially to help anybody. I don't try to be of assistance, that's not my point. So you simply got luck I was willing to do another interesting to me programming crossword. As you see I don't think much of it what it is for. There's no reason to be here other than having fun of purely egoistic satisfaction that gives finding solutions for questions I couldn't realise they exist. So far I never was aware I can do things looking how to:

  1. save path to the script I'm running at the same moment
  2. use blur radius proportionally to document dimension
  3. collapse layer sets by a method there's no exist

that was like another game levels to achieve, where each next one was more tricky.

3) The site from link you gave wants me to log in to see the content, despite previous ones you shared were available for guests too. Maybe just upload it other way, or else update old one? For now I can not look over your new corrected code.

4) Use this function right after saving, but before closing document, so with new document opening, folders will be closed:

;(function(){

     (fle = activeDocument.fullName).encoding = 'binary',

     fle.open('r'), rep = fle.read().replace(/\x018BIMp/ig,

     '\x028BIMp'), fle.open('w'), fle.write(rep), fle.close()

})()

Do you have some bell on your computer ringing to you I just posted?

Translate
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 ,
Jan 18, 2018 Jan 18, 2018

PTH = "C/Users/Feast/Desktop/Test"; displayDialogs = DialogModes.NO 

var Path = $.getenv('pth') || PTH || "d/"; 

 

// for saving files 

var doc = app.activeDocument; 

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

var Prefix = "";

var Suffix = "";

var BlurAmount = [2,4,7];

var DivideScaleBlurAmount = [1.25,1.5,2];

var AddBlur = false;

var AddVeg = false;

var AddSP = false;

var AllowNonGroupLayers = false;

var Res = 512;

var DocTrimmedWidth = 0;

var DocTrimmedHeight = 0;

var BlurredTrimmedWidth = 0;

var BlurredTrimmedHeight = 0;

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

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

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

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

'Path:');

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

ScriptUI.Alignment.TOP]

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

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

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

'Reso:');

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

Res, {name: "Res"});

// Resolution UI

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

dlg.msgPnl.orientation = "row";

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

'Prefix:');

dlg.titleEt = dlg.msgPnl.add('edittext', [0,0,100,20],

Prefix, {name: "Prefix"});

// Add a checkbox to control the buttons that dismiss an alert box

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

'SP', {name: "SP"});

dlg.hasBtnsCb.value = true;

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

'B', {name: "Blur"});

dlg.hasBtnsCb.value = true;

// Add a checkbox to control the buttons that dismiss an alert box

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

'VG', {name: "Veg"});

dlg.hasBtnsCb.value = false;

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

'Render Ungrouped', {name: "allowLayers"});

dlg.hasBtnsCb.value = false;

dlg.msgPnl.btn = dlg.msgPnl.add('button', undefined, 'Export', {name:"export"});   

dlg.msgPnl.btn.onClick = Algorithm;   

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

function key_handle(e) 

    { 

    switch (e.keyIdentifier) 

        { 

        case "Enter": 

//~             dlg.btnPnl.btn.notify(); 

            Algorithm();

            break; 

        } 

    }   

   

dlg.msgEt.active = true; 

   

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

   

dlg.show(); 

// Algorithm must be called when clicking on Export button

function Algorithm()

    GetUIValues();   

   

    SetSuffix();

   

     $.setenv('pth', Path.slice(1)) 

if (PTH != ge = $.getenv('pth')) { 

     (fle = File($.fileName)).open('r'), jsxFile = fle.read() 

     .replace(/"(.*)"(?=;)/, '"' + ge + '"'), fle.close() 

     fle.open('w'), fle.write(jsxFile), fle.close() 

   

    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

        };

        doc.layerSets.visible = false;

    };

    // set  all non-groups layers off

    var oldNonGroupLayers = new Array();

    if (AllowNonGroupLayers == false)

    {

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

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

            oldNonGroupLayers.push(1);

        };

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

            oldNonGroupLayers.push(0); // not visible

        };

        doc.layers.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 (AddBlur)

            {

                for (var j = 1; j <= BlurAmount.length; j++)

                {

                    var currentBlur = BlurAmount[j-1];

                    CreateBlurredVersion(doc.layerSets, currentBlur, j, DivideScaleBlurAmount[j-1]);

                }

             }

            // 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;

    }

    // set whichever non-group layers were originally on visible again

    if (AllowNonGroupLayers == false)

    {

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

        if (oldNonGroupLayers == 1)

            doc.layers.visible = true;

        }

    }

      ;(function(){ 

     (fle = activeDocument.fullName).encoding = 'binary', 

     fle.open('r'), rep = fle.read().replace(/\x018BIMp/ig, 

     '\x028BIMp'), fle.open('w'), fle.write(rep), fle.close() 

})() 

     alert("Finished exporting files");

}

function GetUIValues() {

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

   

    AddBlur = dlg.findElement("Blur").value;

    AddVeg = dlg.findElement("Veg").value;

    AddSP = dlg.findElement("SP").value;

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

    AllowNonGroupLayers = dlg.findElement("allowLayers").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);

   

    // we need to find the current width and height of the picture now

    // this way we can find the right amount of pixels the picture should be blurred

    // to keep the blur even across all the pictures.

    // ========== set units to pixels ==========//

    var oldPrefs = app.preferences.rulerUnits;

    app.preferences.rulerUnits = Units.PIXELS;

    DocTrimmedWidth = doc.width;

    DocTrimmedHeight = doc.height;

   

    // resize algorithm

    if (DocTrimmedHeight > DocTrimmedWidth) {

        doc.resizeImage(null, UnitValue(Res, "px"),72, ResampleMethod.BICUBIC, 100);

    }

    else {

        doc.resizeImage(UnitValue(Res, "px"), null, 72, ResampleMethod.BICUBIC, 100);

    }

    app.preferences.rulerUnits = oldPrefs;

    // we save the ouput

    if (AddBlur)

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

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

    SavePNG(saveFile);

   

    doc.activeHistoryState = savedState;

}

function CreateBlurredVersion(group, blurStrength, number, scale)

{

    var doc = app.activeDocument;

    var savedState = doc.activeHistoryState;

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

   

    var theName = group.name;

    // we need to find the current width and height of the picture now

    // this way we can find the right amount of pixels the picture should be blurred

    // to keep the blur even across all the pictures.

    // ========== set units to pixels ==========//

    var oldPrefs = app.preferences.rulerUnits;

    app.preferences.rulerUnits = Units.PIXELS;

   

    doc.activeHistoryState = savedState;

   

     // transform document to smart object

//~      var result = createSmartObject(group);

     var result = group.merge();

    

     // gaussian blur

     //result.applyGaussianBlur(blurStrength, blurStrength);

//~      (aD = activeDocument).activeLayer.applyGaussianBlur(blurStrength * (sqrt = Math.sqrt(aD.width * aD.height / Math.pow(512, 2))) + sqrt / Math.PI / aD.resolution * 72)

//~     Math.blur = function(v1, v2) {with(Math) {with(activeDocument) {activeLayer.applyGaussianBlur(v1 * sqrt(width * height / pow(v2, 2)))}}} 

//~     Math.blur = function(v1, v2) {with(Math) {with(activeDocument) {activeLayer.applyGaussianBlur(v1 * sqrt(width * height / pow(v2, 2)))}}} 

var v2= DocTrimmedWidth * DocTrimmedHeight;

Math.blur = function(v1, v2) {with(Math) {with(activeDocument) {return v1 / sqrt(512* 512 / v2)}}}

//~ if (number==3) alert("blurAmount of "+ theName + ": " + Math.blur(blurStrength, v2));

result.applyGaussianBlur(Math.blur(blurStrength, v2));

   

//~     var v1= Math.sqrt (doc.width * doc.height);

//~     var blurFactor = v1 /  v2;

//~     alert("DocTrimmedWidth: " + DocTrimmedWidth);

//~     alert("DocTrimmedWidth: " + DocTrimmedHeight);

//~     alert("doc.width: " + doc.width);

//~     alert("doc.height: " + doc.height);

//~     alert("blurFactor: " + blurFactor);

//~     result.applyGaussianBlur(blurStrength * blurFactor);

//~     var blurCalc = blurStrength * Math.sqrt(DocTrimmedWidth * DocTrimmedHeight / Math.pow((DocTrimmedWidth + DocTrimmedHeight)/2, 2));

//~     result.applyGaussianBlur(blurCalc);

//~  Math.blur(blurStrength, (doc.width + doc.height)/2);

   

   

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

    BlurredTrimmedWidth = doc.width;

    BlurredTrimmedHeight = doc.height;

    // resize algorithm

    if (BlurredTrimmedHeight > BlurredTrimmedWidth) {

        doc.resizeImage(null, UnitValue(Res/ scale, "px"), 72, ResampleMethod.BICUBIC, 100);

    }

    else {

        doc.resizeImage(UnitValue(Res/ scale, "px"), null, 72, ResampleMethod.BICUBIC, 100);

    }

   

    app.preferences.rulerUnits = oldPrefs;

   

    // 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

function SetSuffix()

{

    if (AddSP && AddVeg)

        Suffix = "-VG-SP";

        else if (AddSP)

        Suffix = "-SP";

        else if (AddVeg)

        Suffix = "-VG";

}

I'm having problems with my pastebin account, but no matter: here's the code.

I find it hard to believe it's completely selfish but it's true this kind of behaviour often goes unrewarded. There are other easier ways to satisfy your curiosity but as long as it's not too selfless that's good... Also, I think you enjoy the praise a little don't you? Hopefully, this doesn't bother you.

5000 was just a random number I could've picked anything else it just has to be unchanged, at the time I was working in a 5k document. I switched it back to 512 as you suggested.

I get mails when you answer my questions, that's when I come running.

Mhh I tried this code to close the groups but it didn't work. Maybe I misunderstood something?

Translate
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
LEGEND ,
Jan 18, 2018 Jan 18, 2018

What's good solving others tasks I can get knowledge that later I use rapidly, that is without loosing time figuring out things I need only to do somehitng else, more important at the time.

I don't know when those folders are created in your document. But it works for sure when you create document, add some groups, save it as .psd and run that snippet. Then after reopening document folders will be collapsed. I did not focus on your script but maybe you save your document as .png, while that snippet is meant for .psd. But if so, then you do not need it, as reopened .png's don't contain folders, do they? Describe process your artists do, because I have no idea where is difference. And attach file they start with to use a script for, so I see how it works. Also some psd you'd like it worked with but doesn't.

Translate
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 ,
Jan 18, 2018 Jan 18, 2018

My artist creates some groups of layers with art in it. Then the script renders out in png the groups that are visible 1 by 1 as a new png.

This means the groups are being opened up (unfolded) during the script. Ah you say it's after reopening the document, is it possible to do so without having to reopen the document? My script does not close the document.
It is saved a .psd yes. It's a big .psd with lots of groups and many props, that's why all the folders/art layers being opened slows down the process.

Downloading from DiskStation

Translate
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
LEGEND ,
Jan 18, 2018 Jan 18, 2018

That should work with that additional line (for me does):

;(function(){ 

     (fle = activeDocument.fullName).encoding = 'binary', 

     fle.open('r'), rep = fle.read().replace(/\x018BIMp/ig, 

     '\x028BIMp'), fle.open('w'), fle.write(rep), fle.close() 

     preferences.autoUpdateOpenDocuments = true

})()

Now only binary data got changed, the state of layer sets get updated, that means they get collapsed without reopening

Translate
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