Skip to main content
laiy10580071
Inspiring
August 7, 2021
Answered

Toggle “Sample Current Layer” option of eyedroppper

  • August 7, 2021
  • 2 replies
  • 2485 views

I'm trying to create a script to toogle Eyedropper Tool's Sample option between "Current Layer" and "All Layers".

 

I search a bit and only see suggestions telling me to create two tool presets and swtich between them. It works, but I'd like to make it a bit more powerful: a single script/button that changes Eyedropper Tool's option, while keeping the Brush Tool as my active tool. Is it possible?

This topic has been closed for replies.
Correct answer jazz-y

 

#target photoshop
var s2t = stringIDToTypeID;

(ref = new ActionReference()).putProperty(s2t('property'), p = s2t('tool'));
ref.putEnumerated(s2t('application'), s2t('ordinal'), s2t('targetEnum'));
var cTool = executeActionGet(ref).getEnumerationType(p);

(selectTool = function (tool) {
    (r = new ActionReference()).putClass(tool);
    (d = new ActionDescriptor()).putReference(s2t('target'), r);
    executeAction(s2t('select'), d, DialogModes.NO);
})(s2t('eyedropperTool'));

(d = new ActionDescriptor()).putInteger(p = s2t('eyeDropperSampleSheet'),
    executeActionGet(ref).getObjectValue(s2t('currentToolOptions')).getInteger(p) ? 0 : 1);
(r = new ActionReference()).putClass(s2t('eyedropperTool'));
(d1 = new ActionDescriptor()).putReference(s2t('target'), r);
d1.putObject(s2t('to'), s2t('target'), d);
executeAction(s2t('set'), d1, DialogModes.NO);

selectTool(cTool)

 

2 replies

Participant
April 18, 2024

Sorry if I am a bit of an idiot, I am very new to adding scripts, but what actual method so you use to switch between the curent layer and all layer? is a a keyboard shortcut? If so what key is it or how to I set the key?

I'm sure this is all so super obvious to others but it has confounded me and I really need this tool! Ha

 

jazz-yCorrect answer
Legend
August 7, 2021

 

#target photoshop
var s2t = stringIDToTypeID;

(ref = new ActionReference()).putProperty(s2t('property'), p = s2t('tool'));
ref.putEnumerated(s2t('application'), s2t('ordinal'), s2t('targetEnum'));
var cTool = executeActionGet(ref).getEnumerationType(p);

(selectTool = function (tool) {
    (r = new ActionReference()).putClass(tool);
    (d = new ActionDescriptor()).putReference(s2t('target'), r);
    executeAction(s2t('select'), d, DialogModes.NO);
})(s2t('eyedropperTool'));

(d = new ActionDescriptor()).putInteger(p = s2t('eyeDropperSampleSheet'),
    executeActionGet(ref).getObjectValue(s2t('currentToolOptions')).getInteger(p) ? 0 : 1);
(r = new ActionReference()).putClass(s2t('eyedropperTool'));
(d1 = new ActionDescriptor()).putReference(s2t('target'), r);
d1.putObject(s2t('to'), s2t('target'), d);
executeAction(s2t('set'), d1, DialogModes.NO);

selectTool(cTool)

 

Stephen Marsh
Community Expert
Community Expert
August 7, 2021

@jazz-y – Great job as always! I added a couple of alerts to your code for visibility, however, I'm guessing that this may "interrupt the painting flow".

 

#target photoshop
var s2t = stringIDToTypeID;

(ref = new ActionReference()).putProperty(s2t('property'), p = s2t('tool'));
ref.putEnumerated(s2t('application'), s2t('ordinal'), s2t('targetEnum'));
var cTool = executeActionGet(ref).getEnumerationType(p);

(selectTool = function (tool) {
    (r = new ActionReference()).putClass(tool);
    (d = new ActionDescriptor()).putReference(s2t('target'), r);
    executeAction(s2t('select'), d, DialogModes.NO);
})(s2t('eyedropperTool'));

(d = new ActionDescriptor()).putInteger(p = s2t('eyeDropperSampleSheet'),
    executeActionGet(ref).getObjectValue(s2t('currentToolOptions')).getInteger(p) ? 0 : 1);
(r = new ActionReference()).putClass(s2t('eyedropperTool'));
(d1 = new ActionDescriptor()).putReference(s2t('target'), r);
d1.putObject(s2t('to'), s2t('target'), d);
executeAction(s2t('set'), d1, DialogModes.NO);

// Alerts //
if (executeActionGet(ref).getObjectValue(s2t('currentToolOptions')).getInteger(p) === 0) {
    alert('Sample from "All Layers" selected');
}
else if (executeActionGet(ref).getObjectValue(s2t('currentToolOptions')).getInteger(p) === 1) {
    alert('Sample from "Current Layer" selected');
}
////////////

selectTool(cTool)
Legend
August 7, 2021

I was already afraid that switching between tools would be too slow and would interrupt drawing 🙂 Although on my computer the script runs in about 200 milliseconds