need a pause/delay in my script?

I have a series of applescript I used tied to quickeys to show/hide different sets of applications (my version of “spaces”). But the scripts seem to be confused and I think it could be because they aren’t executed in order of the script before the next command shows up. The following is an example:

tell application "System Events"
	set visible of (every process whose visible is false) to true
end tell


tell application "System Events"
	set visible of process "Finder" to false
	set visible of process "Nambu" to false
	set visible of process "Mail" to false
	set visible of process "Adium" to false
	set visible of process "iBiz" to false
	set visible of process "iBank" to false
	set visible of process "pandora" to false
	set visible of process "Basecamp" to false
	set visible of process "iTunes" to false
end tell

The problem with this script is that by the time it gets to the section that sets all the other ones to “false” it still hasn’t totally executed the first section which makes everything visible. The result I get is that the second chunk doesn’t hide the apps I want it to. If I comment out the first chunk, the second chunk works perfectly. Do I need to build a delay or something into the second chunk so it can execute? Or what’s the best way to target specific apps to be invisible when you want everything else to be visible? This has been bothering me for some time now and I would LOVE a solution!

What I did is to check if all processes are visible after telling System Events to show them. It does so for a maximum of 3 seconds before continuing anyway. If this still won’t work on your Mac, then simply insert a delay in between showing all processes and hiding the ones you want hidden. The command is simply the word delay followed by a number representing the seconds to delay (e.g. delay 0.5 or delay 3).


on run
	
	-- Specify the names of the processes to hide
	set _processNames to {"Finder", "Nambu", "Mail", "Adium", "iBiz", "iBank", "pandora", "Basecamp", "iTunes"}
	
	-- Show all proceses
	showAllProcesses()
	
	-- Hide the specified processes
	hideProcesses(_processNames)
	
end run





on showAllProcesses()
	
	-- Show all processes
	-- Note: The visible property has no effect on background only processes
	tell application "System Events"
		set visible of (every process whose visible is false and background only is false) to true
	end tell
	
	-- Make sure all hideable processes are visible before continuing
	repeat 30 times -- To avoid infinite loop, time out after 3 seconds
		if hiddenProcesses() is {} then exit repeat
		delay 0.1
	end repeat
	
end showAllProcesses

on hiddenProcesses()
	
	-- Return all hidden processes
	-- Note: Background only processes never have their visible property set to false
	tell application "System Events"
		return (every process whose visible is false and background only is false)
	end tell
	
end hiddenProcesses

on hideProcesses(_listOfProcessNames)
	
	-- Hide multiple processes using the specified list of process names
	repeat with _processName in _listOfProcessNames
		hideProcess(_processName)
	end repeat
	
end hideProcesses

on hideProcess(_processName)
	
	tell application "System Events"
		-- Make sure the process is running before continuing
		if (exists process _processName) then
			-- Make sure the process is actually visible before hiding it
			if (visible of process _processName) is true then
				-- Hide the process
				set visible of process _processName to false
			end if
		end if
	end tell
end hideProcess