C# and controlling layers

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

moongirl

C# and controlling layers

Post by moongirl »

Hi, I am trying to convert some code from the scriptListenerVB.log file into C# so that I can control the visibility of layers in a PSD document from a C# application. I am struggling to understand the scriptListener file though and wondered if anyone had any pointers? The scriptlistener code is:
Code: Select all
REM =======================================================
DIM objApp
SET objApp = CreateObject("Photoshop.Application")
REM Use dialog mode 3 for show no dialogs
DIM dialogMode
dialogMode = 3
DIM id1
id1 = objApp.CharIDToTypeID( "Opn " )
    DIM desc1
    SET desc1 = CreateObject( "Photoshop.ActionDescriptor" )
    DIM id2
    id2 = objApp.CharIDToTypeID( "null" )
    Call desc1.PutPath( id2, "C:\\Inetpub\\wwwroot\\jockeySilks\\dataIn\\test.psd" )
Call objApp.ExecuteAction( id1, desc1, dialogMode )

REM =======================================================
DIM objApp
SET objApp = CreateObject("Photoshop.Application")
REM Use dialog mode 3 for show no dialogs
DIM dialogMode
dialogMode = 3
DIM id3
id3 = objApp.CharIDToTypeID( "Shw " )
    DIM desc2
    SET desc2 = CreateObject( "Photoshop.ActionDescriptor" )
    DIM id4
    id4 = objApp.CharIDToTypeID( "null" )
        DIM list1
        SET list1 = CreateObject( "Photoshop.ActionList" )
            DIM ref1
            SET ref1 = CreateObject( "Photoshop.ActionReference" )
            DIM id5
            id5 = objApp.CharIDToTypeID( "Lyr " )
            DIM id6
            id6 = objApp.CharIDToTypeID( "Ordn" )
            DIM id7
            id7 = objApp.CharIDToTypeID( "Trgt" )
            Call ref1.PutEnumerated( id5, id6, id7 )
        Call list1.PutReference( ref1 )
    Call desc2.PutList( id4, list1 )
Call objApp.ExecuteAction( id3, desc2, dialogMode )

REM =======================================================
DIM objApp
SET objApp = CreateObject("Photoshop.Application")
REM Use dialog mode 3 for show no dialogs
DIM dialogMode
dialogMode = 3
DIM id8
id8 = objApp.CharIDToTypeID( "Dslc" )
    DIM desc3
    SET desc3 = CreateObject( "Photoshop.ActionDescriptor" )
    DIM id9
    id9 = objApp.CharIDToTypeID( "null" )
        DIM ref2
        SET ref2 = CreateObject( "Photoshop.ActionReference" )
        DIM id10
        id10 = objApp.CharIDToTypeID( "Path" )
        Call ref2.PutClass( id10 )
    Call desc3.PutReference( id9, ref2 )
Call objApp.ExecuteAction( id8, desc3, dialogMode )

REM =======================================================
DIM objApp
SET objApp = CreateObject("Photoshop.Application")
REM Use dialog mode 3 for show no dialogs
DIM dialogMode
dialogMode = 3
DIM id11
id11 = objApp.CharIDToTypeID( "Hd  " )
    DIM desc4
    SET desc4 = CreateObject( "Photoshop.ActionDescriptor" )
    DIM id12
    id12 = objApp.CharIDToTypeID( "null" )
        DIM list2
        SET list2 = CreateObject( "Photoshop.ActionList" )
            DIM ref3
            SET ref3 = CreateObject( "Photoshop.ActionReference" )
            DIM id13
            id13 = objApp.CharIDToTypeID( "Lyr " )
            Call ref3.PutName( id13, "blue_circle" )
        Call list2.PutReference( ref3 )
    Call desc4.PutList( id12, list2 )
Call objApp.ExecuteAction( id11, desc4, dialogMode )

REM =======================================================
DIM objApp
SET objApp = CreateObject("Photoshop.Application")
REM Use dialog mode 3 for show no dialogs
DIM dialogMode
dialogMode = 3
DIM id14
id14 = objApp.CharIDToTypeID( "Cls " )
    DIM desc5
    SET desc5 = CreateObject( "Photoshop.ActionDescriptor" )
    DIM id15
    id15 = objApp.CharIDToTypeID( "Svng" )
    DIM id16
    id16 = objApp.CharIDToTypeID( "YsN " )
    DIM id17
    id17 = objApp.CharIDToTypeID( "N   " )
    Call desc5.PutEnumerated( id15, id16, id17 )
Call objApp.ExecuteAction( id14, desc5, dialogMode )




Does anyone have any C# (or VB.Net) code that changes the visibility of a layer to help me out?

Thanks! Any help greatly appreciated!
Larry Ligon

C# and controlling layers

Post by Larry Ligon »

Create a document with at least 4 layers. This code will first make all the layers invisible. It will then make one layer visible. The layers are indexed from zero, so the layer closest to the background will have an index of 0.

This is Visual Basic 6 code:

Code: Select allDim appRef As Photoshop.Application
Dim docRef As Photoshop.Document
Dim artLayerRef As Photoshop.ArtLayer

Set appRef = CreateObject("Photoshop.Application")
If (appRef.Documents.Count > 0) Then
    Set docRef = appRef.ActiveDocument
    'Make all layers invisible
    For Each artLayerRef In docRef.ArtLayers
        artLayerRef.Visible = False
    Next
    'Now turn on just one layer.  Layers indexed from zero
    docRef.ArtLayers(2).Visible = True
End If

MsgBox "ArtLayer 2 is now visible"

I hope this helps,
Larry