Need to clear Photoshop's memory

Discussion of actual or possible Photoshop Scripting Bugs, Anomalies and Documentation Errors

Moderators: Tom, Kukurykus

cargaralo
Posts: 1
Joined: Wed Aug 03, 2016 11:11 am

Need to clear Photoshop's memory

Post by cargaralo »

Hey all,
I am trying to automate the cropping of thousands of photos. I create an action (which includes closing the image) but when Photoshop starts the Batch process, it sucks up all the memory.
I'm looking at the Resource Monitor on Win8 and it's the Working Set that seems to being going up the most, followed by Shareable and Commit. The Private memory isn't rising too much though. I have 7.6GB of free memory available but Photoshop will use it all after a few hundred photos.
Is there something I can add to the Action to free up this memory? Purge doesn't do anything, and I can only free the memory after restarting Photoshop.
User avatar
Kukurykus
Posts: 528
Joined: Mon Jul 25, 2016 12:36 pm

Re: Need to clear Photoshop's memory

Post by Kukurykus »

Your post inspired me to write new script. I don't know you know how to use them, because you mentioned only about actions, so here few instructins:

- I tested it only in CS2, CS5.1
- I didn't implement any try-catch
- that may not working in same extreme cases
- it uses 2 files cropping.jsx and cropping.exe
- cropping.jsx and cropping.exe should be put on your desktop
- you can ran script from there, no matter which of them you execute


How it works:

- You'll be prompted to put width & height & resolution in xx,xx,xxx where "x" is a digit
- You'll be asked to browse a folder with images you want to crop
- there will be automatically created "Cropped" subfolder in that folder
- you'll have to crop first image (don't rotate but do use (alt+)shift on corners only)
- it starts cropping up to 100 images in choosen folder
- it'll close Photoshop to reopen it after 2 seconds itself with cropping.exe
- it'll start to crop next 100 images in the same folder
- this process will be renewed as far as there left some images


What's else:

Meantime of auto-cropping, a script will be creating 3 notepad files on your desktop to keep metadata, whr.txt (image dimension), pth.txt (images folder path) and bnd.txt (calculation on first image). All of them will be removed after script finish its job.

Cropping.exe is compiled cropping.ahk script with just 3 very short code lines inside. I needed it as far as I know JavaScript is asynchronous language what means you can't run two process at same time. So to execute cropping.jsx I couldn't just give an proper instruction at end of that script, but I had execute AHK script (from JS's), which ran cropping.jsx after its process (with quitting photoshop) ceased. If you don't want to run cropping.exe I attached, you must compile it yourself pasting to notepad that 3 lines you find below and saving it with.ahk extension (but then you need downloaded autohotkey from ahkscript.net)


I love scripting, though I started just one year ago, and only one language I tried was JavaScript, mainly to use Photoshop from its hidden side.

Cropping.exe:

Code: Select all

sleep 2000
run, %A_Desktop%\Cropping.jsx
return
Cropping.jsx:

Code: Select all

#target photoshop
bringToFront();

(function dat() {
var whr = File('~/Desktop/whr.txt')
if (whr.exists) whr.open('r'), prm = whr.read(), whr.close(), dlg = false
else {
PRM = 'Put date of cropping - width, height, resolution, ex. :\n\n'
PRM += '10,20,300 or 20,40,400 or else 30,45,72'
var prm = prompt(PRM, ''); whr.open('w'), whr.write(prm), whr.close(), dlg = true
}
if (!(DAT = /(\d{2}),(\d{2}),(\d{2,3})/i).test(prm)) {alert('Try correctly...'), whr.remove(), dat()}
else {
var pth = File('~/Desktop/pth.txt'), rmv = 'whr.remove(), pth.remove(), bnd.remove(), br = false'

if (!(pth).exists) (fld = Folder.selectDialog()), pth.open('w'), pth.write(fld), pth.close()
else pth.open('r'), fld = File(pth.read()), pth.close()

var crp = fld + '/Cropped', reg = /\.jpg$/i, br = true
if (!File(crp).exists) Folder(crp).create()
var len = (cnt = File(crp).getFiles(reg)).length, fls = fld.getFiles(reg)

if (!len || (cnt[cnt.length - 1].name != fls[fls.length - 1].name)) {
for(var i = len; i < (LEN = len + 100); i++) {
var img = open(File(fls[i])), w = prm.match(DAT)[1], h = prm.match(DAT)[2], r = prm.match(DAT)[3], p = prm.match(DAT)[4]
wPx = Math.round(w * (px = 28.3465)), hPx = Math.round(h * px), preferences.rulerUnits = Units.PIXELS, bnd = File('~/Desktop/bnd.txt');
(wi = (aD = activeDocument).width / wPx) < (he = aD.height / hPx) ? (WI = aD.width, HE = wi * hPx) : (HE = aD.height, WI = he * wPx), f = Math.min(wi, he)

if (!File(crp).getFiles(reg).length) {
var aHS = aD.activeHistoryState; aD.activeLayer.duplicate(), executeAction(stringIDToTypeID("newPlacedLayer"), undefined, DialogModes.NO)
var DLG = displayDialogs = (dlg ? DialogModes.ALL : DialogModes.NO); aD.crop(Array(0, 0, WI, HE)), activeDocument.activeLayer = aD.layers[0]
displayDialogs = DialogModes.NO, aD.selection.fill(foregroundColor), DLG, aD.revealAll()

bN = activeDocument.layers[0].bounds; bO = activeDocument.layers[1].bounds
if (((b0 = (bN[0] < bO[0] ? bN[0] : 0) - bO[0]) || !b0) && (bN[2] <= bO[2] || bN[2] > bO[2])) b2 = b0
if (((b1 = (bN[1] < bO[1] ? bN[1] : 0) - bO[1]) || !b1) && (bN[3] <= bO[3] || bN[3] > bO[3])) b3 = b1
aD.activeHistoryState = aHS; bnd.open('w'), bnd.write([bN[0] + b0, bN[1] + b1, bN[2] + b2, bN[3] + b3]), bnd.close()
}

if (bnd) {
bnd.open('r'), BND = bnd.read(), bnd.close(), px = BND.match(/(-?\d+ px),(-?\d+ px),(\d+ px),(\d+ px)/), aD.resizeImage(null, null, r), R = r / 72
activeDocument.crop(Array(new UnitValue(px[1])*R, new UnitValue(px[2])*R, new UnitValue(px[3])*R, new UnitValue(px[4])*R), null, (WI / f)*R, (HE / f)*R)
}

var dlg = false, jpg = new JPEGSaveOptions(); jpg.quality = 12
img.saveAs(File(fls[i].parent + '/Cropped/' + img.name), jpg), img.close(SaveOptions.DONOTSAVECHANGES)
if (fls[i].name == fls[fls.length - 1].name) {eval(rmv); break}
}
}
else alert('The process of cropping is finished for all ' + len + ' files!'), eval(rmv)

if (br) File('~/Desktop/Cropping.exe').execute()
photoshop.quit()
}
})()
Attachments
Cropping.zip
(531 KiB) Downloaded 717 times
Last edited by Kukurykus on Sun Aug 07, 2016 7:22 pm, edited 1 time in total.
User avatar
txuku
Posts: 136
Joined: Thu Jan 01, 1970 12:00 am

Re: Need to clear Photoshop's memory

Post by txuku »

Bonjour

An excellent idea Kukurykus :)

J have tests after having downloaded and installed AutoHotKey - very good software - and recreated Cropping.exe

CS5 closes after processing 100 images then not restarts.

I manually raise and the Cropping.jsx and image processing well continue as a result of already processed images.

What happened ?

The path of the software?

A dropplet is necessary?
User avatar
Kukurykus
Posts: 528
Joined: Mon Jul 25, 2016 12:36 pm

Re: Need to clear Photoshop's memory

Post by Kukurykus »

Fortunately I have CS5.1 too. I had to test this script there as well, but my .jsx were set to open in CS2 so I gave up. That's of course no problem I reset them to be open by CS5.1 and later when I need I simply reinsatll CS2 so they can be linked to version I use mainly. Just give me a time to test it and I'll come later to update this post.

I assumed that process may fail in higher Phosothop version as there is bug with .resizeImage(), and it seems also .crop() in CS2, or just strange behaviour changed for better starting from CS3 (as I noticed).

I'm not sure that will be easy to fix it for .crop() for me, though I did it for .resizeImage() with success. If I'll fail I try the same with ScriptListener function. Maybe that'll help if nothing else for the moment.


EDIT:

I just tested script on CS5.1 without any changes in code and it worked alike CS2.
Frankly I didn't tested it on 100 or more photos as I don't have so many, so maybe that is problem, but I don't think so.

For a try change images limit to open at one session (before photoshop reopen for next part) in the code:

change this line:

Code: Select all

for(var i = len; i < (LEN = len + 100); i++) {
for this with any lower number of photos:

Code: Select all

for(var i = len; i < (LEN = len + 5); i++) {
That will be good if you have about 20 - 30 images in your folder, just for a try. Let me know if it worked for you this time...

The one change (but had no effect to functionality) was RegEx correction in this line:

Code: Select all

if (!(DAT = /(\d{2}),(\d{2}),(\d{3})/i).test(prm)) {alert('Try correctly...'), whr.remove(), dat()}
for possibility to set resolution under 100 dpi, like 72 dpi for example.

Code: Select all

if (!(DAT = /(\d{2}),(\d{2}),(\d{2,3})/i).test(prm)) {alert('Try correctly...'), whr.remove(), dat()}

I don't know will it help but I used Windows 10, all scripts I put on Desktop, and 'test' folder kept on desktop too.
I know that if I'd like to run cropping.exe when it's on some server or another computer then in lower versions of Windows it asked each time am I sure I want to run unknown software. Provided script assumes all is on desktop, so that is not a case. Anyway maybe you should let for running such applications (like ahk) changing windows settings about level of risk (I don't remember how and where's it's named exactly).


If it doesn't work for you, maybe you do something wrong, or there is something specific I can't even guess what. It would be good if someone else tested it beside you, from people you know or just this community.


Ps.1 I exchanged code for that just little updetated version I mentioned both in text and attachment.

Ps.2 ...or maybe it sufficients you increase a delay in ahk code from 2000 to 4000 or 6000 as yout Photoshop quits longer than mine, if so try this:

Code: Select all

sleep 5000
run, %A_Desktop%\Cropping.jsx
return
User avatar
txuku
Posts: 136
Joined: Thu Jan 01, 1970 12:00 am

Re: Need to clear Photoshop's memory

Post by txuku »

Bonjour

J have tried to sleep 5000.
This does not open Photoshop Cropping.jsx but the script (with Notepad2).


If I run adds

Code: Select all

C: \ Program Files \ Adobe \ Adobe Photoshop CS5 (64 Bit) \ Photoshop.exe
run, A_Desktop%% \ Cropping.jsx
return

This does not restart the script.

What is the reload command?
User avatar
Kukurykus
Posts: 528
Joined: Mon Jul 25, 2016 12:36 pm

Re: Need to clear Photoshop's memory

Post by Kukurykus »

Make sure you keep Cropping.jsx and Cropping.exe on your desktop not in your Photoshop script folder, however I think you do correctly ;)


Reload javascript command is at the end:

Code: Select all

if (br) File('~/Desktop/Cropping.exe').execute()
it'll be executed on the condition br is true.
You may test this condition is fulfilled changing last 2 lines to these:

Code: Select all

alert(br)
if (br) File('~/Desktop/Cropping.exe').execute()
photoshop.quit()
Only then you'll get to know is all about false condition, and that makes after photoshop quitting Cropping.exe isn't executed or if it's true is that problem of something other (maybe not PS setting). TIP: Even if you kept files on your destop, don't change paths inside code.


Just to be sure you may set alert also after this line:

Code: Select all

var crp = fld + '/Cropped', reg = /\.jpg$/i, br = true
so:

Code: Select all

var crp = fld + '/Cropped', reg = /\.jpg$/i, br = true, alert(br)
Sometimes such things might happen is caused by enabled system security (do you have Windows 10?) or non-english version of WIN/PS?

PS. I'm not sure you minded reload command in ahk? if so I don't know this language good, but if all I adviced now will fail you can make a loop of that ahk 3 lines code... Personally I never tried yet but there is excellent documentation and rapid communication with community on the forum.


Just give it a try and then we'll see...
User avatar
txuku
Posts: 136
Joined: Thu Jan 01, 1970 12:00 am

Re: Need to clear Photoshop's memory

Post by txuku »

Bonjour


I swim a little ...............


Scripts are on my desk.

I have Windows 7 64 bit.

The answer is true.

I have tried to open my scripts ExtendScript Toolkit and start the execution with F5 AutoHotkey:

Code: Select all

WinWaitActive ( "Cropping.jsx - ExtendScript Toolkit" )
;IfWinExist ( "Cropping.jsx - ExtendScript Toolkit" )
{
Send, F5
}
return
No results ! :oops:
User avatar
Kukurykus
Posts: 528
Joined: Mon Jul 25, 2016 12:36 pm

Re: Need to clear Photoshop's memory

Post by Kukurykus »

I have Win 7 in my work when I'll back in a week. So if you won't think up anything esle to get it done I test it there and tell you how it worked.

For now I wanted just to ask. Have you ran each time this script from ExtendScript like I see now in your example or just outside, so I mean ExtendScript stays closed all time through script is working?

If you had opened ExtendScript then after Photoshop quitted, it would go back to ExtendScript (and perhaps opened there a launched script on new sheet with current script content. But then it wouldn't open back Photoshop for sure).

So if you used it with ExtendScript that is reason it couldn't work. You must directly run it from Desktop when ExtendScript is closed.


EDIT 1

New idea. Did you try to run a script (of course when ExtendScript is cloded) from cropping.exe or only from cropping.jsx ? If it works from cropping.exe like from cropping.jsx then there is no problem. But if cropping.exe doesn't open Photoshop like cropping.jsx that means Windows 7 doean's read %A_Desktop% part like in Windows 10. If so you must go to AutoHotKey documentation or forum community to find/ask what is Windows 10 equivalent of %A_Desktop% for Windows 7. Or you can use typical (undirect) path of %A_Desktop% (however I'm not familiar with AHK enough to tell you now how to create unshortered system path, although it must be very obvious and simple I guess).


EDIT 2

Let's do other little test.

1) create these scripts:

scr1.jsx:

Code: Select all

#target photoshop
File('~/Desktop/scr.exe').execute()
scr2.jsx:

Code: Select all

#target photoshop
alert('Hello!')
scr.exe

Code: Select all

sleep 2000
run, %A_Desktop%\scr2.jsx
return
2) save them on your Desktop
3) quit ESTK and PS if you have them turned on
4) Launch your Photoshop
5) double-click scr1.jsx



If that worked then all should work fine with those scripts before, if not then it want become a unexplained mystery...
Last edited by Kukurykus on Wed Aug 10, 2016 3:23 pm, edited 2 times in total.
User avatar
txuku
Posts: 136
Joined: Thu Jan 01, 1970 12:00 am

Re: Need to clear Photoshop's memory

Post by txuku »

Bonjour

I have tried them without success and 2 src1.jsx
This opens ExtendScript without throwing.

And with Notepad2 same.


J wanted to your Cropping.exe in place of mine:

CS5 does not open - ExtendScript only opens

So I must ExtendScript can start with F5 via AutoHotkey!


But my English is far from being the height for AutoHotkey FCC !!! :oops:
User avatar
Kukurykus
Posts: 528
Joined: Mon Jul 25, 2016 12:36 pm

Re: Need to clear Photoshop's memory

Post by Kukurykus »

In last post I made 2 mistakes. Now I corrected them.

I changed cropping.exe for scr.exe in scr1.jsx and scr1.jsx for scr2.jsx in scr.exe (originally scr.ahk - another mistake).

scr1.jsx:

Code: Select all

#target photoshop
File('~/Desktop/scr.exe').execute()
scr2.jsx:

Code: Select all

#target photoshop
alert('Hello!')
scr.exe

Code: Select all

sleep 2000
run, %A_Desktop%\scr2.jsx
return
Please try again...