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
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.
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
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_
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]
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.
<?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>
chmod 644 ~/Library/LaunchAgents/com.numero.watch.plist
launchctl load ~/Library/LaunchAgents/com.numero.watch.plist
One thing I don't like about this solution is that the script shows in the Dock.
<key>LSUIElement</key>
<string>1</string>
Take a look at the LSUIElement Launch Services key to add to the Info.plist of the applet.
Code:<key>LSUIElement</key> <string>1</string>
Info : http://developer.apple.com/library/ios/#documentation/general/Reference/InfoPlistKeyReference/Articles/LaunchServicesKeys.html