Script for displaying two dialogs at the same time

I like to download challenging scripts and try to figure out how they work and this one is really different. I copied the script from Bastiaan Boertien submitted on 10/31/10. I’ve tried for several days but I can’t get it to run right on my machine (iMac mid-2011, Mac OS X 10.6.8), either in AppleScript Editor or from a saved application on the desktop.

on cancelDialog()
	display dialog "Process is canceled"
end cancelDialog

on backgroundScript()
	script prototype
		property __forkID : missing value
		property __callBack : missing value
		property __applescriptCode : missing value
		on __construct()
			set __forkID to null
			set __callBack to null
			set __applescriptCode to null
		end __construct
		on setApplescriptCode(applescriptCode)
			set __applescriptCode to applescriptCode
		end setApplescriptCode
		on setCallBack(callBack)
			set __callBack to callBack
		end setCallBack
		on startFork()
			set theCommand to "osascript -e" & quoted form of __applescriptCode & " > /dev/null 2> /dev/null & 
           echo $!"
			set __forkID to (do shell script theCommand)
		end startFork
		on stopFork()
			do shell script ("kill " & __forkID & " > /dev/null 2> /dev/null" as string)
		end stopFork
	end script
	__construct() of prototype
	return prototype
end backgroundScript

set a to backgroundScript()
tell application \"finder\" to display dialog \"Hello World!\"
end repeat") of a
setCallBack(cancelDialog) of a
startFork() of a

delay 5

stopFork() of a

Here are the problems that I have had:

When I double click on the application, the Finder icon in the Dock bounces and a few seconds later the “Process is canceled” dialog window appears (at this point I move the dialog so I can see the other dialog if it appears). Icon continues to bounce (probably forever).

(1) If I click on the Finder desktop the bouncing icon stops, the Cancel dialog is still present, and the Hello dialog appears (so at this point both dialogs are on the screen).
(a) If I click on either the Cancel or OK buttons of the Hello dialog, the Hello dialog goes away and the Cancel dialog remains on the screen. If I click on either the Cancel or the OK button of the Cancel dialog the program quits.
(b) If I click on either the Cancel or OK buttons of the Cancel dialog, the Cancel dialog goes away and the Hello dialog remains on the screen. If I click on either the Cancel or the OK button of the Cancel dialog the program quits.

(2) If instead of clicking on the desktop I click on the Finder icon in the Dock everything happens as described in (1) above.

(3) In fact I discovered that if I simply hover the mouse over the icon in the Dock it does the same as in (1) above. I was very surprised at this because I have never seen it happen with any other of my programs (is this normal behavior for the Finder ?).

When I run the script from AppleScript Editor the Finder icon bounces and the Cancel dialog appears after a few seconds. If I dismiss the Cancel dialog by clicking either button the Finder icon continues to bounce. If I then click on the Finder desktop the Hello dialog appears. In other words with this way of running the program I have never gotten both dialogs on the screen at the same time (as above the order of appearance is reversed).

I would like to get this script to work just on general principals and it’s possible that I could use this method in some of my own scripts.

So my questions would be:

Is the script not compatible with 10.6.8 or is there a problem with timing on the latest iMac compared to older machines? If this is the case I will give up.

I would welcome any suggestions as to how I can change the script to get it to work or to find out where the problem is? I will try any suggestions, but I should say that I consider myself a rank beginner at debugging programs.


Model: iMac 2011
AppleScript: 2.1.2
Browser: Safari 533.21.1
Operating System: Mac OS X (10.6)

first of all I’ll hope you enjoyed reading my article…

What I was trying to explain in the article is not some nice and smooth code, it’s purpose is to show that you can fork code into a new process and control it by wrapping an script object around it. This code is useful for studio applications who lacks in multi threading but since you can use cocoa (also in scripts in Lion who comes very close) this “code forking” isn’t really needed anymore. I would advise to use NSThreads instead or performSelectorInBackground method when you can use AppleScriptObjC.

Did you save the script as an Application? That would be probably the problem. I just run the code in script editor when I wrote the article and never tested it as an standalone application. The problem would be the current application difference between those two. When you try to force to show the dialog “Process is canceled” in another process would solve the problem.

The first word in your script was “displayon”. I removed the word “display”, left “on” and moved the closing AppleScript tag to the end. Now it downloads and runs for me. (Obviously, with Admin privileges, I can edit a post – hope you don’t mind).

I read and enjoyed the article too, BW – you’ve seen me around as NovaScotian

BW, thank you for your quick reply. With such a long post it’s easy to loose track of the various things I tried and I apologise for that. As I stated I tried both running it from the Editor and saving the script as an application and then launching it by double clicking on it. The results were similar except that in running it from the Editor I could never get the two dialogs to appear at the same time.

I’m not sure what you mean by the last sentence of your post: “When you try to force to show the dialog “Process is canceled” in another process would solve the problem”.

I’ve never done any programming involving NSThread or AppleScriptObjC. I will see if I can find some references and read up on these things, but frankly I don’t know where to start.

Adam, I welcome your changes. Never having posted an applescript before, I saw immediately after I submitted it that I didn’t get the script in the “Open … Editor” field. I think I know what to do for next time.