Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

AppleScript Newbie - Display dialog box when quitting an application

Hello.


I was wondering if it's possible to write a script that displays a dialog box (in this case one saying "BACK UP!!!!") everytime i quit Final Draft.


Thanks.


F.

OS X Mountain Lion (10.8.2)

Posted on Feb 2, 2013 2:32 PM

Reply
Question marked as Best reply

Posted on Feb 2, 2013 3:57 PM

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 does have the ability to do Cocoa-y stuff such as register for system notifications, so you can create a new applet (AppleScript Editor > File > New from Template > Cocoa-AppleScript Applet), paste in the following, and save it as an application (uncheck the option to show the startup screen):


propertytheApps : {"Final Draft"} -- a list of applications to watch for


onrun-- example


# add observers for workspace notifications


tellcurrent application's NSWorkspace's sharedWorkspace's notificationCenter

addObserver_selector_name_object_(me, "appQuit:", current application's NSWorkspaceDidTerminateApplicationNotification, missing value)


endtell


-- tell application "Final Draft" to activate

endrun


onappQuit_(aNotification) -- an application quit


# aNotification's userInfo record contains an NSRunningApplication instance that we can get properties from


set
theApplicationto (aNotification's userInfo's NSWorkspaceApplicationKey's localizedName()) astext


iftheApplicationisintheAppsthen


tellcurrent application-- me

beep 3

activate-- make sure dialog comes to front

display dialog"BACK UP!!!!"with title"Final Draft quit notice"buttons {"OK"} default button1

quit


endtell


endif

endappQuit_

When the application is run it will sit there and receive notifications when other applications quit, and checks to see if it was the application you want.

2 replies
Question marked as Best reply

Feb 2, 2013 3:57 PM in response to fgarrido

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 does have the ability to do Cocoa-y stuff such as register for system notifications, so you can create a new applet (AppleScript Editor > File > New from Template > Cocoa-AppleScript Applet), paste in the following, and save it as an application (uncheck the option to show the startup screen):


propertytheApps : {"Final Draft"} -- a list of applications to watch for


onrun-- example


# add observers for workspace notifications


tellcurrent application's NSWorkspace's sharedWorkspace's notificationCenter

addObserver_selector_name_object_(me, "appQuit:", current application's NSWorkspaceDidTerminateApplicationNotification, missing value)


endtell


-- tell application "Final Draft" to activate

endrun


onappQuit_(aNotification) -- an application quit


# aNotification's userInfo record contains an NSRunningApplication instance that we can get properties from


set
theApplicationto (aNotification's userInfo's NSWorkspaceApplicationKey's localizedName()) astext


iftheApplicationisintheAppsthen


tellcurrent application-- me

beep 3

activate-- make sure dialog comes to front

display dialog"BACK UP!!!!"with title"Final Draft quit notice"buttons {"OK"} default button1

quit


endtell


endif

endappQuit_

When the application is run it will sit there and receive notifications when other applications quit, and checks to see if it was the application you want.

AppleScript Newbie - Display dialog box when quitting an application

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.