Photoshop Javascript: как получить/установить текущий инструмент?

Как получить/установить текущий инструмент с помощью Javascript в Photoshop?

#target photoshop

if (app.currentTool == BRUSH_TOOL) {
    app.currentTool  = ERASE_TOOL;
} else {
    app.currentTool  = BRUSH_TOOL;
}

person skibulk    schedule 17.03.2015    source источник


Ответы (3)


Я нашел решение, но хотелось бы, чтобы оно было проще. В основном я использую это, чтобы выбрать инструмент кисти и переключить инструмент ластика, используя только одну кнопку. Таким образом, мне нужно использовать только одну кнопку на планшете Wacom.

#target photoshop

if (getTool() == 'paintbrushTool') {
    setTool('eraserTool');
} else {
    setTool('paintbrushTool');
}

// https://forums.adobe.com/thread/579195
function getTool(){  
    var ref = new ActionReference();   
    ref.putEnumerated( charIDToTypeID("capp"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );   
    var cTool = typeIDToStringID(executeActionGet(ref).getEnumerationType(stringIDToTypeID('tool')));  
    return cTool;  
}

// https://www.ps-scripts.com/viewtopic.php?f=68&t=11342&p=152772
function setTool(tool) {
    var desc9 = new ActionDescriptor();
    var ref7 = new ActionReference();
    ref7.putClass( app.stringIDToTypeID(tool) );
    desc9.putReference( app.charIDToTypeID('null'), ref7 );
    executeAction( app.charIDToTypeID('slct'), desc9, DialogModes.NO );
}
// Tool names (use quoted strings, e.g. 'moveTool')
// moveTool
// marqueeRectTool
// marqueeEllipTool
// marqueeSingleRowTool
// marqueeSingleColumnTool
// lassoTool
// polySelTool
// magneticLassoTool
// quickSelectTool
// magicWandTool
// cropTool
// sliceTool
// sliceSelectTool
// spotHealingBrushTool
// magicStampTool
// patchSelection
// redEyeTool
// paintbrushTool
// pencilTool
// colorReplacementBrushTool
// cloneStampTool
// patternStampTool
// historyBrushTool
// artBrushTool
// eraserTool
// backgroundEraserTool
// magicEraserTool
// gradientTool
// bucketTool
// blurTool
// sharpenTool
// smudgeTool
// dodgeTool
// burnInTool
// saturationTool
// penTool
// freeformPenTool
// addKnotTool
// deleteKnotTool
// convertKnotTool
// typeCreateOrEditTool
// typeVerticalCreateOrEditTool
// typeCreateMaskTool
// typeVerticalCreateMaskTool
// pathComponentSelectTool
// directSelectTool
// rectangleTool
// roundedRectangleTool
// ellipseTool
// polygonTool
// lineTool
// customShapeTool
// textAnnotTool
// soundAnnotTool
// eyedropperTool
// colorSamplerTool
// rulerTool
// handTool
// zoomTool
person skibulk    schedule 17.03.2015
comment
Вы можете использовать app.currentTool, чтобы получить текущий инструмент - person noon; 11.12.2018

В AppleScript это проще. Это мой небольшой библиотечный скрипт, который я использую со своим Wacom:

-- put into ~/Library/Script\ Libraries
-- use as set currentTool to script "Photoshop_ScriptLibrary"'s toggleCurrentTool("lassoTool", "eraserTool")
on writeVar(theName, theVar)
    do shell script "echo " & theVar & " > /tmp/" & theName & ".txt"
end writeVar

on readVar(theName)
    try
        return do shell script "cat /tmp/" & theName & ".txt"
    end try
    return ""
end readVar

on getGroupPath()
    set thelayer to ""
    tell application "Adobe Photoshop CC 2019" to tell the current document
        set thelayer to current layer
    end tell
    return thelayer
end getGroupPath

on getCurrentTool()
    tell application "Adobe Photoshop CC 2019" to return current tool
end getCurrentTool

on deselect()
    tell application "Adobe Photoshop CC 2019" to tell the current document to deselect
    setFeathered(false)
end deselect

on toggleCurrentTool(tool1, tool2)
    setFeathered(false)
    set currentTool to tool1
    tell application "Adobe Photoshop CC 2019"
        if current tool is equal to currentTool then set currentTool to tool2
        set current tool to currentTool
    end tell
    return currentTool
end toggleCurrentTool

on getAllLayers(layerName)
    set allLayerNames to {}
    tell application "Adobe Photoshop CC 2019"
        set allLayers to the layers of current document
        repeat with thelayer in allLayers
            set theName to the name of thelayer
            if theName starts with the first word of layerName then set end of allLayerNames to theName
        end repeat
    end tell
    return allLayerNames
end getAllLayers


on getPrevLayer(targetLayerName, currentLayer)
    set currentLayerId to id of currentLayer
    tell application "Adobe Photoshop CC 2019"
        return the first art layer of the current document whose name is targetLayerName and id is not currentLayerId
        return the first art layer of the current document whose name is targetLayerName
    end tell
end getPrevLayer

on getPrevious(layerName)

    set suffix to the last word of layerName
    set suffixlength to get (length of suffix) + 2
    log "getPrevious(" & layerName & "), suffix: " & suffix

    -- remove the last word from the layer name. Unless we're dealing with numbered copies
    set targetLayerName to (text 1 thru (-1 * suffixlength)) of layerName

    -- first: Check if layer name ends in number. If number > 2, we want the layer with the next-smaller number
    -- WIP copy 16 -> WIP copy 15
    -- WIP copy 1 -> WIP copy 
    -- second: If the layer does not end in a number, 
    try
        set thenumber to (suffix as number)
        if thenumber is greater than 2 then set targetLayerName to targetLayerName & " " & thenumber - 1
    on error -- if layer doesn't end in a number: remove "copy"
        if layerName ends with "copy" then set targetLayerName to text 1 thru -6 of layerName
    end try
    return targetLayerName

end getPrevious


on getPreviousLayer(currentLayer)
    return getPrevLayer((getPrevious(name of currentLayer)), currentLayer)
end getPreviousLayer

on getFeathered()
    return "true" is equal to readVar("feathered")
end getFeathered

on setFeathered(b)
    writeVar("feathered", b)
end setFeathered

on isOnLayerMask()
    try
        set windowName to ""
        tell application "System Events" to tell process "Adobe Photoshop CC 2019"
            tell (1st window whose value of attribute "AXMain" is true)
                set windowName to value of attribute "AXTitle"
            end tell
        end tell
    end try
    return windowName contains "Layer Mask"
end isOnLayerMask

on isSelected()
    try
        tell application "Adobe Photoshop CC 2019" to tell current document
            set props to properties of selection
            if bounds of props is not equal to missing value then return true
        end tell
    end try
    return false
end isSelected

on tryFeather()
    if getFeathered() or not isSelected() then return
    try
        tell application "Adobe Photoshop CC 2019" to tell current document
            feather selection by 5
        end tell
        setFeathered(true)
    on error
        setFeathered(false)
    end try
end tryFeather


on tryBlur(_radius)

    if isSelected() then
        try
            tell application "Adobe Photoshop CC 2019" to tell current document
                filter current layer using gaussian blur with options {radius:_radius}
            end tell
        end try
    end if
end tryBlur

person manavortex    schedule 18.10.2019
comment
Ваш сценарий выглядит так же, как принятый, но с использованием Apple Script. Я думаю, это проще, только если вы знаете КАК :) - person Sergey Kritskiy; 18.10.2019
comment
Ну, такие вещи, как ref.putEnumerated( charIDToTypeID("capp"),, меня пугают, поэтому я решил просто выложить это здесь на случай, если кто-нибудь еще погуглит об этом :) - person manavortex; 19.10.2019
comment
Справедливо! Вы правы, CharID может быть пугающим - person Sergey Kritskiy; 19.10.2019

вы можете попробовать использовать photoshop-python-api

from photoshop import Session

with Session() as ps:
    print(ps.app.currentTool)

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

person Hal    schedule 21.04.2020