Applescript help wanted - how to select some layers?

Talk about Pixelmator Pro, share tips & tricks, tutorials, and other resources.
User avatar

2022-07-13 16:46:09

OK I just spent couple of hours trying to understand how to code this in Applescript, no success. Can anyone help?

I need a script that takes 1 selected layer, and selects all layers with same property (e.g. same fill color). However I am unable to simply select any layers from "array" (or "list"). I'm probably missing some syntax.

Here is my attempt:
tell application "Pixelmator Pro"
	activate
	tell its front document
		if not (count selected layers) = 1 then
			display alert "Make sure a single text layer is selected."
			return
		end if
		-- set scope to ¬
			-- (choose from list {"fill", "stroke"} with prompt "Select all layers with the same:" default items {"fill"}) as text
		
		set all_layers to every layer whose visible is true
		set selected_layer to current layer
		repeat with l in all_layers
			-- if (scope = "fill" and fill color of selected_layer = fill color of l) then
				set selected of l to true
			-- end if
		end repeat
	end tell
end tell
Another reason could be that "select" command does not accept lists. E.g. this works:
select (every layer)
but this does not:
set all_layers to every layer
select (all_layers)
Is there no hope?
User avatar

2022-07-14 08:18:27

As far as I can see, the select command takes a specifier as its parameter and, when using phrases like "every x", AppleScript automatically turns the result of the phrase into an NSScriptObjectSpecifier. This appears to be an object that is then passed to the script command for Pixelmator Pro to handle. When passing the select command a list (which is what happens when you create a variable from a specifier), the select command isn't even called because AppleScript tries to turn the list into a specifier and that doesn't seem to work. The only workaround I can think of right now is to temporarily rename every layer, select them according to their name, then rename them again:
set allLayers to every layer whose visible is true
set originalNamesOfEveryVisibleLayer to name of every layer whose visible is true
repeat with currentLayer in allLayers
	set name of currentLayer to "selectMe"
end repeat
select (every layer whose name is "selectMe")
repeat with a from 1 to number of allLayers
	set name of item a of allLayers to item a of originalNamesOfEveryVisibleLayer
end repeat
It's not pretty but it works...