Giving Up in the Background

Here’s a simple scriptlet:

on run
	delay 5
	display dialog "Continue?" buttons {"Yes", "Cancel"} default button 1 giving up after 5
	beep
end run

Save it as an app and run it; it works as expected – unless the applet is in the background. If you switch to a different app during the delay, the script does not continue after 5 seconds.

My question: What’s the best method to make the script continue in the background?

TIA, Alex

you can tell the frontmost application to do the display dialog command

on run
	delay 5
	set front_app to name of (info for (path to frontmost application))
	tell application front_app to display dialog "Continue?" buttons {"Yes", "Cancel"} default button 1 giving up after 5
	beep
end run

Or, you can save your script as a stay-open application like this:


on idle
	display dialog "Continue?" buttons {"Yes", "Cancel"} default button 1 giving up after 5
	beep 3
	return 6 -- runs every six seconds
end idle

Combine this with telling the frontmost app to display the dialog and it will appear every 6 seconds for five.

Thanks, but that’s not what I want. I want to continue working in the frontmost app without any interruption or dialogue, if the applet is in the background.

Alex

Thanks, but it doesn’t seem like what I’m looking for. Maybe I should elaborate.

The specific function of the script is to batch process a series of audio files with sox (which can be slow). So I drop a set of files on the applet, and it starts processing the first file. When it’s done, it asks the user, “Done with file 1, continue with file 2?”. If the user doesn’t answer, it goes on to the next file automatically. The point is to give the user an option to stop processing without having to kill sox, but also without making user input sine qua non. The problem is that it only works when the applet is in the foreground. It doesn’t seem to me that using the idle handler is the solution, and having the frontmost app displaying the dialogue isn’t either, because it interrupts the user – which is precisely what I want to avoid.

Alex

I’m testing your original script and I am finding that it is continuing even if I switch to different programs and start doing other things?? What OS are you in? Is the actual code of your full script different in any way from the sample you posted?

I wanted to experiment with some other options but the code you posted is working for me…

Model: G5 Tower (not Intel) - Script Editor ver 2.1.1
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

ok, what’s about that:

on run
	delay 5
	if me is frontmost then
		display dialog "Continue?" buttons {"Yes", "Cancel"} default button 1 giving up after 5
		beep
	end if
end run

the dialog appears only if the applet is frontmost

For me this script bails out in the background:


on run
	delay 5
	with timeout of 5 seconds
		try
			display dialog "Continue?" buttons {"Yes", "Cancel"} default button 1 giving up after 5
			beep
		end try
	end timeout
end run

Thanks, but it generates an error: “Can’t get frontmost.” Perhaps it works only in Tiger? I should have included system info in the OP.

Alex

AppleScript: 1.9.3
Browser: Safari 312.3
Operating System: Mac OS X (10.3.9)

Thanks, but I’m afraid it doesn’t for me. Timeout doesn’t do the trick in the background for me. I should have included the system info to begin with, I guess.

AppleScript: 1.9.3
Browser: Safari 312.3
Operating System: Mac OS X (10.3.9)

Well, I wouldn’t retrograde to 10.3.9 for now but I have never had any issue with AS time outs since 10.1.
So it should work - try playing with the timing values…

Hi alexn,

Try this:


on open these_items
	set my_proc to name of (info for (path to me))
	if my_proc ends with ".app" then
		set my_proc to text 1 thru -5 of my_proc
	end if
	set c to count these_items
	repeat with i from 1 to c
		set this_item to item i of these_items
		-- do something with this item
		set n to name of (info for this_item)
		say "The name of item " & i & " is " & n
		try
			if i is not c then
				tell application "System Events"
					set this_proc to (name of first process whose frontmost is true)
				end tell
				if this_proc is my_proc then
					display dialog ¬
						"Continue on with item " & (i + 1) & "?" giving up after 5
				end if
			end if
		on error err_mess
			display dialog err_mess
			exit repeat
		end try
	end repeat
end open

gl,

Oops, forgot to readjust the error handler:


on open these_items
	set my_proc to name of (info for (path to me))
	if my_proc ends with ".app" then
		set my_proc to text 1 thru -5 of my_proc
	end if
	set c to count these_items
	repeat with i from 1 to c
		set this_item to item i of these_items
		-- do something with this item
		set n to name of (info for this_item)
		say "The name of item " & i & " is " & n
		if i is not c then
			tell application "System Events"
				set this_proc to (name of first process whose frontmost is true)
			end tell
			if this_proc is my_proc then
				try
					display dialog ¬
						"Continue on with item " & (i + 1) & "?" giving up after 5
				on error
					exit repeat
				end try
			end if
		end if
	end repeat
end open

I modified the script a little:


on open these_items
	set my_name to name of (info for (path to me))
	set c to count these_items
	repeat with i from 1 to c
		set this_item to item i of these_items
		-- do something with this item 
		set n to name of (info for this_item)
		say "The name of item " & i & " is " & n
		if i is not c then
			set front_app to name of (info for (path to frontmost application))
			if front_app is my_name then
				try
					display dialog ¬
						"Continue on with item " & (i + 1) & "?" giving up after 5
				on error
					exit repeat
				end try
			end if
		end if
	end repeat
end open

Thanks.

I did do a bit of testing, including trying it on a fresh user account. The results are consistent – the timeout clause works as expected, except when in the background.

Alex

Many thanks. Based on your suggestions, I came up with this modification of the original scriptlet, which does (finally!) work:

on run
	set myName to name of (info for (path to me))
	delay 5
	if myName = name of (info for (path to frontmost application)) then display dialog "Continue?" buttons {"Yes", "Cancel"} default button 1 giving up after 5
	beep
end run

Alex