Copy link to clipboard
Copied
Hi,
I am trying to create a script which simply pops open the "Select > Save Selection" window.
I need this to work in any document and on any layer. After researching basic scripting, I came across the script below, however, it throws an error, stating that a semicolon was expected?
Is additional code needed?
Any help would be greatly appreciated.
Thank you all in advance.
var docRef = app.activeDocument;
var chanRef:Channel = docRef.channels.add();
chanRef.name = "New Selection";
chanRef.kind = ChannelType.SELECTEDAREA;
docRef.selection.store( docRef.channels.getByName("New Selection"), SelectionType.EXTEND );
Copy link to clipboard
Copied
To do the most likely you would need to use action manager code that shows the dialog.
I save selection in one of my scripts without the dialog showing here the action manager code you would need to turn on the dialog
function saveAlpha(alphaName){
var idDplc = charIDToTypeID( "Dplc" );
var desc27 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref11 = new ActionReference();
var idChnl = charIDToTypeID( "Chnl" );
var idfsel = charIDToTypeID( "fsel" );
ref11.putProperty( idChnl, idfsel );
desc27.putReference( idnull, ref11 );
var idNm = charIDToTypeID( "Nm " );
desc27.putString( idNm, alphaName );
try {
executeAction( idDplc, desc27, DialogModes.NO );
}
catch(e) {}
}
Copy link to clipboard
Copied
What about this?
var docRef = app.activeDocument;
var chanRef = docRef.channels.add();
chanRef.name = "New Selection";
chanRef.kind = ChannelType.SELECTEDAREA;
docRef.selection.store( docRef.channels.getByName("New Selection"), SelectionType.EXTEND );
Copy link to clipboard
Copied
Thanks gentlemen, I will look into your answers as soon as I get a spare 5 minutes.
Very much appreciated!
Copy link to clipboard
Copied
OK - I just tried both versions:
JJMack - the code you kindly posted did not work (no channel created).
Tomas - the modified code which you provided creates a channel, but for some strange reason it is inverted (the selection in the channel is black on a white background - I would have expected the selection to appear white on a black background).
Any idea how to invert the selection?
Thanks.
Copy link to clipboard
Copied
OK, let's try inverted then.
var docRef = app.activeDocument;
docRef.selection.invert();
var chanRef = docRef.channels.add();
chanRef.name = "New Selection";
chanRef.kind = ChannelType.SELECTEDAREA;
docRef.selection.store( docRef.channels.getByName("New Selection"), SelectionType.REPLACE );
Copy link to clipboard
Copied
The code is acting erratic.
I tried the original code which Tomas modified (without the invert addition) and although it seems to create the alpha channel inverted in the channels panel (black selection on a white background) - it works as expected when the channel is loaded.
However - the only problem now is that it deselects all the other channels, so that ONLY the newly created channel is visible?
Very strange.
Copy link to clipboard
Copied
Hi Ric. I am sorry I dont understand what you need then.
Can you explain more clearly or paste a few screenshots?
Copy link to clipboard
Copied
OK.......
This is the code I currently have:
var docRef = app.activeDocument;
var chanRef = docRef.channels.add();
chanRef.name = "New Selection";
chanRef.kind = ChannelType.SELECTEDAREA;
docRef.selection.store( docRef.channels.getByName("New Selection"), SelectionType.EXTEND );
This code saves my selection as an alpha channel, but with 2 strange side effects:
1) The channel in the channels panel looks inverted (black selection on white background - the opposite as expected) BUT, when the selection is loaded - it works as expected.
2) The visibility of the other 4 default channels (RGB, R, G, B) all get hidden / deactivated (eye icon turned off).
Hope that makes it clearer
Thanks.
Copy link to clipboard
Copied
1. Well, second code produces same results as the first one, except alpha channel is inverted, correcT?
2. Also what do you mean by "BUT, when the selection is loaded - it works as expected."? I don't follow.
3. Referring to your #2 - you want RGB channels to be selected, except the newly created channel?
Please post a screenshot of expected result.
Copy link to clipboard
Copied
Tomas,
yes - I would need the 4 default channels to be selected, and the newly created channel deselected, (the same as what happens when you do this via "Select > Save Selection" from the menubar).
Regarding the first point...
The newly created channel in the channels panel seems to be created inverted (black selection on white background),but when loaded it works correctly (not inverted).
This is fine, - it works as expected when loaded - therefore, there is no problem.
It is just strange that the newly created channel in the channels panel looks inverted (but isn't)
Copy link to clipboard
Copied
Give me ~2 hours and I'll be back on this issue - my Photoshop is busy doing some work now.
Copy link to clipboard
Copied
Cool. Thank you for helping me with this issue!
Copy link to clipboard
Copied
Tomas,
I posted a similar question in another thread, and the code which JJMack posted does exactly what I need - here is the code:
function saveAlpha(alphaName){
var idDplc = charIDToTypeID( "Dplc" );
var desc27 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref11 = new ActionReference();
var idChnl = charIDToTypeID( "Chnl" );
var idfsel = charIDToTypeID( "fsel" );
ref11.putProperty( idChnl, idfsel );
desc27.putReference( idnull, ref11 );
var idNm = charIDToTypeID( "Nm " );
desc27.putString( idNm, alphaName );
try {
executeAction( idDplc, desc27, DialogModes.ALL );
}
catch(e) {}
}
saveAlpha("Change This Name");
Thank you so much for all your help - I appreciate it.
Copy link to clipboard
Copied
There are many ways to skin a cat:)
As long as you find a suitable tool for your task - go with it:)
Copy link to clipboard
Copied
Hi RicSum,
Try this code..
#target photoshop
$.level=0;
if(documents.length>0){main();}
function main(){
var docRef = app.activeDocument;
if(hasSelection()){
var chanRef = docRef.channels.add();
chanRef.name = "New Selection";
chanRef.kind = ChannelType.MASKEDAREA;
docRef.selection.store( docRef.channels.getByName("New Selection"), SelectionType.EXTEND );
docRef.selection.deselect();
selectComponentChannel();
}
else{alert("Document doen't have a selection!");}
}
function selectComponentChannel() {
try{
var map = {}
map[DocumentMode.GRAYSCALE] = charIDToTypeID('Blck');
map[DocumentMode.RGB] = charIDToTypeID('RGB ');
map[DocumentMode.CMYK] = charIDToTypeID('CMYK');
map[DocumentMode.LAB] = charIDToTypeID('Lab ');
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID('Chnl'), charIDToTypeID('Chnl'), map[app.activeDocument.mode] );
desc.putReference( charIDToTypeID('null'), ref );
executeAction( charIDToTypeID('slct'), desc, DialogModes.NO );
}catch(e){}
};
function hasSelection() {
var doc = app.activeDocument;
try{doc.selection.copy(); return true;}
catch(e){ return false;}
}
Copy link to clipboard
Copied
Bonjour
more simply :
"
var docRef = app.activeDocument;
var channelRef = docRef.activeChannels;
var chanRef = docRef.channels.add();
chanRef.name = "New Selection";
chanRef.kind = ChannelType.SELECTEDAREA;
docRef.selection.store( docRef.channels.getByName("New Selection"), SelectionType.EXTEND );
docRef.activeChannels = channelRef;
docRef.selection.deselect();
"
Copy link to clipboard
Copied
Thank you everyone for all the varying solutions - I appreciate all your help