Applescript active window problem

Discussion in 'Mac Programming' started by NeeLonMac, Oct 30, 2010.

  1. macrumors newbie

    Oct 6, 2010
    Hey guys, I created a script that will put the computer to sleep when I open it with voice commands. But because I don't want it to go to sleep by accident, I installed a "Are you sure?" dialog box.
    Of course, I want to handle that dialog box with my voice as well, because the whole idea is putting my computer to sleep with my voice.

    Problem is, I can't get the new dialog window active. I have to click the dialog window, before it accepts my "Yes" or "No". The rest all works fine.

    How can I fix this?
  2. thread starter macrumors newbie

    Oct 6, 2010
    I'm also creating a script that will open this site, on the specific tread when somebody replied on a tread of mine, so don't be shy and reply! :)
  3. macrumors member

    Aug 25, 2008
    NE Florida
    You might try adding tell me to activate immediately preceding the display dialog command ("me" being the running script itself).

    For example:

    tell application "Finder" to activate
    tell me to activate
    display dialog 
    Notice what would happen if tell me to activate is commented out or removed; adding it ensures that the dialog window comes to the fore, since the Finder is no longer the frontmost application.
  4. thread starter macrumors newbie

    Oct 6, 2010
    Great, it works! Thanks!

    Since I love it when I can find complete working scripts when I am in search of one, I'll post the working script here.

    -- tell application "Finder" to activate
    tell me to activate
    set question to display dialog "Are you sure you want the computer to go to sleep?" buttons {"Yes", "No"} default button 1 with icon 1
    set answer to button returned of question
    if answer is equal to "Yes" then
    	say "Going to sleep now"
    	tell application "Finder"
    	end tell
    end if

Share This Page