"giving up after..." in login script gives up too soon

I am working on a script, saved as an application, that checks the name of the startup disk then, if it’s my internal drive, starts apps in sequence to reduce load, put menu items in my preferred order, and make sure important (to me) apps are started.

If the startup disk is one of my clones, I probably won’t want those apps running, but, just in case, I put in an alert - giving up after 5 seconds - so I could choose to continue. But it flashes for a fraction of a second. Giving up after 10 seconds gives me about 2 seconds (using the “one Mississippi” method of timing) and 13 gives me about 5. If I run the application after login is complete, it gives up after the correct delay.

Could somebody explain why this is happening? I am, as usual, working on this in the middle of the night so my apologies if I am missing something obvious.

thanks

j

EDIT: This morning I discovered that the menu items check causes a hang when logging in after a restart. It works fine if I log out, then in, without a restart, so I guess I have more to figure out.


property menuItems : {"AirPort Menu Extra", missing value, "iChat menu extra", "AppleScript menu extra", "battery menu extra", "user menu extra"} --missing value is screen lock

property scriptDisk : "Macintosh HD"

property appList1 : {"Hey Folders!", "Meteorologist", "Google Notifier", "ClamXavSentry", "Alarm Clock", "Butler", "Edgies"}

property appList2 : {"BackTrack", "Email Backup Pro", "ClamXav"}

(* Wait for Menu Items before continuing*)
(*Borrowed from http://www.macosxhints.com/article.php?story=20060925214542457*)
tell application "System Events"
	
	tell process "SystemUIServer"
		repeat until value of attribute "AXDescription" of menu bar items of menu bar 1 is menuItems
			delay 0.1
		end repeat
		
	end tell
end tell

(*Check Startup Disk before continuing*)
tell application "Finder"
	
	set bootDisk to (get name of startup disk)
	if bootDisk is not scriptDisk then
		
		set runScript to button returned of (display alert return & "Startup script cancelled because boot volume is" & return & "\"" & (get name of startup disk) & "\"" message "Run startup script" & space & "for" & space & "\"" & bootDisk & "\"" & space & "?" buttons {"Yes", "No"} default button "No" giving up after 13)
		
		if runScript is "yes" then
			my runMyScript()
		end if
		
	else
		my runMyScript()
	end if
end tell

on runMyScript()
	repeat with anApp in appList1
		tell application anApp to activate
		delay 1.25
	end repeat
	
	delay 5
	
	tell application "Finder"
		set {d1, d2, d3, d4} to (desktop's window's bounds)
		set margins to {d1 + 22, d2 + 44, d3 - 22, d4 - 22}
	end tell
	
	tell application "Mail"
		activate
		
		repeat until window 1 exists
			delay 0.1
		end repeat
		set bounds of window 1 to margins
		close window 1
	end tell
	
	repeat with anApp in appList2
		tell application anApp
			activate
			tell application "System Events"
				repeat until exists process anApp
					delay 0.01
				end repeat
				tell process anApp to set visible to false
			end tell
		end tell
	end repeat
	
	tell application "Finder" to activate
	
end runMyScript

I’m not an expert but I believe that, first a “delay” must be a whole number. I’ve not seen any delays other than whole values. As for startup scripts, I’ve seen them have a significant delay until all the startup processes get underway. Maybe someone else has a more definitive answer, but I would not count on a short delay to be reliable.

I use a delay of 0.5 all the time so a decimal value shouldn’t cause a problem.

The only thought I have is that maybe there are things in starting up that prevent the dialog box from showing up even though the “giving up” delay is already counting down. If you put a big delay right before your display dialog or a display dialog, maybe you can test to see if by waiting for 10 seconds or something the delay of 13 will actually give you 13 seconds. It would at least confirm whether the problem was in the “giving up 13” or in something leading up to it.

That’s all I got…

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

Thank you both for the suggestions. I knew the fraction of a second delay wasn’t the problem - but I had already tried it at a full second because I was trying everything.

Working on my clone, I tried the delay both before and after the SystemUIServer tell block - neither worked. So I put it in the Finder tell block, right after “tell application Finder” and that worked. I gradually reduced the delay to .01 but it still worked, which seemed odd. So I tried .001. and Scipt Editor “quit unexpectedly” when I tried to compile. I relaunched it, then tried the script again - forgetting that I had lost the changes.

It worked. Then I realized this was the unchanged version. After some eye blinling and head scratching, I decided to zip a copy and go back to my regular startup drive. I restarted, expecting to get the hang because I hadn’t replaced the script. It didn’t hang.

Then I tried replacing the script with the zipped version. That one hung. I went back to the old one. It still worked.

So now the script works as expected, without any changes. I don’t get it, but I’m going to leave it alone for a while and play Scrabble.

As a general mark - I always substitute “delay x” for

do shell script "sleep x"

It consumers less CPU power, and seems more reliable & exact (using system clocking?)

Thanks, I’ll keep that in mind.