skip to Main Content

How does one get / set the current tool using Javascript in Photoshop?

#target photoshop

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

4

Answers


  1. Chosen as BEST ANSWER

    I found a solution, but I wish it was simpler. Basically I'm using this to select the brush tool and toggle the eraser tool, using only one button. That way I only have to use one button on my Wacom Tablet.

    #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
    

  2. It’s simpler in AppleScript. This is my little library script that I use with my 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
    
    
    Login or Signup to reply.
  3. you can try to use photoshop-python-api

    from photoshop import Session
    
    with Session() as ps:
        print(ps.app.currentTool)
    

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

    Login or Signup to reply.
  4. Old question, but still relevant so I wanted to simplify it further. The OP was on the right track with the code, it needed only the necessary string values for various tools, which were provided by @skibulk above.

    According to the Photoshop Javascript Reference PDF, currentTool is a read-write string property of the Application object. This means we can get the value of the current tool, but also set the value of the current tool.

    #target photoshop
    
    if (app.currentTool == "paintbrushTool") {
        app.currentTool  = "eraserTool";
    } else {
        app.currentTool  = "paintbrushTool";
    }
    
    // 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
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search