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

Load a remote script with the help of this code

Explorer ,
Jan 12, 2021 Jan 12, 2021

Copy link to clipboard

Copied

I have in my hands a script that downloads a photo via the internet and opens it in Photoshop.

Which was made available in another question


https://community.adobe.com/t5/photoshop/opening-a-remote-image-in-photoshop/m-p/11746704#M503332


would it be possible to modify it so that, instead of opening an image, it opens Javascript?

Can someone help me? Thank you!


This would be the javascript I would like to open; https://gist.githubusercontent.com/eliasmarquesfotografo/4dce4fbbe44ee29e5626af956ba15a9c/raw


Code;

// OpenImageFromWeb.jsx
// Copyright 2006-2009
// Written by Jeffrey Tranberry
// Photoshop for Geeks Version 3.0
// modified by MLH
// modified by JJMACK 2010
/*
<javascriptresource>
<about>$$$/JavaScripts/OpenImageFromWeb/About=JJMack's OpenImageFromWeb.^r^rCopyright 2010 Mouseprints.^r^rJJMack's Script.^rOpen Image From Web as a Placed smart object layer!</about>
<category>JJMack's Script</category>
</javascriptresource>
*/

/*
 Description:
 This sample script shows how to download images from a web server using the
 Socket object.
*/
 
// Note: Socket.read() parameter & behavior
// Socket.read() will read or time out. It may not read all data fromserver. <---------------
// Socket.read(999999) will read 999999 bytes, or timeout, or socket will be
// closed by the server.
 
// enable double clicking from the
// Macintosh Finder or the Windows Explorer
#target photoshop
 
// Make Photoshop the frontmost application
app.bringToFront();
 
/////////////////////////
// SETUP
/////////////////////////

var html = "";
var request = "";
var url = "";
var binary = "";
var requesthtml = "";
var socket = new Socket;
var domain = "www.mouseprints.net" // the domain for the file we want
var sImg = "/old/dpr/JJMack8btiSrgb.png"// the rest of the url for the file we want
var port = ":80"// the port for the file we want

 
/////////////////////////
// MAIN
/////////////////////////
var url = prompt("Enter the image's full URL http://domain/full image path",url);   // prompt for domain name
if (url != null && url != ""){
    if ( (url.indexOf("https://") != -1) || (url.indexOf("HTTPS://") != -1)) {
        //alert("Strip)")
        save  = url.substr(8,);
        url =  "http://" + save;
    }
    if ( (url.indexOf("http://") != -1) || (url.indexOf("HTTP://") != -1)  ) {
        domainPathLength = url.length - "http://".length
        domainPath = url.substr(7domainPathLength);
        pathOffset = domainPath.indexOf("/"); 
        domain = domainPath.substr(0pathOffset);
        sImg = domainPath.substr(pathOffsetdomainPath.length - pathOffset );
        // Isolate Image name 
        var Name =  sImg
        var imagePath = "";
        while (Name.indexOf("/") != -1 ) {              // Strip Path
            imagePathimagePath + Name.substr(0Name.indexOf("/") + 1);
            Name = Name.substr(Name.indexOf("/") + 1 ,);        
            }
        //alert("domain = " +  domain + " , Image = " + sImg + " Image File Name = " + Name);
        if ( domain != "" && sImg != "" && sImg != "/" && Name.indexOf(".") != -1 ) {
            var f = File("~/Desktop/" + Name); // Image file name
            f.encoding = "binary"// set binary mode
            f.open("w");

            if (socket.open(domain + port"binary")){
                //alert("GET " + sImg +" HTTP/1.1\n\n");
                requesthtml ="\n\nDmain:" + domain + " Port" + port + " binary\n"
                //request ="GET " + sImg +" HTTP/1.0\n\n"
                request ="GET " + sImg +" HTTP/1.1\nHost: " + domain + "\nAccept: image/gif, image/x-xbitmap, image/png, image/jpeg, */*\n\n";
                //alert(request);
                socket.write(request); // get the file
                //alert("After Request");
                var binary = socket.read(99999999);
                //alert("remove Header");
                binary = removeHeaders(binary);
                //alert("Write File")
                f.write(binary);
                socket.close();
                } 
            else { alert("Connection to Domain:" + domain + " Port" + port + " Failed   ");}
            
            //alert("Close");

            f.close();

            if (binary.length != 0) {
                //alert ("file length = " + binary.length );

                if(app.documents.length == 0) { 
                    //app.documents.add([width] [, height] [, resolution] [, name] [, mode] [, initialFill] [,pixelAspectRatio] [, bitsPerChannel] [,colorProfileName])
                    app.documents.add(new UnitValue(3840,'px'), new UnitValue(2160,'px'), 72nullNewDocumentMode.RGBDocumentFill.WHITE1,BitsPerChannelType.EIGHT"sRGB IEC61966-2.1" );
                    }
                placeSmartObjectf );
                } 

            f.remove(); // Remove temporary downloaded files
            }
        else { alert("Invalid Image URL: " + url ); }
        }
    else { alert("Invalid URL: " + url ); }
    }
else { if ( url == "" ) alert("No URL Entered"); }
 
/////////////////////////
// FUNCTIONS
/////////////////////////
function placeSmartObject(fileRef){
    //create a new smart object  layer using a file
    try { 
        var desc = new ActionDescriptor();
            desc.putPathcharIDToTypeID"null" ), new FilefileRef ) );
            desc.putEnumeratedcharIDToTypeID"FTcs" ), charIDToTypeID"QCSt" ),charIDToTypeID"Qcsa" ));
            desc.putUnitDoublecharIDToTypeID"Wdth" ),charIDToTypeID"#Prc" ), 100 );
            desc.putUnitDoublecharIDToTypeID"Hght" ), charIDToTypeID"#Prc" ), 100 );
            desc.putUnitDoublecharIDToTypeID"Angl" ), charIDToTypeID"#Ang" ), 0 );
            desc.putBooleancharIDToTypeID"Lnkd" ), true );
        executeActioncharIDToTypeID"Plc " ), descDialogModes.NO );
        activeDocument.activeLayer.resize(100 ,100,AnchorPosition.MIDDLECENTER);
        activeDocument.revealAll();
    } catch (e) { alert("Placeing file: '" + fileRef + "' failed"); }
};

// Remove header lines from HTTP response
function removeHeaders(binary){
    var bContinue = true ; // flag for finding end of header
    var line = "";
    var httpheader = "";
    var nFirst = 0;
    var count = 0;
    while (bContinue) {
        line = getLine(binary) ; // each header line
        httpheader = httpheader + line;
        bContinue = line.length >= 2 ; // blank header == end of header
        nFirst = line.length + 1 ;
        binary = binary.substr(nFirst) ;
    }
    if (httpheader.indexOf("Bad Request") != -1 || httpheader.indexOf("Not Found") != -1) {
        alert (requesthtml + request + httpheader);
        var binary = "";
    }
    //alert (requesthtml + request + httpheader + "\nFile length = " + binary.length);
    return binary;
};
 
// Get a response line from the HTML
function getLine(html){
    var line = "" ;
    for (var i = 0html.charCodeAt(i) != 10i++){ // finding line end
        line += html[i] ;
    }
    return line ;
};
TOPICS
Actions and scripting

Views

570

Translate

Translate

Report

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
Adobe
Community Expert ,
Jan 12, 2021 Jan 12, 2021

Copy link to clipboard

Copied

Please elaborate: What do you mean with "would it be possible to modify it so that, instead of opening an image, it opens Javascript?"

That does not make much sense, as the aim with Photoshop is to open images?

ABAMBO | Hard- and Software Engineer | Photographer

Votes

Translate

Translate

Report

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
Explorer ,
Jan 12, 2021 Jan 12, 2021

Copy link to clipboard

Copied

What does this script do is a correct web request?


but he makes a request for a photo ... I would like him to make a Javascript code that opens within my pHotoshop ...

Votes

Translate

Translate

Report

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
Community Expert ,
Jan 12, 2021 Jan 12, 2021

Copy link to clipboard

Copied

Votes

Translate

Translate

Report

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
Explorer ,
Jan 12, 2021 Jan 12, 2021

Copy link to clipboard

Copied

This is amazing and it really works !!! Thanks!

Would I be able to do this without the need for a panel?

Votes

Translate

Translate

Report

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
Community Expert ,
Jan 12, 2021 Jan 12, 2021

Copy link to clipboard

Copied

I haven't looked at the code, you'll need to do so, but I imagine it would be a case of hard coding the path or coding the path into a variable to replace the GUI selection.

Votes

Translate

Translate

Report

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
Explorer ,
Jan 12, 2021 Jan 12, 2021

Copy link to clipboard

Copied

Thanks for the answer, I'm really very raw in Javascript, I took a look here but I couldn't find the part where it points to the uri, thank you very much for your help!

Votes

Translate

Translate

Report

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
Explorer ,
Jan 12, 2021 Jan 12, 2021

Copy link to clipboard

Copied

I took a look at the code and got it but it didn't work;





//Re: Load a remote script with the help of this cod... - Adobe Support Community - 11746734






var dialog = new Window("dialog"); 
    dialog.text = "LOAD SCRIPT"
    dialog.orientation = "column"
    dialog.alignChildren = ["center","top"]; 
    dialog.spacing = 10
    dialog.margins = 16

var statictext1 = dialog.add("statictext"undefinedundefined, {name: "statictext1"}); 
    statictext1.text = "LOAD SCRIPT"

var button1 = dialog.add("button"undefinedundefined, {name: "button1"}); 
    button1.text = "LOAD"

dialog.show();



button1.onclick =
function loadRemoteScript(url) {
  var xhr = new XMLHttpRequest();
  xhr.open('GET'urltrue);
  xhr.responseType = 'text';
  xhr.onload = function(e) {
    if (this.status == 200 || this.status == 304) {
      var response = this.response;
      // var downloadedFile = createTempFolder() + name + '.js';
      // window.cep.fs.writeFile(downloadedFile, response);
      csInterface.evalScript("$.runScriptFromInput("+$.stringify(response)+")");
    }
  };
  xhr.send();
};

$('#load-script').on('click'function(e){
  var url = $.trim($('#script-url').val());
  if(url.length > 0){
    loadRemoteScript(url);
  }
});
// Loads / executes a jsx file
function loadJSXFile(pPath) {
  var scriptPath = csInterface.getSystemPath(SystemPath.EXTENSION) + pPath;
  try {
    csInterface.evalScript('$._ext.evalFile("' + scriptPath + '")');
  }catch(e){
    alert('error '+e);
  }
}

Votes

Translate

Translate

Report

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
Community Expert ,
Jan 13, 2021 Jan 13, 2021

Copy link to clipboard

Copied

LATEST

I found the following which worked on the Mac, but not on Win:

 

https://gist.github.com/Trevor-/f661c2daf339f8f4a6afe65e0877bdb5

 

It appears that Windows will need different code.

Votes

Translate

Translate

Report

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