Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

the Otter

macrumors member
Original poster
Jan 10, 2001
37
5
Lafayette, IN
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!
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.