Applescript activation after quit

Discussion in 'Mac Programming' started by mauianne, Feb 10, 2013.

  1. macrumors newbie

    Joined:
    Feb 10, 2013
    Location:
    Benevento, Italy
    #1
    Hi guys, I'd like to run an application after another one has been closed (eventually with a dialogue box for confirmation), is there a way to make this with Applescript? Thanks in advance
     
  2. macrumors 6502

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #2
    A regular AppleScript does not have the ability to get system notifications, so you would have to do something like have the script continually check (poll) for applications that are running (a Cocoa-AppleScript application does have the ability to do Cocoa-y stuff such as register for system notifications), or you can use something like Do Something When.
     
  3. thread starter macrumors newbie

    Joined:
    Feb 10, 2013
    Location:
    Benevento, Italy
    #3
    Thanks, I've tried with DSW and it works nicely. Anyway how could I write a Cocoa-AppleScript for this task? I'd prefer to don't have applications continuously running in background. Is it eventually possible using Automator?
    Thks
     
  4. macrumors regular

    Joined:
    Jul 23, 2002
    Location:
    OR
    #4
    Adjust the application you are watching and the one you want to launch as needed. This has a 15 second wait between checks. Adjust that as well for your needs.

    This script would have to be launched after the application you want to watch has been launched. It could be modified to launch on startup and never quit.

    Another alternative that just came to mind is to write a shell script that is started by launchd. The shell script could grep the ps command looking for the target app. Put in the appropriate sleep and repeat loops that I have in the AppleScript example. To launch the second application (Safari in my example) the shell script could invoke the osascript command to launch the second application.

    Doing it with launchd would have the benefit of being started automatically and it could be configured to relaunch if it ever quit.

    Here's the AppleScript version.

    Code:
    tell application "System Events"
    	set targetProcess to count (every application process whose name is "Mail")
    end tell
    
    if targetProcess > 0 then
    	repeat until targetProcess = 0
    		tell application "System Events"
    			set targetProcess to count (every application process whose name is "Mail")
    		end tell
    		if targetProcess > 0 then
    			delay 15
    		end if
    	end repeat
    	
    	display dialog "Mail has stopped running. Do you want to run Safari?"
    	tell application "Safari"
    		activate
    	end tell
    	
    else
    	display dialog "Mail not found"
    end if
    
     
  5. macrumors 6502

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #5
    Automator is a bit more limited than AppleScript, and for stuff like this you usually wind up using an AppleScript or shell script action anyway.

    You didn't mention what OS you are using, but beginning with OS X 10.7 Lion, the AppleScript Editor can be used to create Cocoa-AppleScript applets by using the AppleScript Editor > File > New from Template > Cocoa-AppleScript Applet menu item. From there, you can do something like the following:

    Code:
    property theApps : {"System Preferences"} -- a list of applications to watch for
    
    on run -- example
    	# add observers for workspace notifications
    	tell current application's NSWorkspace's sharedWorkspace's notificationCenter
    		addObserver_selector_name_object_(me, "appQuit:", current application's NSWorkspaceDidTerminateApplicationNotification, missing value)
    	end tell
    end run
    
    on appQuit_(aNotification) -- an application quit
    	# aNotification's userInfo record contains an NSRunningApplication instance that we can get properties from
    	set theApplication to (aNotification's userInfo's NSWorkspaceApplicationKey's localizedName()) as text
    	if theApplication is in theApps then -- check if it is one we are interested in
    		tell application "Finder" to activate -- or whatever
    		-- tell current application to quit -- tell me to quit
    	end if
    end appQuit_
    The application registers for the desired notifications and just sits there until the system sends it one of the registered notifications (this is more or less what DSW does) - it does remain running in the background, but doesn't really use any CPU time while waiting.
     
  6. thread starter macrumors newbie

    Joined:
    Feb 10, 2013
    Location:
    Benevento, Italy
    #6
    Tks, it is perfect. How could be modified lo launch on startup and never quit?

    ----------

    Hi, I'm sure it is a nice piece of code, unfortunately my understanding of code is very limited, I'll try to study it and see what happens...Thanks, by the way
     
  7. macrumors regular

    Joined:
    Jul 23, 2002
    Location:
    OR
    #7
    I'm not convinced this is the best way, but it is one way of accomplishing what you want. One thing I don't like about this solution is that the script shows in the Dock. It's late and I'm brain dead so you get what you get. :)

    Put the following in a file located in ~/Library/LaunchAgents. If you have more than one user on a Mac that you want this to work for then you will need a pslist file in each of their LaunchAgents directories along with their own copy of the script. For my example I'll say that I named my file "com.numero.watch.plist". For consistency the name of the file should match the label value in the file. I don't think launchd cares, but I think it is convention.
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
        <key>Label</key>
        <string>com.numero.watch</string>
        <key>ProgramArguments</key>
        <array>
            <string>/Users/numero/Desktop/WatchMail_Cocoa-AppleScript_Applet.app/Contents/MacOS/CocoaApplet</string>
        </array>
        <key>KeepAlive</key>
        <true/>
    </dict>
    </plist>
    I liked Red Menace's version so I used it. Follow the instructions given on making a new applet and save it wherever you like. Fix up the path value in the plist file. Change "/Users/numero/Desktop/WatchMail_Cocoa-AppleScript_Applet.app" to the appropriate path to your script. Keep everything from Contents/ on down to the end the same.

    Make sure permissions are right on your plist file. The file should be owned by your user. Make sure file mask is right. It should be rw r r. If you need to change it issue the command in the terminal
    Code:
    chmod 644 ~/Library/LaunchAgents/com.numero.watch.plist
    Then you just need to kick off the plist.
    Code:
    launchctl load ~/Library/LaunchAgents/com.numero.watch.plist
    Of course substituting your actual plist file name for com.numero.watch.plist.

    I hope that about does it. Every time you log in the watch script will launch. If you quit the script it will come back in a few seconds. Even if you force quit it launchd will start it back up. When you log out it will quit.

    Launchctl can be very particular. If you run into trouble just give a shout.

    A little documentation from Apple.
    https://developer.apple.com/library/mac/#documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/CreatingLaunchdJobs.html
     
  8. macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #8
  9. macrumors regular

    Joined:
    Jul 23, 2002
    Location:
    OR
    #9

Share This Page