Close open dialog ui

Discussion of Photoshop Scripting, Photoshop Actions and Photoshop Automation in General

Moderators: Tom, Kukurykus

luis miguel
Posts: 16
Joined: Thu Oct 27, 2016 9:56 am

Close open dialog ui

Post by luis miguel »

Good morning
I would like to close the test panel when I click on the levels button
then when I click ok at the levels window
the test panel must be reopened

this is the starting script

Code: Select all

var dlg = new Window('dialog', 'TEST',undefined);   
dlg.btnPnl = dlg.add('panel', undefined, '');
dlg.btnPnl.levels = dlg.btnPnl.add('button', undefined,'Levels', {name:'ok'});
dlg.btnPnl.cancelBtn = dlg.btnPnl.add('button', undefined, 'Cancel', {name:'cancel'});


dlg.btnPnl.levels.onClick = function () {
// =======================================================
ErrStrs = {}; ErrStrs.USER_CANCELLED=localize("$$$/ScriptingSupport/Error/UserCancelled=User cancelled the operation");
try {var idLvls = charIDToTypeID( 'Lvls' ); executeAction( idLvls, undefined, DialogModes.ALL ); }
catch(e){if (e.toString().indexOf(ErrStrs.USER_CANCELLED)!=-1) {;}
else{alert(localize("$$$/ScriptingSupport/Error/CommandNotAvailable=The command is currently not available"));}}

}

dlg.show();
User avatar
Kukurykus
Posts: 528
Joined: Mon Jul 25, 2016 12:36 pm

Re: Close open dialog ui

Post by Kukurykus »

FIRST METHOD:

Code: Select all

$.level = 0, (function fun(){
dlg.btnPnl = (dlg = new Window('dialog', 'TEST', undefined)).add('panel')
dlg.btnPnl.levels = dlg.btnPnl.add('button', undefined,'Levels', {name: 'ok'})
dlg.btnPnl.cancel = dlg.btnPnl.add('button', undefined, 'Cancel', {name :'cancel'})

dlg.btnPnl.levels.onClick = function () {
dlg.close(), executeAction(charIDToTypeID('Lvls'), undefined, DialogModes.ALL), dlg = null, fun()
}
dlg.show()
})()
SECOND METHOD:

Code: Select all

#target photoshop

dlg.btnPnl = (dlg = new Window('dialog', 'TEST', undefined)).add('panel')
dlg.btnPnl.levels = dlg.btnPnl.add('button', undefined,'Levels', {name: 'ok'})
dlg.btnPnl.cancel = dlg.btnPnl.add('button', undefined, 'Cancel', {name: 'cancel'})

dlg.btnPnl.levels.onClick = function(){
$.level = 0; dlg.close(); try{
executeAction(charIDToTypeID('Lvls'), undefined, DialogModes.ALL), $.evalFile(File($.fileName))
}
catch(err) {}
}
dlg.show()
  • You can change $.evalFile(File($.fileName)) to File($.fileName).execute(), but as far as your script isn't located in Documents / Adobe Scripts folder you will be getting security question are you sure to run this file in Photoshop.
  • Alternate was is to change $.evalFile(File($.fileName)) to eval('#include ' + File($.fileName)) to avoid that security question. It works fine for Photoshop and ExtendScript ToolKit, but if you used eval('#include ' + File($.fileName).name) then it's going to work only in Photoshop.
luis miguel
Posts: 16
Joined: Thu Oct 27, 2016 9:56 am

Re: Close open dialog ui

Post by luis miguel »

it seems that it works very well
thank you.
User avatar
Kukurykus
Posts: 528
Joined: Mon Jul 25, 2016 12:36 pm

Re: Close open dialog ui

Post by Kukurykus »

VARIATION of SECOND METHOD:

If you'd like to use File($.fileName).execute() after all, you could use following script:

Code: Select all

#target photoshop
function rw(v1, v2, v3) {v2.open(v1), v1 < 'w' ? v3 = v2.read() : v2.write(v3), v2.close(); return v3}

if (!/A[\w ]+s$/.test(decodeURI(fNp = (fN = File($.fileName)).parent))) {
rw('w', pth = File(Folder.desktop + '/Path.txt'), '')
File(fNp + '/Sender.bat').execute()
while(!rw('r', pth)) refresh();
(jsx = File(eval(rw('r', pth).replace(/\n/, '')
.replace(/\\/g, '//')) + '/' + fN.name))
File(app.path + '/Presets/' + fN.name.slice(0, -4))
.createAlias(jsx), pth.remove(), jsx.execute()
}

else {
dlg.btnPnl = (dlg = new Window('dialog', 'TEST', undefined)).add('panel')
dlg.btnPnl.levels = dlg.btnPnl.add('button', undefined,'Levels', {name: 'ok'})
dlg.btnPnl.cancel = dlg.btnPnl.add('button', undefined, 'Cancel', {name: 'cancel'})

dlg.btnPnl.levels.onClick = function() {
$.level = 0; dlg.close(); try{
executeAction(charIDToTypeID('Lvls'), undefined, DialogModes.ALL), File($.fileName).execute()
}
catch(err) {}
}
dlg.show()
}
as well as Windows batch file that should be saved in the same folder your script is with a .bat extension:

Code: Select all

set "dat=%HOMEPATH%\desktop"
cd %dat%
for /f "skip=2 tokens=2*" %%a in ('reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Personal"') do @set "docs=%%b"
xcopy "%dat%\c.jsx" "%docs%\Adobe Scripts" /f /y
echo "%docs%\Adobe Scripts" > %dat%\Path.txt
Now your script will copy itself to Documents / Adobe Scripts Folder and execute its copy from that folder. It let you to avoid security question, what pops up when script is ran from other source. Additionally it will create alias In your Photoshop / Presets Folder to your script placed in Dcouments / Adobe Scripts Folder that let you use it from your Photoshop any time you lauch it again, as it will be placed on File / Scripts list.
luis miguel
Posts: 16
Joined: Thu Oct 27, 2016 9:56 am

Re: Close open dialog ui

Post by luis miguel »

From what I see it seems to me that these are good for windows
I use mac so I do not know if they are usable.
User avatar
Kukurykus
Posts: 528
Joined: Mon Jul 25, 2016 12:36 pm

Re: Close open dialog ui

Post by Kukurykus »

Unfortunately not, but if anyone wants to try it I come with even more advanced vesion for Windows...

EXTENDED VARIATION of SECOND METHOD:

Code: Select all

#target photoshop
function rw(v1, v2, v3) {v2.open(v1), v1 < 'w' ? v3 = v2.read() : v2.write(v3), v2.close(); return v3}
function SYS(v) {return File(Folder.desktop + '/sys.' + v)} sys = SYS('txt'), bat = SYS('bat')
$.level = 0; try{aDfN = activeDocument.fullName, close(SaveOptions.DONOTSAVECHANGES)}
catch(err) {aDfN = ''}; lN = File(Folder.desktop + '/fullName.txt')

if (!/A[\w ]+s$/.test(decodeURI(fNp = (fN = File($.fileName)).parent))) {
rw('w', pth = File(Folder.desktop + '/Path.txt'), '')
if (aDfN) {try{rw('w', lN, aDfN)} catch(err) {}}
File(fNp + '/Sender.bat').execute()
while(!rw('r', pth)) refresh();
(jsx = File(eval(rw('r', pth).replace(/\n/, '')
.replace(/\\/g, '//')) + '/' + fN.name)), pth.remove()
File((ap = app.path) + '/Presets/' + fN.name.slice(0, -4)).createAlias(jsx)

sys.open('w'), sys.write('ping 127.0.0.1 -n 2 > nul\r\start "' +
ap.fsName + '\\photoshop.exe" ' + '"' + jsx.fsName + '"\r\exit')
sys.close(), File('~/Desktop/sys.txt').rename('sys.bat')
File(String(sys).replace(/txt/, 'bat')).execute(), photoshop.quit()
}

else {
dlg.btnPnl = (dlg = new Window('dialog', 'TEST', undefined)).add('panel')
dlg.btnPnl.levels = dlg.btnPnl.add('button', undefined,'Levels', {name: 'ok'})
dlg.btnPnl.cancel = dlg.btnPnl.add('button', undefined, 'Cancel', {name: 'cancel'})

dlg.btnPnl.levels.onClick = function() {
bat.remove(), dlg.close(); try{
executeAction(charIDToTypeID('Lvls'), undefined,
DialogModes.ALL), File($.fileName).execute()
}
catch(err) {}
}
if (lN.exists) open(File(rw('r', lN))), refresh(), lN.remove()
if (documents.length) dlg.show() else alert('Open document!')
}
Now your Photoshop is going to be reopened, and last active Document there was opened will open once again while script start its main part, display dialog box. If there was no document opened before Photoshop was closed you recieve appriopate information. The adventage of this method is that you don't have manually reopen Photoshop to add and activate script on File / Scripts! As you see in the script, it uses another batch file (this time created by .jsx script) which opens Photoshop with Command Line (Prompt), what btw is another way to run scripts in Ps without security warning. Take into consideration that this script uses also:

Code: Select all

set "dat=%HOMEPATH%\desktop"
cd %dat%
for /f "skip=2 tokens=2*" %%a in ('reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Personal"') do @set "docs=%%b"
xcopy "%dat%\c.jsx" "%docs%\Adobe Scripts" /f /y
echo "%docs%\Adobe Scripts" > %dat%\Path.txt
luis miguel
Posts: 16
Joined: Thu Oct 27, 2016 9:56 am

Re: Close open dialog ui

Post by luis miguel »

thank you so much
for all your help