Layer index with applescript

Is there a way to get the index (number) of the current layer using AppleScript?

This is what I would like to do:

I have a document that might have multiple layers: "Image from Clipboard, Shape Layer, Bitmap Layer 3, Shape Layer, "

I might have multiple selections on one of the bitmap layers which is the current layer

I then want to scriptomatically:

  • create new layer with selection
  • go back to the layer I was on and deselect
  • apply my filter (usually exposure down 0.6)
  • jump back to the top layer
  • do more stuff like add a further shape layer (e.g. arrows) on top

step 2 is what is tripping me up.

…and I see something teasingly close in Cocoascript: var layer = doc.layers()[0];

Thanks

Rob

There’s a private api called “indexInGroup()” you can use on a layer object. Try that out and let me know if it works for what you’re trying to do. If it does, I’ll add it to the official API (or come up with another way to get what you’re after).

(The non-private way is to loop through a group’s layer and increment some value until you come across the layer you’re curious about)

thanks for that Gus.

That looks a bit cocoascripty. I should add that I don’t have a clue about CocoaScript and instead spend time torturing myself with AppleScript Syntax.

So is that private API available there?

Thanks

Rob

tell application "Acorn"
    tell front document
        set this_layer to indexingroup
    end tell
end tell

this_layer

Late reply, summertime stuff.

At any rate - there’s no way to do this in AppleScript. I’ll add it to the list though.

I just put up a new build of Acorn, where the paste command returns the layer if a layer was created. You can grab it from here: Latest Builds from Flying Meat

Here’s a sample script which will do what you’re after (and let me know if not!)

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

tell application "Acorn"
	tell document 1
		set curLayer to current layer
		copy
		set newLayer to paste
		
		set current layer to curLayer
		
		deselect
		
		tell layer 1
			load filter preset name "Exposure Preset"
		end tell
		
		set current layer to newLayer
		
	end tell
end tell

apologies for the delay in responding. Thanks for that.

that appears to do the trick, nice!!! I changed the script ever so slightly.

Thanks

use scripting additions
tell application "Acorn"
    tell document 1
        set curLayer to current layer
        copy
        set newLayer to paste
        set current layer to curLayer
        deselect
        load filter preset name "exposure_down_6"
    end tell
end tell