Checking button text with applescript I'm trying to automate CCleaner, so far I can start the application and click the button that starts the cleanup job, however I'm relying on a delay to determine when I should move onto the next portion of the script.

tell application "/Applications/CCleaner.app" to activate

tell application "System Events"
    tell application process "CCleaner"
        click button "Run Cleaner" of window 1
        delay 10
    end tell
end tell


I don't particularly like this approach and would prefer to detect when CCleaner is finished running (it may be much sooner or later than the 10 second delay).  

While CCleaner is inactive, the button text is "Run Cleaner"; when CCleaner is active, the button text is "Cancel".  Can someone advise how I check the text on the button?  If I know how to do that I can do something like this:

tell application "/Applications/CCleaner.app" to activate

tell application "System Events"
    tell application process "CCleaner"
        click button "Run Cleaner" of window 1
        delay 10
    end tell
end tell

repeat
    # ?
    # ? if button text is "Run Cleaner" then exit repeat
    # ?
    delay 1
end repeat

# do more stuff
You can check every button of the window and wait until one is titled like you want :


property btnTitle : "Run Cleaner"

set btnFound to false

tell application "/Applications/CCleaner.app" to activate

tell application "System Events"
    tell application process "CCleaner"
        click button btnTitle of window 1
        delay 1

        -- Start checking every seconds if window 1 contains a button titled "Run Cleaner"      
        repeat while not btnFound
            repeat with btn in buttons of window 1
                try -- Some buttons don't have title which would return an error if not in try
                    if (title of btn is btnTitle) then
                        set btnFound to true
                    end if
                end try
            end repeat
            delay 1
        end repeat

    end tell
end tell

if btnFound then
    -- Do more stuff
end if