Weird UI scripting issue - after a click, items can't be found

I am trying to automate the process of selecting builds for text fields in Keynote 6. Unfortunately they are not exposed to Applescript so I am doing it with UI scripting.

The problem is that after the script clicks the button to change the build and selects the new build, the contents of the window are no longer available to system events.

Here’s the script:

tell application "System Events"
	tell process "Keynote"
		set frontmost to true
		tell window 1
			
			-- open the toolbar if it's not open already
			if toolbars is {} then
				click menu item "Show Toolbar" of menu "View" of menu bar item "View" of menu bar 1
			end if
			
			-- getting the properties of the default body item also selects it in the UI
			tell document 1 of application "Keynote"
				set s to current slide
				properties of default body item of s
			end tell
			
			-- open the animations pane if it's not open already
			if value of radio button "Animate" of radio group 1 of toolbar 1 is 0 then
				click radio button "Animate" of radio group 1 of toolbar 1
			end if
			
			-- deal with existing and new animations
			if button "Change" exists then
				click button "Change"
				click button "Appear" of scroll area 1 of pop over 1 of button "Change"
				set s to entire contents
			else if button "Add an Effect" exists then
				click button "Add an Effect"
				click button "Appear" of scroll area 1 of pop over 1 of button "Add an Effect"
			end if
		end tell
	end tell
end tell

return s

as it is, this returns {}

if the line “set s to entire contents” is moved up above the line that clicks “Appear”, s contains the contents of the window as expected.

Also, if I run the following subsequently (to complete the desired process), in a separate script, it works perfectly:

tell application "System Events"
	tell process "Keynote"
		tell window 1
			click pop up button 2 of scroll area 1
			click menu item "By Bullet" of menu 1 of pop up button 2 of scroll area 1
		end tell
	end tell
end tell

but if I include this code in the first script, or call it using a run script command from the first script, I get

error “System Events got an error: Can’t get scroll area 1 of window 1 of process "Keynote". Invalid index.” number -1719 from scroll area 1 of window 1 of process “Keynote”

Surprisingly, the same thing happens if I call both of the scripts in sequence from a single bash script using osascript. But it I run each separately on the command line using osascript, the second one works fine.

No doubt this is a weird Keynote bug but if anyone has any suggestions I would be very grateful! If I can get this working it will save me a few hundred clicks a week :slight_smile:

I suggest you have a close look here:
iworkautomation.com/keynote/index.html

That site is great but it only deals with supported AppleScript, not UI scripting, and it certainly doesn’t addres the kind of scenario i describe. Thanks though.

Bump…anyone else have any ideas?

Hello.

Not sure how much this is going to help you, but there are at least some scripts for getting at the UI properties of an app here. :slight_smile:

Thanks. I have used the original script from macosxhints quite a bit but hadn’t seen the refinements in this thread before…I’ll see if they can help.

Hello

Not tested but I guess that some delay instructions are needed.

    if button "Change" exists then
               click button "Change"
delay 1 # ADDED
               click button "Appear" of scroll area 1 of pop over 1 of button "Change"
               set s to entire contents
           else if button "Add an Effect" exists then
               click button "Add an Effect"
delay 1 # ADDED
               click button "Appear" of scroll area 1 of pop over 1 of button "Add an Effect"
           end if

With these delays, the scroll area will have the time needed to appear.
If this solve the problem, it will be time to replace them by a loop checking that the scroll area is available.

Yvan KOENIG (VALLAURIS, France) mercredi 18 mars 2015 12:28:28