Selecting shapes in PowerPoint using AppleScript -- how much control do we have?

New poster, new topic, but: I’m racking my brains trying to figure out how AppleScript controls the shapes selected in PowerPoint. (What I’d like to do is automatically select all objects of a certain color so that I can subsequently manipulate them manually, but that’s – perhaps – not possible.)

The documentation lets us unselect whatever shapes are selected, and we do this by talking directly to the selection:

tell application "Microsoft PowerPoint"
	set theSelection to selection of document window 1 of active presentation
	tell theSelection
		unselect
	end tell
end tell

There is, however, no documentation allowing us to ‘select’ anything, but we can do it anyways, and this is where it gets very confusing. The slide lets us select an arbitrary item (in this case ‘item 1’, but ‘item 2’ or ‘item 505’ work, if you have that many shapes on the slide):

tell application "Microsoft PowerPoint"
	set theSelection to selection of document window 1 of active presentation
	set theSlide to slide 1 of slide range of theSelection
	set theShapes to shapes of theSlide
	tell theSlide
		select item 1 of theShapes
	end tell
end tell

In the Log, it looks as if ‘theShapes’ is a list. Indeed, we can select all items very easily if we just ask the slide to select that list:

tell application "Microsoft PowerPoint"
	set theSelection to selection of document window 1 of active presentation
	set theSlide to slide 1 of slide range of theSelection
	set theShapes to shapes of theSlide
	tell theSlide
		select theShapes
	end tell
end tell

Makes sense; theShapes is now all the shapes on the slide, and we’re asking the slide to select theShapes, which selects all of them. However, if we create our own list with just one shape in it, it still selects everything on the slide:

tell application "Microsoft PowerPoint"
	set theSelection to selection of document window 1 of active presentation
	set theSlide to slide 1 of slide range of theSelection
	set theShapes to shapes of theSlide
	set shapeList to {}
	set end of shapeList to item 1 of theShapes
	tell theSlide
		select shapeList
	end tell
end tell

(We can get around this by saying ‘select item 1 of shapeList’, incidentally; then we are just feeding the ‘select’ command a single shape object.)

Has anyone successfully used the undocumented ‘select’ command to select more than one, but fewer than all, shapes on a slide?

Any thoughts very welcome!

P.

I have the same experience.

Far from any solution, but a few ‘the mystery deepens’ style observations, perhaps, having dug into a few more dead ends:

  • The ‘select’ command is not defined in PowerPoint, but rather in System Events (specifically, it’s the ‘misc’ \ ‘slct’ Apple Event that is sent)
  • The ‘select’ command should take one UI element as its direct parameter
  • PowerPoint gracefully handles ‘select’ either being passed a UI element (in which case that shape is selected) or a list of UI elements (in which case every shape on the whole slide is selected, regardless of what was in that list)
  • However, giving System Events the ‘select’ command directly is only handled gracefully when one UI element is passed to it
  • Neither PowerPoint nor System Events deals gracefully with other application control commands that are documented in System Events. For example, ‘click’ does not work, nor does ‘keystroke’

Not sure what to make of any of this, but putting it out there for reference, in case anyone else is trying to select things with AppleScript.

Some code that will run if you have three or more shapes on a PowerPoint slide:

-- Getting this set up:
tell application "Microsoft PowerPoint"
	set theSelection to selection of document window 1 of active presentation
	set theShapeRange to shape range of theSelection
	set theSlide to slide 1 of slide range of theSelection
	set theShapes to shapes of theSlide
end tell

-- What you can tell the slide
tell theSlide
	select item 2 of theShapes -- this works, and selects one shape
	select items 2 thru 3 of theShapes -- this works, and selects all shapes (as it's given a list)
	select theShapes -- this works, and selects all shapes
end tell

-- alternate way: use System Events directly
tell application "System Events"
	select item 3 of theShapes
	--	select items 2 thru 3 of theShapes -- this doesn't work, here, hence the commenting out
	--	select theShapes -- this doesn't work here, hence the commenting out
end tell

Again, if anyone has any insights, any commentary / thoughts are much appreciated!

P.