Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(action_manager): Action Manager module integration #184

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions examples/apply_crystallize_filter_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@
References:
https://github.com/lohriialo/photoshop-scripting-python/blob/master/ApplyCrystallizeFilterAction.py

Now the submodule action_manager provides a more friendly way to deal with action manager.
Read document for details.
"""

# Import third-party modules
import examples._psd_files as psd # Import from examples.

# Import local modules
from photoshop import Session
import photoshop.api.action_manager as am


PSD_FILE = psd.get_psd_files()
Expand All @@ -28,13 +31,9 @@
active_document.activeLayer = active_document.layerSets.item(len(nLayerSets)).artLayers.item(len(nArtLayers))

def applyCrystallize(cellSize):
cellSizeID = ps.app.CharIDToTypeID("ClSz")
eventCrystallizeID = ps.app.CharIDToTypeID("Crst")

filterDescriptor = ps.ActionDescriptor
filterDescriptor.putInteger(cellSizeID, cellSize)

ps.app.executeAction(eventCrystallizeID, filterDescriptor)
filter_dict = {"_classID": None, "ClSz": cellSize}
filter_desc = ps.ActionDescriptor.load(filter_dict)
ps.app.executeAction(am.str2id("Crst"), filter_desc)

applyCrystallize(25)
print("Apply crystallize done.")
20 changes: 13 additions & 7 deletions examples/convert_smartobject_to_layer.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
"""Convert Smart object to artLayer."""

# Import built-in modules
from textwrap import dedent

# Import local modules
from photoshop import Session
import photoshop.api.action_manager as am


# example 1
with Session() as ps:
js = """
var idplacedLayerConvertToLayers = stringIDToTypeID( "placedLayerConvertToLayers" );
executeAction( idplacedLayerConvertToLayers, undefined, DialogModes.NO );
"""
js = dedent(
"""
var idplacedLayerConvertToLayers = stringIDToTypeID( "placedLayerConvertToLayers" );
executeAction( idplacedLayerConvertToLayers, undefined, DialogModes.NO );
"""
)
ps.app.doJavaScript(js)

# example 2
with Session() as ps:
descriptor = ps.ActionDescriptor
idplacedLayerConvertToLayers = ps.app.stringIDToTypeID("placedLayerConvertToLayers")
ps.app.executeAction(idplacedLayerConvertToLayers, descriptor)
# The event id used in executeAction differs across Photoshop versions.
# Look up eventid list or record with ScriptListener Plug-in to decide which eventid to use.
ps.app.executeAction(am.str2id("placedLayerConvertToLayers"), None)
82 changes: 34 additions & 48 deletions examples/emboss_action.py
Original file line number Diff line number Diff line change
@@ -1,62 +1,48 @@
# Import local modules
from photoshop import Session
import photoshop.api.action_manager as am


with Session() as ps:
app = ps.app
for index, x in enumerate(range(50)):
# Execute an existing action from action palette.
idPly = app.charIDToTypeID("Ply ")
desc8 = ps.ActionDescriptor()
idnull = app.charIDToTypeID("null")
ref3 = ps.ActionReference()
idActn = app.charIDToTypeID("Actn")
ref3.putName(idActn, "Sepia Toning (layer)")
idASet = app.charIDToTypeID("ASet")
ref3.PutName(idASet, "Default Actions")
desc8.putReference(idnull, ref3)
app.executeAction(idPly, desc8, ps.DialogModes.DisplayNoDialogs)
exec_dict = {
"_classID": None,
"null": [
"!ref",
am.ReferenceKey(desiredclass="action", value="Sepia Toning (layer)"),
am.ReferenceKey(desiredclass="actionSet", value="Default Actions"),
],
}
exec_desc = ps.ActionDescriptor.load(exec_dict)
app.executeAction(am.str2id("Ply "), exec_desc, ps.DialogModes.DisplayNoDialogs)

# Create solid color fill layer.
idMk = app.charIDToTypeID("Mk ")
desc21 = ps.ActionDescriptor()
idNull = app.charIDToTypeID("null")
ref12 = ps.ActionReference()
idContentLayer1 = app.stringIDToTypeID("contentLayer")
ref12.putClass(idContentLayer1)
desc21.putReference(idNull, ref12)
idUsng = app.charIDToTypeID("Usng")
desc22 = ps.ActionDescriptor()
idType = app.charIDToTypeID("Type")
desc23 = ps.ActionDescriptor()
idClr = app.charIDToTypeID("Clr ")
desc24 = ps.ActionDescriptor()
idRd = app.charIDToTypeID("Rd ")
desc24.putDouble(idRd, index)
idGrn = app.charIDToTypeID("Grn ")
desc24.putDouble(idGrn, index)
idBl = app.charIDToTypeID("Bl ")
desc24.putDouble(idBl, index)
idRGBC = app.charIDToTypeID("RGBC")
desc23.putObject(idClr, idRGBC, desc24)
idSolidColorLayer = app.StringIDToTypeID("solidColorLayer")
desc22.putObject(idType, idSolidColorLayer, desc23)
idContentLayer2 = app.StringIDToTypeID("contentLayer")
desc21.putObject(idUsng, idContentLayer2, desc22)
app.executeAction(idMk, desc21, ps.DialogModes.DisplayNoDialogs)
filledlayer_dict = {
"_classID": None,
"null": ["!ref", am.ReferenceKey(desiredclass="contentLayer", value=None)],
"using": {
"_classID": "contentLayer",
"type": {
"_classID": "solidColorLayer",
"color": {"_classID": "RGBColor", "red": index, "grain": index, "blue": index}, # noqa
},
},
}
filledlayer_desc = ps.ActionDescriptor.load(filledlayer_dict)
app.executeAction(am.str2id("Mk "), filledlayer_desc, ps.DialogModes.DisplayNoDialogs)

# Select mask.
idSlct = app.charIDToTypeID("slct")
desc38 = ps.ActionDescriptor()
idNull1 = app.charIDToTypeID("null")
ref20 = ps.ActionReference()
idChnl1 = app.charIDToTypeID("Chnl")
idChnl2 = app.charIDToTypeID("Chnl")
idMsk = app.charIDToTypeID("Msk ")
ref20.putEnumerated(idChnl1, idChnl2, idMsk)
desc38.putReference(idNull1, ref20)
idMkVs = app.charIDToTypeID("MkVs")
desc38.putBoolean(idMkVs, False)
app.executeAction(idSlct, desc38, ps.DialogModes.DisplayNoDialogs)
selectmask_dict = {
"_classID": None,
"null": [
"!ref",
am.ReferenceKey(desiredclass="channel", value=am.Enumerated(type="channel", value="mask")),
],
"makeVisible": False,
}
selectmask_desc = ps.ActionDescriptor.load(selectmask_dict)
app.executeAction(am.str2id("slct"), selectmask_desc, ps.DialogModes.DisplayNoDialogs)

app.activeDocument.activeLayer.invert()
2 changes: 1 addition & 1 deletion examples/enable_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@

with Session() as ps:
plugin_name = "generator-assets-dummy-menu"
generatorDesc = ps.ActionDescriptor
generatorDesc = ps.ActionDescriptor()
generatorDesc.putString(ps.app.stringIDToTypeID("name"), plugin_name)
ps.app.executeAction(ps.app.stringIDToTypeID("generateAssets"), generatorDesc)
28 changes: 24 additions & 4 deletions examples/import_image_as_layer.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
"""Import a image as a artLayer."""

# Import built-in modules
import pathlib

# Import local modules
from photoshop import Session
import photoshop.api.action_manager as am


def importfile(app, path: pathlib.WindowsPath, position: (float, float), size: (float, float)): # noqa
px, py = position
sx, sy = size
import_dict = {
"null": path,
"freeTransformCenterState": am.Enumerated(type="quadCenterState", value="QCSAverage"),
"offset": {
"horizontal": am.UnitDouble(unit="distanceUnit", double=px),
"vertical": am.UnitDouble(unit="distanceUnit", double=py),
"_classID": "offset",
},
"width": am.UnitDouble(unit="percentUnit", double=sx),
"height": am.UnitDouble(unit="percentUnit", double=sy),
"_classID": None,
}
import_desc = ps.ActionDescriptor.load(import_dict)
app.executeAction(am.str2id("placeEvent"), import_desc)


with Session(action="new_document") as ps:
desc = ps.ActionDescriptor
desc.putPath(ps.app.charIDToTypeID("null"), "your/image/path")
event_id = ps.app.charIDToTypeID("Plc ") # `Plc` need one space in here.
ps.app.executeAction(ps.app.charIDToTypeID("Plc "), desc)
importfile(ps.app, pathlib.Path("your/image/path"), (-100, 456), (4470, 4463))
13 changes: 7 additions & 6 deletions examples/replace_images.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
"""Replace the image of the current active layer with a new image."""

# Import built-in modules
import pathlib

# Import third-party modules
import examples._psd_files as psd # Import from examples.

# Import local modules
from photoshop import Session
import photoshop.api.action_manager as am


PSD_FILE = psd.get_psd_files()
Expand All @@ -14,12 +18,9 @@
active_layer = ps.active_document.activeLayer
bounds = active_layer.bounds
print(f"current layer {active_layer.name}: {bounds}")
input_file = PSD_FILE["red_100x200.png"]
replace_contents = ps.app.stringIDToTypeID("placedLayerReplaceContents")
desc = ps.ActionDescriptor
idnull = ps.app.charIDToTypeID("null")
desc.putPath(idnull, input_file)
ps.app.executeAction(replace_contents, desc)
input_dict = {"_classID": None, "null": pathlib.Path(PSD_FILE["red_100x200.png"])}
input_desc = ps.ActionDescriptor.load(input_dict)
ps.app.executeAction(am.str2id("placedLayerReplaceContents"), input_desc)

# replaced image.
active_layer = ps.active_document.activeLayer
Expand Down
41 changes: 11 additions & 30 deletions examples/session_smart_sharpen.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

# Import local modules
from photoshop import Session
import photoshop.api.action_manager as am


PSD_FILE = psd.get_psd_files()
Expand All @@ -19,35 +20,15 @@
with Session(file_path, action="open") as ps:

def SmartSharpen(inAmount, inRadius, inNoise):
idsmart_sharpen_id = ps.app.stringIDToTypeID(ps.EventID.SmartSharpen)
desc37 = ps.ActionDescriptor()

idpresetKind = ps.app.stringIDToTypeID(ps.EventID.PresetKind)
idpresetKindType = ps.app.stringIDToTypeID(ps.EventID.PresetKindType)
idpresetKindCustom = ps.app.stringIDToTypeID(ps.EventID.PresetKindCustom)
desc37.putEnumerated(idpresetKind, idpresetKindType, idpresetKindCustom)
idAmnt = ps.app.charIDToTypeID("Amnt")
idPrc = ps.app.charIDToTypeID("Rds ")
desc37.putUnitDouble(idAmnt, idPrc, inAmount)

idRds = ps.app.charIDToTypeID("Rds ")
idPxl = ps.app.charIDToTypeID("#Pxl")
desc37.putUnitDouble(idRds, idPxl, inRadius)

idnoiseReduction = ps.app.stringIDToTypeID("noiseReduction")
idPrc = ps.app.charIDToTypeID("#Prc")
desc37.putUnitDouble(idnoiseReduction, idPrc, inNoise)

idblur = ps.app.charIDToTypeID("blur")
idblurType = ps.app.stringIDToTypeID("blurType")
idGsnB = ps.app.charIDToTypeID("GsnB")
desc37.putEnumerated(idblur, idblurType, idGsnB)

ps.app.ExecuteAction(idsmart_sharpen_id, desc37)

docRef = ps.active_document
nlayerSets = docRef.layerSets
nArtLayers = docRef.layerSets.item(nlayerSets.length)
docRef.activeLayer = nArtLayers.artLayers.item(nArtLayers.artLayers.length)
ss_dict = {
"_classID": None,
"presetKindType": am.Enumerated(type="presetKindType", value="presetKindCustom"), # noqa
"amount": am.UnitDouble(unit="radius", double=inAmount),
"radius": am.UnitDouble(unit="pixelsUnit", double=inRadius),
"noiseReduction": am.UnitDouble(unit="percentUnit", double=inNoise),
"blur": am.Enumerated(type="blurType", value="gaussianBlur"),
}
ss_desc = ps.ActionDescriptor.load(ss_dict)
ps.app.ExecuteAction(am.str2id("smartSharpen"), ss_desc)

SmartSharpen(300, 2.0, 20)
37 changes: 11 additions & 26 deletions examples/smart_sharpen.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

# Import local modules
import photoshop.api as ps
import photoshop.api.action_manager as am


app = ps.Application()
Expand All @@ -25,32 +26,16 @@


def SmartSharpen(inAmount, inRadius, inNoise):
idsmart_sharpen_id = app.stringIDToTypeID(ps.EventID.SmartSharpen)
desc37 = ps.ActionDescriptor()

idpresetKind = app.stringIDToTypeID(ps.EventID.PresetKind)
idpresetKindType = app.stringIDToTypeID(ps.EventID.PresetKindType)
idpresetKindCustom = app.stringIDToTypeID(ps.EventID.PresetKindCustom)
desc37.putEnumerated(idpresetKind, idpresetKindType, idpresetKindCustom)

idAmnt = app.charIDToTypeID("Amnt")
idPrc = app.charIDToTypeID("Rds ")
desc37.putUnitDouble(idAmnt, idPrc, inAmount)

idRds = app.charIDToTypeID("Rds ")
idPxl = app.charIDToTypeID("#Pxl")
desc37.putUnitDouble(idRds, idPxl, inRadius)

idnoiseReduction = app.stringIDToTypeID("noiseReduction")
idPrc = app.charIDToTypeID("#Prc")
desc37.putUnitDouble(idnoiseReduction, idPrc, inNoise)

idblur = app.charIDToTypeID("blur")
idblurType = app.stringIDToTypeID("blurType")
idGsnB = app.charIDToTypeID("GsnB")
desc37.putEnumerated(idblur, idblurType, idGsnB)

app.ExecuteAction(idsmart_sharpen_id, desc37)
ss_dict = {
"_classID": None,
"presetKindType": am.Enumerated(type="presetKindType", value="presetKindCustom"), # noqa
"amount": am.UnitDouble(unit="radius", double=inAmount),
"radius": am.UnitDouble(unit="pixelsUnit", double=inRadius),
"noiseReduction": am.UnitDouble(unit="percentUnit", double=inNoise),
"blur": am.Enumerated(type="blurType", value="gaussianBlur"),
}
ss_desc = ps.ActionDescriptor.load(ss_dict)
app.ExecuteAction(am.str2id("smartSharpen"), ss_desc)


SmartSharpen(300, 2.0, 20)
6 changes: 3 additions & 3 deletions photoshop/api/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"""Python API for Photoshop."""
# Import local modules
from photoshop.api import constants
from photoshop.api.action_descriptor import ActionDescriptor
from photoshop.api.action_list import ActionList
from photoshop.api.action_reference import ActionReference
from photoshop.api._actionmanager_type_binder import ActionDescriptor
from photoshop.api._actionmanager_type_binder import ActionList
from photoshop.api._actionmanager_type_binder import ActionReference
from photoshop.api.application import Application
from photoshop.api.batch_options import BatchOptions
from photoshop.api.colors import CMYKColor
Expand Down
Loading