Duplicate layers with index

Here is a handler that duplicate a layer with name with count index. (Pixelmator Pro)

I would like to know if this could be done in a better way or approach ??
As this could be very useful in other situations.

Regards.
Fredrik

(**
* [duplicateLayerWithName:(string):byCount:]
*
* Description: Duplicate a layer with name string and add index.
*)

my duplicateLayerWithName:"background" byCount:5

on duplicateLayerWithName:_nameString byCount:_countInteger
	tell application "Pixelmator Pro"
		tell front document to set layerList to (its name of layers)
		repeat with i from 1 to _countInteger - 1
			-- Check if the number exists
			if ((_nameString & space & "1") is in layerList) then
				log "error: " & "Check if the layer excists"
				error number -128
			else
				set layerName to (my renameLayerNameFrom:_nameString toLayerName:(_nameString & space & i))
				tell front document to duplicate layer (_nameString & space & i)
				-- rename the last item.
				if i is _countInteger - 1 then
					set layerName to (my renameLayerNameFrom:_nameString toLayerName:(_nameString & space & i + 1))
				end if
			end if
		end repeat
	end tell
end duplicateLayerWithName:byCount:
on renameLayerNameFrom:_nameStringCurrent toLayerName:_nameStringNew
	tell application "Pixelmator Pro"
		tell front document
			set theLayerList to its name of every layer
			try
				set theIndex to my list_position(_nameStringCurrent, theLayerList)
				set name of layer (get item theIndex of theLayerList) to _nameStringNew
			end try
		end tell
	end tell
end renameLayerNameFrom:toLayerName:
on list_position(this_item, this_list)
	repeat with i from 1 to the count of this_list
		if item i of this_list contains this_item then return i
	end repeat
	return 0
end list_position
1 Like

I have find so many strange things with Pixelmator scripting and bugs too.
Check this script… selected layer are stored as layer id. That is not useful
if I like to to move selected layer to a group.

Here is example.

-- my duplicateLayerWithNameMoveToGroup:"background" byCount:3
on duplicateLayerWithNameMoveToGroup:_nameString byCount:_countInteger
	tell application "Pixelmator Pro"
		-- Get the layer names
		tell front document to set layerList to (its name of layers)
		-- Get the group names.
		tell front document to tell group layers to set groupName to its name
		
		-- Make the layer index
		repeat with i from 1 to (_countInteger - 1)
			set layerName to _nameString
			-- Check if the _nameString already excists and group name.
			if ((layerName & space & i) is in layerList) or (_nameString is in groupName) then
				log "error: " & "Check if the layer excists"
				error number -128
			else
				(my renameLayerNameFrom:layerName toLayerName:(layerName & space & i))
				tell front document to duplicate layer (layerName & space & i)
				(my renameLayerNameFrom:layerName toLayerName:(layerName & space & i + 1))
			end if
		end repeat
		
		-- Make group layer with name.
		tell front document
			-- Make the group layer at the front of layers with _nameString
			set theGroup to make new group layer at the front of layers with properties {name:_nameString}
		end tell
		
		-- Select the layer index
		set theSelection to {}
		repeat with j from 1 to (_countInteger - 1)
			(my selectLayerWithNamesFrom:(_nameString & space & (j - 1)) toLayerName:(_nameString & space & (j + 1)))
		end repeat
		
		-- Get the selected layers
		tell front document to set theList to selected layers
		
		-- Build a list of names
		set nameList to {}
		repeat with i from 1 to (count theList)
			tell item i of theList to set theName to its name
			copy theName to end of nameList
		end repeat
		
		-- Move the selected layers to group name.
		tell front document
			repeat with i from 1 to (count nameList)
				move the layer (item i of nameList) to group layer _nameString
			end repeat
		end tell
	end tell
end duplicateLayerWithNameMoveToGroup:byCount:
1 Like