-1

We are working on downloading finished product photo for a website and put it into a custom layer in photoshop. It takes a long time to do manually so i'm trying to see if we can automate the process by downloading the JPEG into a local folder and output the final product into another folder as a JPEG.

Workflow - -Download JPEG into folder -Move to Photoshop file -Replace layer image in photoshop with new downloaded JPEG -Export new photoshop file as a JPEG in output folder (possibly into google drive)

A screenshot of the photoshop file is here. enter image description here

I'm not a coder so I used ChatGPT to do try and build an introductory code and I'm just seeing if it will work.

// Load required modules
const request = require('request');
const fs = require('fs');

// Download the JPEG file from a URL and save it in a local folder
const imageUrl = 'https://example.com/image.jpg';
const downloadFolder = '/path/to/download/folder';
const fileName = 'downloaded_image.jpg';

request(imageUrl)
  .pipe(fs.createWriteStream(`${downloadFolder}/${fileName}`))
  .on('close', function() {
    console.log('JPEG downloaded successfully');
    
    // Open the Photoshop file
    const photoshopFile = '/path/to/your/photoshop/file.psd';
    const docRef = app.open(new File(photoshopFile));
    
    // Replace the layer image in Photoshop with the new downloaded JPEG
    const layerName = 'Layer 1';
    const layerRef = docRef.layers.getByName(layerName);
    const newLayerRef = docRef.artLayers.add();
    newLayerRef.name = layerName;
    docRef.activeLayer = newLayerRef;
    docRef.selection.selectAll();
    docRef.paste();
    docRef.selection.deselect();
    layerRef.remove();

    // Export the new Photoshop file as a JPEG and save it in the output folder
    const outputFolder = '/path/to/output/folder';
    const outputFileName = 'new_file.jpg';
    const jpegOptions = new JPEGSaveOptions();
    jpegOptions.quality = 8; // adjust the quality as needed
    docRef.saveAs(new File(`${outputFolder}/${outputFileName}`), jpegOptions);
    docRef.close(SaveOptions.DONOTSAVECHANGES);

    console.log('New Photoshop file saved as JPEG');
    
    // Upload the new file to Google Drive (assuming you have authenticated and authorized access)
    const { google } = require('googleapis');
    const drive = google.drive({ version: 'v3', auth: YOUR_AUTH_TOKEN });
    const fileMetadata = { name: outputFileName };
    const media = { body: fs.createReadStream(`${outputFolder}/${outputFileName}`) };
    drive.files.create({ resource: fileMetadata, media: media, fields: 'id' }, function (err, file) {
      if (err) {
        console.error('Error uploading file to Google Drive:', err);
      } else {
        console.log(`New file uploaded to Google Drive with ID: ${file.data.id}`);
      }
    });
});

I haven't tried it yet I'm just seeing if it's even possible and if i'm on the right track.

1 Answers1

0

I'm not sure getting ChatGPT to write code without even checking it is the way forward - as I'm not sure it qualifies a Minimal, Reproducible Example.

However, let's look at the tasks: Download image: Swap downloaded image with layer in Photoshop file. Save it.

The central part needs reworking as it ran into several errors:

// Switch off any dialog boxes
displayDialogs = DialogModes.ERROR; // OFF

const imageName = '\\path\\to\\downloaded_image.jpg';
var fileRef = new File(imageName);
var imageRef = app.open(fileRef);
// Select all
imageRef.selection.selectAll();
//copy image
activeDocument.selection.copy();
//select nothing
app.activeDocument.selection.deselect();

const photoshopFile = '\\path\\to\\my\\Photoshop_file.psd';
// open photoshop file
// .PSD are trated differently so the code is a bit different.
var id511 = charIDToTypeID( "Opn " );
var desc109 = new ActionDescriptor();
var id512 = charIDToTypeID( "null" );
desc109.putPath( id512, new File(photoshopFile) );
executeAction( id511, desc109, DialogModes.NO );
// make a reference to the Photoshop file
var docRef = app.activeDocument;

// Replace the layer image in Photoshop with the new downloaded JPEG
const layerName = 'Layer 1';
const layerRef = docRef.layers.getByName(layerName);
// make a reference to the active layer
docRef.activeLayer = layerRef;
// Select all
docRef.selection.selectAll();
// Delete the pixels
docRef.selection.clear();
// Select nothing
app.activeDocument.selection.deselect();
docRef.paste();

Instead of deleting the layer - just delete the pixels and past the image onto the same layer.

Also there was no copy command in your script so it wouldn't have got very far.

Ghoul Fool
  • 6,249
  • 10
  • 67
  • 125