Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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
share|improve this question

1 Answer 1

up vote 0 down vote accepted

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
share|improve this answer
    
This worked, thank you @Bibou –  phatypus Feb 26 '13 at 0:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.