Skip to main content
Participating Frequently
September 11, 2009
Question

Scripting problem replacing layer contents.

  • September 11, 2009
  • 3 replies
  • 1456 views

This is a follow up to the discussion in this thread http://forums.adobe.com/thread/480444?tstart=0.

I've virtually completed the script, I attached it as a .txt file rather than .jsx , In a brief nutshell it does the following:

Gets a text file name.

opens the psd identified in that document

walks the layers in that file

     if the layer is a text file and there is replacement text for that name in the input file, replace the text

     if the layer is an image (NORMAL) and there is an input file with a name (.jpg or .psd) matching the layer name then

           the layer is turned into a smart object and it's contents replaced with the contents of the relevant file.

Saves the modified psd file as the name indicated in the input file and close the original file.

If the input file indicates the file should be left open, reopen the modified file name.

Almost everything works well except ...

1. Sometimes the modified file loses it's layering and winds up with only a "background" layer whereas other times it retains it's layering. I'm sure this is due to some characteristic of the input template .psd, but with limited understanding of photoshop itself I've no idea what characteristic to look for.

Any suggestions as to what might be the source of this problem?

2. It appears that sometimes when creating the 1 layer file that will replace an image layer as a .jpg it losses the background transparency. This doesn't seem to happen if saved as a .psd, but the user wants to work with jpgs.

Any ideas why?

3. When replacing an image layer from another replacement file, the resulting new image in the modified file doesn't seem to be properly aligned. Incidentally, the same behaviour occurs if I do the steps of creating a smart layer and replacing manually in photoshop. I tried to attached the .psd files but the forum would not allow it.

Again any ideas why it might be misaligned or a code change that will fix it?

Any suggestions would be most thankfully accepted.

Steve Ellis.

This topic has been closed for replies.

3 replies

Inspiring
September 12, 2009
1. Sometimes the modified file loses it's layering and winds up with only a "background" layer whereas other times it retains it's layering. I'm sure this is due to some characteristic of the input template .psd, but with limited understanding of photoshop itself I've no idea what characteristic to look for.

Any suggestions as to what might be the source of this problem

Some formats do not support layers- jpg and gif for example.

3. When replacing an image layer from another replacement file, the resulting new image in the modified file doesn't seem to be properly aligned. Incidentally, the same behaviour occurs if I do the steps of creating a smart layer and replacing manually in photoshop. I tried to attached the .psd files but the forum would not allow it.

Again any ideas why it might be misaligned or a code change that will fix it

When a file is placed, you set the size and position in the layer. When you replace the contents the same transform values are used. If the file used to replace is not the same size as the first, it will not be the same size and location in the layer. I don't know of any way to determine the transform or reset it. The script could resize but that would require opening the file, resizing, and saving before the replace.

CatkononAuthor
Participating Frequently
September 14, 2009

3. When replacing an image layer from another replacement file, the resulting new image in the modified file doesn't seem to be properly aligned. Incidentally, the same behaviour occurs if I do the steps of creating a smart layer and replacing manually in photoshop. I tried to attached the .psd files but the forum would not allow it.

Again any ideas why it might be misaligned or a code change that will fix it

When a file is placed, you set the size and position in the layer. When you replace the contents the same transform values are used. If the file used to replace is not the same size as the first, it will not be the same size and location in the layer. I don't know of any way to determine the transform or reset it. The script could resize but that would require opening the file, resizing, and saving before the replace.

I created the .psd file with which to replace the image layer from the image layer of the original by changing the image and saving it. I then checked the image size, canvas size, and transform values of both and they matched. When I ran the program the replaced image was misplaced, too high in the new psd. I then editing the replacing file and changing the y transform value until it resulted in the right place when placed by the script. For it to work properly the y transform value of the replacing layer was approximately 100 more than the replaced layer (I assume this particular value will vary with the particulars of the files). This is what confuses me, when they were identical it didn't work properly.

Inspiring
September 14, 2009

In the sample you posted the only smart object layer is the arrow layer. I duped the laptop layer to a new doc and saved as png. I then saved the color bars as a png. I then placed the laptop as a new smart object layer and deleted to non SO version. Now by using replace I can switch back and forth between the laptop and the bars and both are where they should be. My samples are attached

If I just convert the laptop layer to a smart object the bars are offset when replaced. So I think the key is if you want to have a replaceable SO in a doc you need to place instead of convert.

Inspiring
September 11, 2009

2. It appears that sometimes when creating the 1 layer file that will replace an image layer as a .jpg it losses the background transparency. This doesn't seem to happen if saved as a .psd, but the user wants to work with jpgs.

Any ideas why?

This is an easy one. jpgs don't do transparency. Your user will have to use png files (or even gifs) if they need a simple image file with transparency.

I haven't looked at the code or psd files so I can't comment on your other questions.

CatkononAuthor
Participating Frequently
September 11, 2009

I couldn't attached the relavent .psd files to the prior post, so I put them in a zip.