Launching an AppleScript at startup

Discussion in 'Mac Programming' started by North Pacific, Feb 27, 2011.

  1. North Pacific macrumors newbie

    Joined:
    Nov 14, 2007
    #1
    Hi, I have a very simple AppleScript that presents a reminder on screen every 20 minutes along with a 'yes' and 'no' button. Click 'yes' and the message reappears in 20 minutes, click 'no' and the script exits.

    If I manually open the script it works perfectly. But I'd like to have it automatically launch at startup so I saved it as an application and marked it to open at login. It opens at login but after clicking 'yes' the message disappears as it should, but a spinning black and white beachball stays on screen and the application remains open as the ball spins. I have to click on something else to put the application into the background (where it remains open in the dock). If I want to quit the application at this point I'm unable to except through a force quit (or I can wait 20 minutes for the next reminder message and click the 'no' button so the script exits).

    I'm wondering:

    1) If I keep this application, is it possible to not have the spinning black and white beachball after I click the message's 'yes' button, and just have the application automatically recede to the background/dock?

    2) Would I be better off just launching it as a script at startup and not doing it as an application? If so, what's the easiest way to do that, as I've never used Launchd, launchdaemons, or any of that stuff before, but could certainly give it a try.

    3) Is there a way to get the reminder message to stay in the foreground (on top of everything else) when it appears? Right now if I have a document or browser open and am clicking something at the moment the message appears, the message gets hidden underneath.

    Thanks for any help!

    Here's the script:
    __________________________________

    Code:
    set delayTime to 60 * 20
    
    repeat
    	
    	display dialog "Check it." buttons {"Yes", "No"} default button 1
    	if the button returned of the result is "Yes" then
    		delay delayTime
    	else
    		exit repeat
    	end if
    end repeat
     
  2. McGordon macrumors member

    Joined:
    Dec 28, 2010
    Location:
    Scotland
    #2
    This sounds like a job for a "stay open" script with an "on idle" handler. I tried your script and it does work. I didn't see the spinning beach ball though, even when saved as an application and set as a login item. I didn't wait 20 minutes though, so maybe I didn't wait long enough. You can see Quit in the applications menu, but clicking it does nothing and you have to force quit it if you don't want to wait on the dialog appearing again.

    Adding an "activate" command should bring it to the front. Its Dock icon should also bounce to draw your attention to it if you're clicking something else at the same time or just after it appears.

    Try something like this:

    Code:
    
    on idle
       -- Short delay for testing, remember to change back to 20 * 60
       set delayTime to 10
       
       activate
       display dialog "Check it." buttons {"Yes", "No"} default button 1
       if the button returned of the result is "Yes" then
          return delayTime
       else
          quit
       end if
    end idle
    
    on quit
       display dialog "Really Quit?" buttons {"Cancel", "Quit"} default button 2
       if the button returned of the result is "Quit" then
          continue quit
       end if
    end quit
    
    
    Save it as an Application and click the "Stay Open" checkbox when saving it.

    You should now be able to quit it by selecting Quit from the application's menu. The "Really Quit?" dialog can be removed, but its just to let you see when the "on quit" handler is being called. The "on quit" handler MUST contain the "continue quit" command or you'll have to force quit it.

    The only issue I have is that when you select "No" to the "Check it." dialog, then "Quit", it doesn't actually quit until you click on another application. I'm not sure what's happening there.
     
  3. North Pacific thread starter macrumors newbie

    Joined:
    Nov 14, 2007
    #3
    That works great! The only thing I'm not getting is a bouncing dock icon if I click on something else at the same time as the message appears or just after, but that's not a big deal. What's great is that now I'm seeing the message much more frequently the way it appears in front now as opposed to before. No more spinning beachball. And I don't have the quit issue you described, when I click 'no' and then 'quit' it quits without me needing to click on another application.

    Thanks so much for taking the time to figure this out for me. Very much appreciated! :cool:

    Cheers
     
  4. andmr macrumors member

    Joined:
    Aug 25, 2008
    Location:
    NE Florida
    #4
    Another idea, for what it's worth...

    The script below calls upon System Events to force the script into the background when the Yes button is clicked, thus returning you seamlessly to the application you were working in before the dialog appeared.

    In this example, the script is saved as a stay-open application, named "My Reminder Dialog." (You can choose any name you want as long as the reference in the script matches the saved script applet's name.)

    Code:
    on idle
    	activate
    	display dialog "Check it." buttons {"Yes", "No"} default button 1
    	if the button returned of the result is "Yes" then
    		tell application "System Events" to set visible of process "My Reminder Dialog" to false
    		return 10 --> 10 seconds used in testing; change to 60 * 20
    	else
    		quit
    	end if
    end idle
    Just a thought. Good luck.
     
  5. North Pacific thread starter macrumors newbie

    Joined:
    Nov 14, 2007
    #5
    I gave it a try but I get the following error message (and a black and white beachball) after clicking the 'yes' button:

    "Can't set <<class prcs>> "My Reminder Dialog" of application "System Events" to false."

    There's an 'edit' and 'ok' button, clicking 'ok' keeps the application as the active item and resets the timer.

    I tried placing the application in both the computer's script folder and my user script folder, and launched from both the menu bar as well as the dock but keep getting the error message. Any idea why?
     
  6. andmr macrumors member

    Joined:
    Aug 25, 2008
    Location:
    NE Florida
    #6
    Hmm... sorry that didn't work for you. I was unable to reproduce any of the trouble you describe. The script works fine for me, in Mac OS 10.4.11, using AppleScript v. 2.1.1.

    The black-and-white spinning cursor should not appear in an on-idle script. You'd expect to see that with a repeat loop, but not in an on-idle script -- one reason not to use the repeat loop in this case (constant hogging of cpu cycles being another reason).

    The script I offered is meant to work when copied and pasted into your AppleScript Editor, and then saved as a stay-open application. If you choose to save the script with a name other than "My Reminder Dialog," you'd need to edit the corresponding portion of the script accordingly. The saved script name and the reference to it found within the tell application "System Events"... statement must be an exact match.

    Moreover, it shouldn't matter how the applet is launched. My test version performed as expected whether launched from the dock, the desktop, or from the script menu. I'm at a loss... :(
     
  7. McGordon macrumors member

    Joined:
    Dec 28, 2010
    Location:
    Scotland
    #7
    andmr's script worked fine for me too on 10.6.6, Applescript 2.3(118)

    Check the name you saved it as matches exactly what it has in the script.
     
  8. North Pacific thread starter macrumors newbie

    Joined:
    Nov 14, 2007
    #8
    Odd, I'm not sure what the problem is. I gave it a second try, using an exact copy and paste from andmr's post, ensuring the script name and statement reference match exactly, but I still get the same error message. I'm using AppleScript 2.2.1 on 10.5.8..

    I'm open to any further advice or ideas, otherwise McGordon's script will work just fine. Thanks to you both, andmr and McGordon, for all your help and suggestions. :)

    Cheers
     

Share This Page