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

mauianne

macrumors newbie
Original poster
Feb 10, 2013
3
0
Benevento, Italy
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
 

Red Menace

macrumors 6502a
May 29, 2011
578
226
Colorado, USA
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.
 

mauianne

macrumors newbie
Original poster
Feb 10, 2013
3
0
Benevento, Italy
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.

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
 

numero

macrumors regular
Jul 23, 2002
106
3
OR
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
 

Red Menace

macrumors 6502a
May 29, 2011
578
226
Colorado, USA
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.
 

mauianne

macrumors newbie
Original poster
Feb 10, 2013
3
0
Benevento, Italy
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.

[/CODE]

Tks, it is perfect. How could be modified lo launch on startup and never quit?

----------

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.

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
 

numero

macrumors regular
Jul 23, 2002
106
3
OR
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.