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

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

You probarbly figured it out but if you won't add 5th line to the function:

preferences.autoUpdateOpenDocuments = false

Your script will be even much slower than you didn't use closing layer sets at all.

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

Mhh I don't think it's slower because I do this only at the very end of the script?

Anyway doing autoUpdate = false won't close the layersets for me.

I did realize it does something I don't want: which is to turn off all the groups instead of leaving them as they were. (some on, some off depending on how the artist left them).

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 20, 2018 Jan 20, 2018

Yes, your artists will suffer extra delays when autoUpdateOpenDocuments is on, because when once you turn it on it'll be working till you untick it. And while that option serves not only layer folders swich, working of script would be remarkably longer. You are right, seting autoUpdateOpenDocuments back to false right after setting it true doesn't update documents (more about it read under new chunks of code). Closing only layerSets opened by script is not hard thing. It needs only to make a loop over current expanded layerSets at the start of script to make array of them and with end of script close only other ones. You can try these 2 new (Windows) codes and later we'll see how to change your curent script it could be ran twice for the moment, only to make layerSets refreshment, the way that won't trigger any other part of code in you script:

#target photoshop

function e(v1, v2, v3) {

     eval(pre + ' = ' + v1), $.setenv('u', v2), eval('bat.' + v3 + '()')

}

if (bat = File((fN = $.fileName).slice(0, -3) + 'bat'), !(eval(pre =

'preferences.autoUpdateOpenDocuments')) && !$.getenv('u')) {

     (function(){

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

          fle.open('r'); var rep = fle.read().replace(/\x018BIMp/g,

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

          bat.open('w'), bat.write('"' + Folder.startup.fsName +

          '\\photoshop.exe" ' + '"' + File(fN).fsName + '"')

          bat.close(), $.setenv('u', 1), bat.execute()

     })()

}

else if($.getenv('u') == 1) e(1, 2, 'execute') else e(0, '', 'remove')

#target photoshop

if (vbs = File((fN = $.fileName).slice(0, -3) + 'vbs'),

!(eval(p = 'preferences.autoUpdateOpenDocuments'))) {

     (function(){

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

          var rep = fle.read().replace(/\x018BIMp/g, '\x028BIMp'); fle.open('w')

          fle.write(rep), fle.close(), documents.add(1,1,1).close(), eval(p + ' = 1')

          txt = 'CreateObject("Shell.Application").ShellExecute """'

          txt += Folder.startup.fsName + '\\Photoshop.exe""", "'

          txt += File(fN).fsName + '"', vbs.open('w')

          vbs.write(txt), vbs.close(), vbs.execute()

     })()

}

else eval(p + ' = ' + !p), vbs.remove()

Some facts about second code:

  • self invoked function had to be used to prevent ExtendScript ToolKit from freezing
  • var was used as 1st point works only for saving binary to .txt, while not when .psd binary is beeing overritten
  • preferences.autoUpdateOpenDocuments beeing set to true and then to false during single script session result false and vice versa so false, then true gives true. It's why after changing preferences, script had to initiate itself again to apply just changed preferences, and then change them back, as they would slow down process
  • immediatelly closed new created document had to help in the process described in previous point otherwise script had to initiate itself one more time like it's in first version of script where command prompt was used
  • vbscript had to be used to hide system window that flashed for a moment using command prompt, also because there is no option in cmd to silently invoke its commands
  • second and actually main reason of using immediatelly closed new document was to refresh current document state that binary changes made to file, otherwise if not current document (window) switch they couldn't be visually updated

btw, don't post next time your whole script here, better fix your pastebin, as your code may be changed few times more

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 20, 2018 Jan 20, 2018

I'm a bit out of my depth here as I don't understand most of the things you wrote (this my first javascript script btw), but I understand you're opening the .psd file to make some changes to the structure of the document.

I interpreted that I should run the 1st code at the start of the algorithm and the 2nd at the end. This gave me several popups of my UI after having exported the sprites along as some other weird result where props in different groups would be saved out in the same .png. (2props in 1 image). And since then, rendering in that same psd gives me mostly 2 props per image (instead of 1).

Could you provide some more information as to how I should use the code you've given.

Here's the code I've got right now (there's none of the new code you provided in that code):
Photoshop: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 20, 2018 Jan 20, 2018

You don't have to use any of these two codes. They were only samples I wrote meantime. First with command prompt what caused little worse visual side effect with command windows showing for the moment of .bat execution and second where I used vbscript to hide system windows. So they are separeate versions that should not be combained in one code. I noticed now as well that at end of your script, process there is popping alert "Finished exporting files", If not that one little thing you needed to use one of two new codes nested somehow to your script.

It's because when script finish its job it needs for just very short moment any other window (not only Photoshop one) focus. If that won't happen you won't see that layerSets of your main document in layer panel got updated. You had manually for example leave to desktop to come back to Photoshop they were updated, so closed, like before script start.

Fortunately you added alert("Finished exporting files") at end of your script what creates additional window that's needed layerSets of your document got properly displayed (so not only changed in binary data).

If you edited now your script and slashed alert("Finished exporting files"), saved script and run it again you would see that your layerSets are not closed. Well to make them closed without that alert I had somehow to use my new snippet to insert in your script, that's all.

Ps. 2 props (what is it full meaning for?) in one .png image are saved even with the full code you recently posted here with a .psd file you uploaded for my testings. So it's not a thing of new code, but something you had to do earlier.

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 20, 2018 Jan 20, 2018

I think I understand, well then I'll wait some more, it's not a problem. I'll definitely leave in the "Finished exporting files" because I need something to show the user the script worked.

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 20, 2018 Jan 20, 2018

In the second line of your current script put:

if (vbs = File((fN = $.fileName).slice(0, -3) + 'vbs'),

!(eval(p = 'preferences.autoUpdateOpenDocuments'))) {

In the last line...:

}

else eval(p + ' = ' + !p), vbs.remove()

And before alert("Finished exporting files"); (would be best you deleted old commented lines around this alert😞

(function(){

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

     var rep = fle.read().replace(/\x018BIMp/g, '\x028BIMp'); fle.open('w')

     fle.write(rep), fle.close(), documents.add(1,1,1).close(), eval(p + ' = 1')

     txt = 'CreateObject("Shell.Application").ShellExecute """'

     txt += Folder.startup.fsName + '\\Photoshop.exe""", "'

     txt += File(fN).fsName + '"', vbs.open('w')

     vbs.write(txt), vbs.close(), vbs.execute()

})()

Then, just comment your finishing alert and save script; open some layerSets of your document and run a script. You will see that they are closed again with end of script. If you didn't use these 3 parts of code they were opened still. When you happy with result edit your code again and uncomment finish alert (you want it inform of completed job) and save script.

Ps. when I run your script with .psd you uploaded to forum it creates .png's with 2 props, and probably reason is somewhere on your side code. There are sometimes made visible two layer sets at same time, while you do want always only one was?

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 20, 2018 Jan 20, 2018

I updated the pastebin with what you said (maybe I messed up somewhere?):
Photoshop:PngGroupSave - Pastebin.com

I don't see the artLayers closing right now.

PS: yes nevermind that problem, it only happens on this .psd not sure why.

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 20, 2018 Jan 20, 2018

You didn't put function before finishing alert (that last part of code I divided into 3 parts in my last post).

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 20, 2018 Jan 20, 2018

Ah okay I thought you meant delete the comments only. I added it there and it works now, that's really cool thanks

EDIT: Upon trying a few times, it suddenly stopped working (collapsing groups down)
I updated the pastebin to the new code again: Photoshop:PngGroupSave - Pastebin.com

Next step once this works consitently is to save in an array the state of the groups so that we can have the same groups opened/closed as before launching the script, is that the plan?

RE-EDIT: Nevermind this time it works again. I'm not sure why but it doesn't seem to always trigger closing down the groups. I'll tell you when I know more about this, you don't have to mind it for now, maybe it's because I automatically back up files on a server, I'll have to check.

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 20, 2018 Jan 20, 2018

Perhaps there's needed some delay in a function I wrote. For me it worked, but I didn't have material to test to use it many times in a row with different documents that contain plenty of groups. Yes, saving state from beginning  to retrieve it at end.

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 20, 2018 Jan 20, 2018

I'm using the same .psd to test as the one I sent you. I closed the server down and I tested some more that wasn't the reason.
This happens from time to time (doesn't close the groups), if you want to test you just need to save out the .psd I sent you a few times: I use a path like that in the UI and "C/Users/Feast/Desktop/Test" just press enter or export.
I'd say 1 times out of 4 it doesn't close the groups.

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 20, 2018 Jan 20, 2018

You're welcome to uncomment your finishing alert of course. That was only to show you how it work. With alert maybe it will be more stable. If so, even then better keep it commented, so you can notice why there's no folders folding each time.

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 20, 2018 Jan 20, 2018

With alert, it never closes the groups down. Without alert: 3/4 closes the groups down.

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 20, 2018 Jan 20, 2018

I tried it 30 times with many variations, with and without alert, bound to action and selected from scripts list of Ps file menu. With mine script and your from pastebin, closing (tabbing) panels and having them opened, hitting enter or clicing button. It didn't fail even once.

In the path I changed only Feast to Marek. Original ExampleLayoutPS.psd document after downloading had 3 closed groups with one layer each and background. All groups (not layers) and background were set to invisible. I tried each time opening all layer sets and setting them visible, saving this state and trying few times again, then saving file after each time script was finished and again it didn't fail even once. (I kept always .psd file on my desktop).

If it didn't close layer sets and you change window focus to other one or use 'win' & 'd' keys (to go to desktop) and then use it again to be back to Photoshop, are they closed, or still opened? If opened and you save document, to close and open are they opened or closed? And when you check Edit / Preferences / General is first 'Auto-Update Open Documents' box free ?

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 20, 2018 Jan 20, 2018

Yes I think that's weird, you have the same document and you were right to change Feast to Marek I made a quick video to show you the issue: Not closing groups - only if no alert - YouTube

The Auto Update in preferences is set to false.
If the groups are first opened or closed doesn't really matter I think I tried all kinds of variations. They sometimes (if no alert) and always (if alert) stay all opened (because the script open them up).

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 20, 2018 Jan 20, 2018

So you have two options to make it work with alert, putting it between these lines:

txt += File(fN).fsName + '"', vbs.open('w')

alert("Finished exporting files")

vbs.write(txt), vbs.close(), vbs.execute()

what won't refresh layer sets state till you click okey button when alert dialogs pops up.

Or adding alert at end of last line, so:

else eval(p + ' = ' + !p), vbs.remove(), alert("Finished exporting files")

that will display your layer sets already closed, before okey button is clicked.

As to situation without alert when you say it not always closes layer sets, something on your machine must happen too late due to prior event. To test it make script of second code from post dated on 2018-01-21 01:43. Open "our" document, open its sets and run it many times with different variations. From ESTK and PS, with ESTK closed too. For me it's working. If for you will too, then the reason we'll be looking in your whole script...

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 20, 2018 Jan 20, 2018

Ok both options work with alerts. I'm not certain which post you mean since I don't live in the same timeline as you do.
I still have the problem popping up where the groups are not closed down, still in that same ratio 1/4 = not closed.

Do you still believe there is no way to force update a .psd document instead of using autoUpdate = true?
Because if it were in my script, then why would you not have the problem? Aren't you using my script?

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 20, 2018 Jan 20, 2018

I used your code, it's almost same like mine, so yours edited of lines I came today with. That post you ask is the first made 'today' by me after two days of silence from my side You have to use second code there is, as I posted two version there.

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 20, 2018 Jan 20, 2018

Not sure where to put that code so I put it up top and had to comment out a few things. This code, it creates a 2nd window when the script is done after clicking export (the same window as when I start the script).

Updated code: Photoshop: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 20, 2018 Jan 20, 2018

No, you don't have to use it with your script. It's completed code already. Just save it as it is and run in Photoshop with .psd.

Use it many times with 'ExampleLayoutPS.psd' where you have layer sets opened to see will it not close them at some try...

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 20, 2018 Jan 20, 2018

Yes sometimes it doesn't work. It works less often than when using the png save script.

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 20, 2018 Jan 20, 2018

For now that's everything, at least we know where is problem. Later I see what I can do with. For now add alert at end of:

txt += File(fN).fsName + '"', vbs.open('w'), alert()

And test script again to see will it fail. If not then change alert to $.sleep(500). Run few times and decrease it about 100 units, then 10 when you are at 100, to find bottom acceptable limit. MAybe it's not where is problem, but do tests, so we will know should we dig more or it's where change should be done.

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 20, 2018 Jan 20, 2018

Alright, I'll go to bed now then. I'll do these tests tomorrow. Thanks for the help!

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