Handling Duplicate AppleScripts

Discussion in 'Mac Programming' started by the Otter, Dec 22, 2010.

  1. macrumors member

    Joined:
    Jan 10, 2001
    Location:
    Lafayette, IN
    #1
    I have an AppleScript-based application that I have set a cron job to run every 15 minutes. This part works perfectly. However, when the script opens, it pauses and waits for user interaction. Sometimes I put it off, and by the time the next 15 minutes rolls around, a second instance opens.

    What I’d like to have happen is: when the app launches, it checks to see if another instance is already running, brings that instance to the front, and quits itself. Conversely, it could quit the one that’s already running and bring itself to the front. Either way, I’d wind up with a single instance of the application as the frontmost window.

    Right now, the closest I’ve come is:
    Code:
    tell application "System Events" to set previousScript to (name of processes) contains "[I]the app name[/I].app"
    if previousScript is true then
    	tell application "[I]the app name[/I]" to activate
    	return false
    end if
    
    As you can probably guess, this quits the current instance of the app, even if no previous instance is running; and if a previous instance has somehow managed to remain open, the new instance doesn’t interact with the previous one.

    So, bottom line: how can I check for another instance of the same script that’s doing the checking, and operate on that?

    Thanks!
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    Get the script to create a file somewhere at the start and delete it at the end. Check for the existence of that file and quit if it already exists.
     

Share This Page