System Modal Dialog

I tried in vain to find a solution to this problem. I need to display a notification dialog that floats as the front most window. I’m currently using “System Events” to achive the dialog to appear. However if it is clicked but not on the button and then the user click some place else the dialog is not longer front most. I want to ensure the user clicks ok for policy reasons. The applescript is envoked from the background shell script. Thank in advance.

cheers,
Jakub

www.24usoftware.com/AppearanceOSAX publishes “Better” floating dialogs of all kinds.

Why do you need to display such a dialog? What is the goal here?

I’m with mikey-san on this one… I’m curious to know why you have to do this… and “policy reasons” leaves a whole lot of gray area. Are you developing some kind of installer or something? There’s no such thing as a true system-wide dialog… especially not one that applescript is going to provide you easy access to. Applescript is way too high-level to give you access to completely locking out a user from doing anything until they press a button in a simple dialog. This would be a huge security problem in my opinion, and a lot of users would be upset to know that anyone could just whip up a script that could freeze them out of their computer until they did whatever you told them to do. Even the system-generated security windows and ‘dialogs’ that pop up do not block users out completely. You should always allow users to perform other actions and focus other apps while they are using your products. And, you should always allow the user a graceful way to opt out if they so choose. That’s clearly part of the apple workspace vision, and should be adhered to. If for no other reason than to allow users to force quit your process if something goes wrong, so they don’t have to hard restart the whole machine. I especially think you shouldn’t be jumping to the front and hijacking users’ machine if your process is running in the background. If you’re looking for this sort of functionality, you should be writing an actual application in cocoa or applescript studio… or at minimum you should be presenting the terms of your process BEFORE starting it, so you don’t have to bother committing people to it after it’s already started. This way people know that the process is running, and you don’t have to all of a sudden force them to adhere to your conditions or requirements half way through. And, while an osax might give you some more flexibility in how you display your dialog, if this is a public distribution project (even if to just friends and family) you shouldn’t assume that your users will want to install something that merely facilitates doing things in a manner that is such an obvious hack.

There is a fine line that we as developers must walk. When you start doing things in ways that they are not meant to be done, you get people upset and will eventually find that you lose the interest of people that expect you to adhere to certain standards. Don’t get me wrong, there are times and places for using a window that waits for confirmation before continuing, but an applescript is probably not the medium with which to deliver this sort of interface element. Go the distance, and write a script or application that acts as a true cocoa-based interface wrapper for your process. It sounds to me like you’re going about this in the wrong way, and you’re probably not finding answers because you’re trying to do something that is not intended to be done or is irresponsible to do.

j

Thankyou for your feedback.

I need the dialog to appear as we only allow laptops to connect to networks we want. Depending on the configuration set soe machines are not allowed on our company network whilst some are not allowed anywhere else. When my program detects that you are attempting to connect to a wrong area it needs to alert the user and remind them to contact the IT dept.

From my experience a lot of users have a tendency not to read messages unless they are blocking other windows below. There are programs that do that. Retrospect is one that has a floating dialog to alert the user when the backup completed or the machine has not been backed up for a while. There are many reason why you would want to ensure that an system alert stays at the front of the screen.

Anyway I thought this might have been easily done in applescript.

Jakub

I realize this isn’t as light-weight of a solution as you’d like… but you could do this in XCode as either an AppleScript Studio Application or by using the Obj-C tie-ins.

Using Obj-C you’ll need to lookup the “above other windows” code. Here is one link to get started: http://developer.apple.com/documentation/Cocoa/Conceptual/WinPanel/Concepts/WindowLevel.html

However, I think it would MUCH easier for you to do this instead. Design your main window in your nib to look like the dialog you want. Edit the Nib’s “File Owner” so that it has an AppleScript trigger on “Application/resigned active”.

In your applescript, add the following handler:


on resigned active theObject
	tell application (name of me) to activate
end resigned active

Please note that neither of these is a truly modal dialog (ala windows). Users will still be able to click through to other applications. However, this dialog will remain on top of all other windows. If it’s important that your users don’t just drag it out of the way, add code to reset the window position in the handler.

As a quick and dirty solution, you may consider this:


repeat
	set fApp to short name of (info for (path to frontmost application))
	try
		if not my Dialog(fApp) then exit repeat
	on error
		if not my Dialog("Finder") then exit repeat
	end try
end repeat
beep 3
-- script or facilitate IT contact here

to Dialog(tApp)
	tell application tApp
		activate
		set td to display dialog "You should really contact the IT department now..." buttons {" Cancel ", "OK"} default button 2 with icon 0 giving up after 2
		if gave up of td or button returned of td ≠ "OK" then return true
	end tell
	return false
end Dialog

Make sure that you can’t block the script with cmd-. and alike (to meet the desired annoyance level)