Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Feb 10, 2013, 01:06 PM   #1
mauianne
macrumors newbie
 
Join Date: Feb 2013
Location: Benevento, Italy
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
mauianne is offline   0 Reply With Quote
Old Feb 10, 2013, 01:18 PM   #2
Red Menace
macrumors 6502
 
Join Date: May 2011
Location: Littleton, 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.
__________________
MacBook Pro / OS X Yosemite (10.10) / Xcode 6.1 / RubyMotion FTW
Red Menace is offline   1 Reply With Quote
Old Feb 10, 2013, 01:43 PM   #3
mauianne
Thread Starter
macrumors newbie
 
Join Date: Feb 2013
Location: Benevento, Italy
Quote:
Originally Posted by Red Menace View Post
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
mauianne is offline   0 Reply With Quote
Old Feb 10, 2013, 02:11 PM   #4
numero
macrumors regular
 
Join Date: Jul 2002
Location: 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
numero is offline   0 Reply With Quote
Old Feb 10, 2013, 02:44 PM   #5
Red Menace
macrumors 6502
 
Join Date: May 2011
Location: Littleton, 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.
__________________
MacBook Pro / OS X Yosemite (10.10) / Xcode 6.1 / RubyMotion FTW
Red Menace is offline   0 Reply With Quote
Old Feb 11, 2013, 10:17 AM   #6
mauianne
Thread Starter
macrumors newbie
 
Join Date: Feb 2013
Location: Benevento, Italy
Quote:
Originally Posted by numero View Post
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?

----------

Quote:
Originally Posted by Red Menace View Post
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
mauianne is offline   0 Reply With Quote
Old Feb 12, 2013, 02:29 AM   #7
numero
macrumors regular
 
Join Date: Jul 2002
Location: 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/...unchdJobs.html
numero is offline   0 Reply With Quote
Old Feb 12, 2013, 07:53 AM   #8
kryten2
macrumors 6502a
 
Join Date: Mar 2012
Location: Belgium
Quote:
One thing I don't like about this solution is that the script shows in the Dock.
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/i...vicesKeys.html
__________________
Space Corps Directive 34124
kryten2 is offline   0 Reply With Quote
Old Feb 12, 2013, 05:11 PM   #9
numero
macrumors regular
 
Join Date: Jul 2002
Location: OR
Quote:
Originally Posted by kryten2 View Post
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/i...vicesKeys.html
This works great. I didn't know how to do this. Thanks.
numero is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
I want a "Force Quit" button in "Shutdown timed out because (app) didn't quit" dialog moonman239 OS X Mavericks (10.9) 0 Mar 10, 2014 12:37 AM
AppleScript: How do I save multiple Excel sheets to one pdf using Applescript sbtaylor1 Mac Programming 0 Oct 29, 2013 12:38 AM
AppleScript: How do I save multiple Excel sheets to one pdf using Applescript sbtaylor1 Mac Applications and Mac App Store 0 Oct 29, 2013 12:32 AM
Applescript (List Force Quit Apps) Kyp Mac Programming 9 Oct 4, 2013 11:38 PM
When I quit...says "quit unexpectedly" aaronvan Mac Applications and Mac App Store 0 Nov 13, 2012 08:25 PM

Forum Jump

All times are GMT -5. The time now is 05:26 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC