Photoshop Javascript:如何获取/设置当前工具?
Photoshop Javascript: How to get / set current tool?
如何在 Photoshop 中使用 Javascript 获取/设置当前工具?
#target photoshop
if (app.currentTool == BRUSH_TOOL) {
app.currentTool = ERASE_TOOL;
} else {
app.currentTool = BRUSH_TOOL;
}
我找到了解决方案,但我希望它更简单。基本上,我只使用一个按钮将其用于 select 画笔工具和切换橡皮擦工具。这样一来,我只需在我的 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
在 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
你可以试试photoshop-python-api
from photoshop import Session
with Session() as ps:
print(ps.app.currentTool)
如何在 Photoshop 中使用 Javascript 获取/设置当前工具?
#target photoshop
if (app.currentTool == BRUSH_TOOL) {
app.currentTool = ERASE_TOOL;
} else {
app.currentTool = BRUSH_TOOL;
}
我找到了解决方案,但我希望它更简单。基本上,我只使用一个按钮将其用于 select 画笔工具和切换橡皮擦工具。这样一来,我只需在我的 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
在 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
你可以试试photoshop-python-api
from photoshop import Session
with Session() as ps:
print(ps.app.currentTool)