Highlighted

python photoshop scripting creating a text layer

New Here ,
Jul 28, 2018

Copy link to clipboard

Copied

Hi i have this on my python script

import win32com.client


psApp = win32com.client.Dispatch("Photoshop.Application")


psApp.Open(r"C:\Users\Kim-DEV\psd_env\psdtest.psd"

doc = psApp.Application.ActiveDocument

layerRef = doc.ArtLayers.Add()

layerRef.Kind = 2

after i run the script i get error on layerRef.Kind = 2

it says that

Traceback (most recent call last):

  File "C:\Users\Kim-DEV\psd_env\psd.py", line 8, in <module>

    layerRef.Kind = 2

  File "C:\Users\Kim-DEV\psd_env\lib\site-packages\win32com\client\dynamic.py", line 565, in __setattr__

    self._oleobj_.Invoke(entry.dispid, 0, invoke_type, 0, value)

pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 'Adobe Photoshop', '', None, 0, -2147213322), None)

can anyone tell me how to fix this ?

TOPICS
Actions and scripting

Views

4.0K

Likes

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

python photoshop scripting creating a text layer

New Here ,
Jul 28, 2018

Copy link to clipboard

Copied

Hi i have this on my python script

import win32com.client


psApp = win32com.client.Dispatch("Photoshop.Application")


psApp.Open(r"C:\Users\Kim-DEV\psd_env\psdtest.psd"

doc = psApp.Application.ActiveDocument

layerRef = doc.ArtLayers.Add()

layerRef.Kind = 2

after i run the script i get error on layerRef.Kind = 2

it says that

Traceback (most recent call last):

  File "C:\Users\Kim-DEV\psd_env\psd.py", line 8, in <module>

    layerRef.Kind = 2

  File "C:\Users\Kim-DEV\psd_env\lib\site-packages\win32com\client\dynamic.py", line 565, in __setattr__

    self._oleobj_.Invoke(entry.dispid, 0, invoke_type, 0, value)

pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 'Adobe Photoshop', '', None, 0, -2147213322), None)

can anyone tell me how to fix this ?

TOPICS
Actions and scripting

Views

4.0K

Likes

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
Jul 28, 2018 0
New Here ,
Jul 28, 2018

Copy link to clipboard

Copied

layerRef.Kind = 1 works..

but 2 above i get already errors same on that one.

Likes

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
Reply
Loading...
Jul 28, 2018 0
Adobe Community Professional ,
Jul 28, 2018

Copy link to clipboard

Copied

You can't set layer kind this way, only check its kind. From empty layers you can create only text layers. Say what kind is 2?

Likes

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
Reply
Loading...
Jul 28, 2018 0
New Here ,
Jul 28, 2018

Copy link to clipboard

Copied

i wanted to create text layers. and 2 value is a psTextLayer

Likes

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
Reply
Loading...
Jul 28, 2018 0
Adobe Community Professional ,
Jul 28, 2018

Copy link to clipboard

Copied

Like I said I don't think you can set new artLayer to the kind, even if it's textLayer. It won't change its kind. You have to use appropriate command textLayer was created. Anyway if you remove last line from you code will it create at least artLayer?

Likes

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
Reply
Loading...
Jul 28, 2018 0
New Here ,
Jul 28, 2018

Copy link to clipboard

Copied

yes it will create at least the artlayer. do you have any idea on how i can set it ?

Likes

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
Reply
Loading...
Jul 28, 2018 0
Adobe Community Professional ,
Jul 28, 2018

Copy link to clipboard

Copied

In javascript that would be: activeDocument.artLayers.add().kind = LayerKind.TEXT

Likes

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
Reply
Loading...
Jul 28, 2018 0
New Here ,
Jul 28, 2018

Copy link to clipboard

Copied

if i replace

doc = psApp.Application.ActiveDocument

with this

doc = psApp.Documents.Add(320, 240)

everything works well if i make a new document(it will create a text layer)

i read some infos on internet it says it has something to do with UAC? based on this link https://polycount.com/discussion/84358/photoshop-python-problem

but still i cant figure it out.

Likes

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
Reply
Loading...
Jul 28, 2018 0
Guide ,
Jul 28, 2018

Copy link to clipboard

Copied

Likes

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
Reply
Loading...
Jul 28, 2018 0
New Here ,
Jul 28, 2018

Copy link to clipboard

Copied

Hi. i tried that code on that link, and i don't get any errors if the code has a create new document

docRef = docs.Add(250,33)

but when i add

psApp.Open(r"C:\Users\Kim-DEV\psd_env\psdtest.psd")

it will cause error again on the layerkind

Likes

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
Reply
Loading...
Jul 28, 2018 0
Guide ,
Jul 28, 2018

Copy link to clipboard

Copied

Sorry but I don't have Python, in C# it is the same to use 2 to set the artlayer to text though.

Likes

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
Reply
Loading...
Jul 28, 2018 0
Guide ,
Jul 28, 2018

Copy link to clipboard

Copied

Please try this...

import win32com.client

psApp = win32com.client.Dispatch("Photoshop.Application")

# Remember current unit settings and then set units to

# the value expected by this script

originalRulerUnits = psApp.Preferences.RulerUnits

psApp.Preferences.RulerUnits = 1 # 1= psPixesl, 2 = inches

docs = psApp.Documents

psApp.Open(r"C:\Users\Kim-DEV\psd_env\psdtest.psd") 

# Create a new art layer containing text

layers = psApp.activeDocument.artLayers

artLayerRef = layers.add

artLayerRef.kind = 2 #Text layer

# Set the contents of the text layer.

textItemRef = artLayerRef.TextItem

textItemRef.size = 72

textItemRef.Contents = "Hello People!"

# Restore unit setting

psApp.Preferences.RulerUnits = originalRulerUnits

Likes

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
Reply
Loading...
Jul 28, 2018 0
New Here ,
Jul 28, 2018

Copy link to clipboard

Copied

im still getting the same error.

Likes

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
Reply
Loading...
Jul 28, 2018 0
Guide ,
Jul 28, 2018

Copy link to clipboard

Copied

That is strange as I installed Python and used an open document, It created the text layer without a problem.

Likes

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
Reply
Loading...
Jul 28, 2018 0
New Here ,
Jul 28, 2018

Copy link to clipboard

Copied

i think this is something to do with UAC ?

i searched and found this one. https://polycount.com/discussion/84358/photoshop-python-problem

Likes

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
Reply
Loading...
Jul 28, 2018 0
Guide ,
Jul 28, 2018

Copy link to clipboard

Copied

Tried open an existing file and adding a text layer, no problem.

You must be doing something different.

Likes

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
Reply
Loading...
Jul 28, 2018 0
New Here ,
Jul 28, 2018

Copy link to clipboard

Copied

woah. i got it working now

this is my final code

from win32com.client import Dispatch


psApp = Dispatch("Photoshop.Application")

psApp.Open(r"C:\Users\Kim-DEV\psd_env\test1.psd"

doc = psApp.ActiveDocument

layerRef = doc.ArtLayers.Add()


psTextLayer = 2  # from enum PsLayerKind

layerRef.Kind = psTextLayer


textItem = layerRef.TextItem

textItem.Contents = "HELLO WORLD!"

textItem.Position = (120, 120)

Thank you all for the assistance and help.

Likes

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
Reply
Loading...
Jul 28, 2018 0
Adobe Community Professional ,
Jul 28, 2018

Copy link to clipboard

Copied

That's a limitation that you couldn't set 2 (so text layer) directly to kind of layer but had in first instance set it to variable 😕

And in example you used there must be mistake: doc = psApp.Application.ActiveDocument​, or how it worked for him?

Likes

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
Reply
Loading...
Jul 28, 2018 0
Hal_long LATEST
Community Beginner ,
May 28, 2020

Copy link to clipboard

Copied

Hey,

 

Maybe you can try to use the python API for photoshop.

 

 https://github.com/loonghao/photoshop-python-api

 

The docs:

https://photoshop-python-api.readthedocs.io/en/master/

 

Cheers,

Likes

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
Reply
Loading...
May 28, 2020 0