|
|
#1 |
|
Applescript activation after quit
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
|
|
|
|
0
|
|
|
#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.
__________________
MacBook Pro / OS X Mountain Lion (10.8.3) / Xcode 4.6 / [various (much) older stuff keeping dust off the shelves] |
|
|
|
1
|
|
|
#3 | |
|
Quote:
Thks |
||
|
|
0
|
|
|
#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 |
|
|
|
0
|
|
|
#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_
__________________
MacBook Pro / OS X Mountain Lion (10.8.3) / Xcode 4.6 / [various (much) older stuff keeping dust off the shelves] |
|
|
|
0
|
|
|
#6 | ||
|
Quote:
---------- Quote:
|
|||
|
|
0
|
|
|
#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>
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 Code:
launchctl load ~/Library/LaunchAgents/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/...unchdJobs.html |
|
|
|
0
|
|
|
#8 | |
|
Quote:
Code:
<key>LSUIElement</key> <string>1</string>
__________________
Space Corps Directive 34124 |
||
|
|
0
|
|
|
#9 | |
|
Quote:
|
||
|
|
0
|
![]() |
|
«
Previous Thread
|
Next Thread
»
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
All times are GMT -5. The time now is 10:06 PM.








Linear Mode
