Skip to main content
tokuredit
Inspiring
March 14, 2018
Answered

Insert a logo into the active document and Register the logo path in the dialog box!

  • March 14, 2018
  • 3 replies
  • 1487 views

Hello everyone! The title says it all:

This script written by Kukurykus is 99% of what I'm looking for.

Register the path of a file in the dialog box!

  1. PTH = "~/Campaign of the last added image"
  2.  
  3. (function DLG(v) { 
  4.      pth = (dlg = new Window('dialog')).add('edittext', undefined, v), pth.alignment = 'center' 
  5.      b1 = (grp = dlg.add('group')).add('button', undefined, 'Locate file').onClick = function() { 
  6.           if (((fle = decodeURI(File.openDialog('Select file:'))) != 'null') && fle != v) { 
  7.                pth.text = fle, (fle = File($.fileName)).open('r'), jsxFile = fle.read().replace 
  8.                (/"(.*)"(?=;)/, '"' + pth.text + '"'), fle.close(), fle.open('w'
  9.                fle.write(jsxFile), fle.close(), dlg.close(), DLG(pth.text) 
  10.           } 
  11.      } 
  12.      b2 = grp.add('button', undefined, 'Import OK').onClick = function() { 
  13.           if ((fle = File(pth.text)).exists) open(fle), refresh() 
  14.      } 
  15.      dlg.show() 
  16. })(PTH) 

I need to make a small modification so that it works 100% to meet my needs:

Change the "Open" option to Insert in the current file

It is essential that you always keep track of the path of the last image inserted in the dialog box!

Whenever I run this script, always insert the last recorded image into the current document.

Thank you!

This topic has been closed for replies.
Correct answer Kukurykus

Perfect! You are a genius Kukurykus ... Thank you!

You could post here the duly corrected code so that I can mark the right answer. I think it will be very useful for many here.


Normally I don't repost codes but this time there was one line too less! r-bin​​ is a pure genius, my codes are simply crazy!

PTH = "~/Campaign of the last added image";

(function DLG(v) {

     pth = (dlg = new Window('dialog')).add('edittext', undefined, v), pth.alignment = 'center'

     b1 = (grp = dlg.add('group')).add('button', undefined, 'Locate file').onClick = function() {

          if (((fle = decodeURI(File.openDialog('Select file:'))) != 'null') && fle != v) {

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

               (/"(.*)"(?=;)/, '"' + pth.text + '"'), fle.close(), fle.open('w')

               fle.write(jsxFile), fle.close(), dlg.close(), DLG(pth.text)

          }

     }

     b2 = grp.add('button', undefined, 'Import OK').onClick = function() {

          function plc(v) {

               function sTT(v) {return stringIDToTypeID(v)} (dsc1 = new ActionDescriptor()).putPath(sTT('null'), v)

               dsc1.putEnumerated(sTT('freeTransformCenterState'), sTT('quadCenterState'), sTT('QCSAverage'));

               (dsc2 = new ActionDescriptor()).putUnitDouble(sTT('horizontal'), sTT('pixelsUnit'), 0)

               dsc2.putUnitDouble(sTT('vertical'), sTT('pixelsUnit'), 0)

               dsc1.putObject(sTT('offset'), sTT('offset'), dsc2)

               executeAction(sTT('placeEvent'), dsc1, DialogModes.NO);

          }

          if ((fle = File(pth.text)).exists) plc(fle), refresh()

     }

     dlg.show()

})(PTH)

3 replies

tokuredit
tokureditAuthor
Inspiring
March 14, 2018

The editor is ExtendScript Toolkit, I made the modifications and unfortunately the error continues.

PTH = "~/Campaign of the last added image";

(function DLG(v) {

     pth = (dlg = new Window('dialog')).add('edittext', undefined, v), pth.alignment = 'center'

     b1 = (grp = dlg.add('group')).add('button', undefined, 'Locate file').onClick = function() {

          if (((fle = decodeURI(File.openDialog('Select file:'))) != 'null') && fle != v) {

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

               (/"(.*)"(?=;)/, '"' + pth.text + '"'), fle.close(), fle.open('w')

               fle.write(jsxFile), fle.close(), dlg.close(), DLG(pth.text)

          }

     }

     b2 = grp.add('button', undefined, 'Import OK').onClick = function() {

          function plc(v) {

             // (dsc1=new ActionDescriptor()).putPath(sTT('null'),v),dsc1.putEnumerated

             //(sTT('freeTransformCenterState'), sTT('quadCenterState'), sTT('QCSAverage'));

             

                //to these:

                (dsc1 = new ActionDescriptor()).putPath(sTT('null'), v)

                dsc1.putEnumerated(sTT('freeTransformCenterState'), sTT('quadCenterState'), sTT('QCSAverage'));

             

             

               (dsc2 = new ActionDescriptor()).putUnitDouble(sTT('horizontal'), sTT('pixelsUnit'), 0)

               dsc2.putUnitDouble(sTT('vertical'), sTT('pixelsUnit'), 0)

               dsc1.putObject(sTT('offset'), sTT('offset'), dsc2)

               executeAction(sTT('placeEvent'), dsc1, DialogModes.NO);

          }

          if ((fle = File(pth.text)).exists) plc(fle), refresh()

     }

     dlg.show()

})(PTH)

Kukurykus
Legend
March 14, 2018

I used exact code you just posted, saved as .jsx file, opened Photoshop CS6 EXTENDED, opened some file, ran script and it worked. Then I tried it with CC 2018 (19.1.1). Locating button works, but when I click import button it doesn't react. Later I see what is going on. However I don't get error you does.

tokuredit
tokureditAuthor
Inspiring
March 14, 2018

I use here the Photoshop CC 2018 (19.0)

Thanks for the dedication. I'm going to stay here on your own.

Kukurykus
Legend
March 14, 2018

PTH = "~/Campaign of the last added image";

(function DLG(v) {

     pth = (dlg = new Window('dialog')).add('edittext', undefined, v), pth.alignment = 'center'

     b1 = (grp = dlg.add('group')).add('button', undefined, 'Locate file').onClick = function() {

          if (((fle = decodeURI(File.openDialog('Select file:'))) != 'null') && fle != v) {

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

               (/"(.*)"(?=;)/, '"' + pth.text + '"'), fle.close(), fle.open('w')

               fle.write(jsxFile), fle.close(), dlg.close(), DLG(pth.text)

          }

     }

     b2 = grp.add('button', undefined, 'Import OK').onClick = function() {

          function plc(v) {

               (dsc1 = new ActionDescriptor()).putPath(sTT('null'), v), dsc1.putEnumerated

               (sTT('freeTransformCenterState'), sTT('quadCenterState'), sTT('QCSAverage'));

               (dsc2 = new ActionDescriptor()).putUnitDouble(sTT('horizontal'), sTT('pixelsUnit'), 0)

               dsc2.putUnitDouble(sTT('vertical'), sTT('pixelsUnit'), 0)

               dsc1.putObject(sTT('offset'), sTT('offset'), dsc2)

               executeAction(sTT('placeEvent'), dsc1, DialogModes.NO);

          }

          if ((fle = File(pth.text)).exists) plc(fle), refresh()

     }

     dlg.show()

})(PTH)

tokuredit
tokureditAuthor
Inspiring
March 14, 2018

Hi Kukurykus I tried to insert images of different formats: png and jpge and presented this error in line 14

Kukurykus
Legend
March 14, 2018

You probably must remove white spaces there are produced by this lame Jive editor. Those after a line that is red. I'm not sure it is like I'm advising because I added sam tabs and spaces now after that line and it still worked. I tried to open .png files and it worked as well. Maybe change then these lines:

(dsc1 = new ActionDescriptor()).putPath(sTT('null'), v), dsc1.putEnumerated

(sTT('freeTransformCenterState'), sTT('quadCenterState'), sTT('QCSAverage'));

to these:

(dsc1 = new ActionDescriptor()).putPath(sTT('null'), v)

dsc1.putEnumerated(sTT('freeTransformCenterState'), sTT('quadCenterState'), sTT('QCSAverage'));

Copy that script I posted again to your ESTK as I changed 'distanceUnit' to 'pixelUnit' meantime. But I don't think that does any difference. Are you sure you saved to the disk this script and reopened Photoshop before ran it from Ps?

JJMack
Community Expert
Community Expert
March 14, 2018

You seem to want the script to modify itself and rewrite the script while is is open and running or change the way it works and store the file path in a custom script option.  I  think a script rewrite itself if  you exit the script with Alt+F4 the next time you use the scrupt the last  pathe locate will be the default. I was editing it and running it the editor told me something chaned the file. I use Alt+F4 to end the scrupt amd the net time I uset it the last Filepath was the defailt.  I did not realive the script coule rewrite itself.

JJMack
Kukurykus
Legend
March 14, 2018

Why do you think script can't rewrite itself, if that script of me he posted do exactly this thing? Custom option is not needed but may be alternative as far as someone doesn't care link to logo will be reseted while exiting Photoshop.

I removed my post and posted now again, as I see my answer was to your post before you edited it. And still I see you think script can't rewrite itself, but it does.

When you're in ESTK and run srcipt that rewrites a path part then like you say you are prompted of and can accept changes to script in your editor.

When you run it from Ps, choose new path by script. Then reopen Potoshop and edit script with any editor. you will see the default path is changed, so also in Photoshop default path won't be available anymore .

JJMack
Community Expert
Community Expert
March 14, 2018

I thought the file may be locked because Phtoshop opened the script file to execute it that Photoshop may be holding a lock on the file.  Then I read the code and saw that you were indeed rewritting the script file.

JJMack