Creating Guides in a Photoshop document using Python- Part Two

Well. That was actually a lot quicker than I expected. Although the initial code looks like junk, its actually pretty easy to read if you know what you are looking for. Here is my very rapid Python translation that works exactly the same way as the VBS output.

#Python translation... and go!
import comtypes.client

#Create a reference to the PS app
psApp = comtypes.client.CreateObject('Photoshop.Application')

#Create a ref to the dialog mode. Use mode 3 to use no dialog
dialogMode = 3

#Start doing crazy shiz
id43 = psApp.CharIDToTypeID( "Mk  " )

desc7 = comtypes.client.CreateObject( "Photoshop.ActionDescriptor" )

#I assume "Nw  " means... no idea. No worries?
id44 = psApp.CharIDToTypeID( "Nw  " )

desc8 = comtypes.client.CreateObject( "Photoshop.ActionDescriptor" )

#This is obviously the resulting position, in pixels, followed by a measuement. 
id45 = psApp.CharIDToTypeID( "Pstn" )
id46 = psApp.CharIDToTypeID( "#Pxl" )
desc8.PutUnitDouble( id45, id46, 256.000000 )

#The orientation of the guideline, In this case 'Vrtc' or 'Vertical'
id47 = psApp.CharIDToTypeID( "Ornt" )
id48 = psApp.CharIDToTypeID( "Ornt" )
id49 = psApp.CharIDToTypeID( "Vrtc" )

#Call the Action descriptor with the Orientation
desc8.PutEnumerated( id47, id48, id49 )

#This is where we call the guide type
id50 = psApp.CharIDToTypeID( "Gd  " )

#Now assemble "Nw  " and "Gd  " as an action
desc7.PutObject( id44, id50, desc8 )

#Finally, tell Photoshop to execute "Mk  " with the action described and no dialog
psApp.ExecuteAction( id43, desc7, dialogMode )

#Done!

So now its pretty straight forward to experiment with simplifying it as much as possible and encapsulating it as a callable function. The most exciting part is that now, knowing how to get into the guts of it, I imagine that the scripting listener gives access to just about every single function in PS. Pretty cool!

No comments:

Post a Comment

Comments?