Copy link to clipboard
Copied
Hello there,
I am a digital painter that has (with lots of help here, lol!) written this script to help speed a particular function that I consistently use in my painting workflow. While testing, I've found that the script works perfectly when executed at the top of my layer stack, however should I need to scroll down into my stack and utilize the script, it is thrown off because the 'new layer' added in line 49 opens above the current layer, but then jumps to the top of the stack; I need this 'new layer' to maintain its position AND not jump to the top of the stack. Here is the script:
1) function main() {
2)
3) var __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this === item) return i; } return -1; };
4)
5) var makeNewLayer = function() {
6) var newLayer = doc.artLayers.add();
7) newLayer.name = 'magic layer1';
8) newLayer.applyStyle('magic layer1');
9) doc.activeLayer = newLayer;
10) }
11)
12) var doc = app.activeDocument,
13) lay = doc.activeLayer,
14) names = ['magic layer1',
15) 'magic layer2',
16) 'magic layer3',
17) 'magic layer4'];
18)
19) if (__indexOf.call(names, lay.name) < 0 ) {
20) newLayer = doc.artLayers.add();
21) makeNewLayer();
22) return;
23) }
24)
25) if (doc.activeLayer.kind == LayerKind.NORMAL && doc.activeLayer.bounds[2] == 0 && doc.activeLayer.bounds[3] == 0) {
26) var dialog = confirm("End magic layers?");
27) if (dialog) {
28) app.activeDocument.activeLayer.remove();
29) }
30) return;
31) }
32) var idrasterizeLayer = stringIDToTypeID( "rasterizeLayer" );
33) var desc5 = new ActionDescriptor();
34) var idnull = charIDToTypeID( "null" );
35) var ref4 = new ActionReference();
36) var idLyr = charIDToTypeID( "Lyr " );
37) var idOrdn = charIDToTypeID( "Ordn" );
38) var idTrgt = charIDToTypeID( "Trgt" );
39) ref4.putEnumerated( idLyr, idOrdn, idTrgt );
40) desc5.putReference( idnull, ref4 );
41) var idWhat = charIDToTypeID( "What" );
42) var idrasterizeItem = stringIDToTypeID( "rasterizeItem" );
43) var idlayerStyle = stringIDToTypeID( "layerStyle" );
44) desc5.putEnumerated( idWhat, idrasterizeItem, idlayerStyle );
45) executeAction( idrasterizeLayer, desc5, DialogModes.NO );
46)
47) doc.activeLayer.merge();
48) doc.activeLayer.name = "base paint layer";
49) makeNewLayer();
50) return;
51)
52) }
53)
54) main();
Any and all help and/or suggestions will be greatly appreciated!
Thanks,
-Eric
Copy link to clipboard
Copied
Oh,... and here is the original brief for the script,... how it works:
I'm a freelance illustrator that utilizes layer styles extensively in my workflow. As the rasterization and implementation of a new layer style is constant and continual, I would like a way to streamline the 3-4 actions that I currently use into a single conditional script. I'm currently using CS6. Here is the itemized description of how I would like the script to function:
1) Is the selected layer named "magic layer 1"? If no, go to line #2. If yes, go to line #8.
2) Is the selected layer named "magic layer 2"? If no, go to line #3. If yes, go to line #8.
3) Is the selected layer named "magic layer 3"? If no, go to line #4. If yes, go to line #8.
4) Is the selected layer named "magic layer 4"? If no, go to line #'s 5-7. If yes, go to line #8.
5) Make new layer.
6) Name new layer "magic layer1".
7) Add layer style (also named) "magic layer1". Script concludes.
8) Is layer empty? If yes, go to line #9. If no, go to line #'s 10-12.
9) Queue dialog box: "End magic layers?" with choice of "yes" or "no".
• If "yes" is selected, the layer is deleted. Script concludes.
• If "no" is selected, the layer is kept (no change). Script concludes.
10) Rasterize layer.
11) Merge layer down.
12) Rename layer "base paint layer". Go to line #'s 5-7.
Copy link to clipboard
Copied
Adding a layer to the document will always create it as the first layer, you have to move it to the right place.
var makeNewLayer = function(target_layer) {
var newLayer = doc.artLayers.add();
newLayer.name = 'magic layer1';
newLayer.applyStyle('magic layer1');
newLayer.move(target_layer, ElementPlacement.PLACEBEFORE)
doc.activeLayer = newLayer;
}
Copy link to clipboard
Copied
Hey there,
Appreciate the quick reply,... but (as my scripting abilities are nearly non-existent) I'm having a hardtime figuring where to place this bit of code. I tried replacing lines 5-10, but it didn't work. Also tried line 49,... also a no-go. I'm sorry,... I suppose this should be obvious for those familiar with scripting,... so please forgive my ignorance, but I'm not sure how to incorporate this/make this work!? I do appreciate the work that you've put in thus far....
Copy link to clipboard
Copied
So I changed your function so it takes a parameter telling on top of which layer you want to create the new one. Since I don't know your case exactly, can't say how you should use it. But if you want it created above the active layer, you'd call it like makeNewLayer(app.activeDocument.activeLayer)
Copy link to clipboard
Copied
is this what you're looking for:
// =======================================================
var idMk = charIDToTypeID( "Mk " );
var desc2805 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref775 = new ActionReference();
var idLyr = charIDToTypeID( "Lyr " );
ref775.putClass( idLyr );
desc2805.putReference( idnull, ref775 );
executeAction( idMk, desc2805, DialogModes.NO );
Copy link to clipboard
Copied
Possibly,.... and sorry to be such a newby,... but I don't know where to 'plug' this in to the existing script. What line(s) do I need to replace with this new code?!
Thanks,
-Eric
Copy link to clipboard
Copied
That part I'm not sure about. All I can say is that the script I posted will create a new, empty layer above the active layer and *not* send it to the top of the stack. Try running the snippet I posted as is and test it to see if the new layer is created above the active layer. Hopefully it can help you move things along, but someone more knowledgeable might be able to help out with where exactly in your code you need to place it
Copy link to clipboard
Copied
When I run your code independantly, yes, it adds a new layer and keeps in above the current layer,... however, when I placed it within the context of the full script (line 49), I get an error (ExtendScript toolkit) in the last line of your code. Kinda lost as to how to incorporate this into the full script. Yikes! Help, anyone?!
Copy link to clipboard
Copied
Still looking for help with this,... anyone have any suggestions. Sorry, my scripting knowledge is very limited,... any suggestions are greatly appreciated!
Copy link to clipboard
Copied
Bump,... still looking for help on this. Please. Pretty please!