| 
									
										
										
										
											1996-09-20 15:26:20 +00:00
										 |  |  | """Utility routines depending on the finder.""" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-08-20 19:56:13 +00:00
										 |  |  | import Finder | 
					
						
							| 
									
										
										
										
											1998-10-15 14:02:54 +00:00
										 |  |  | import AppleEvents | 
					
						
							| 
									
										
										
										
											1996-09-20 15:26:20 +00:00
										 |  |  | import aetools | 
					
						
							|  |  |  | import MacOS | 
					
						
							|  |  |  | import sys | 
					
						
							|  |  |  | import macfs | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | _finder_talker = None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def _getfinder(): | 
					
						
							|  |  |  | 	global _finder_talker | 
					
						
							|  |  |  | 	if not _finder_talker: | 
					
						
							| 
									
										
										
										
											2000-08-20 19:56:13 +00:00
										 |  |  | 		_finder_talker = Finder.Finder() | 
					
						
							| 
									
										
										
										
											1998-10-15 14:02:54 +00:00
										 |  |  | 	_finder_talker.send_flags = ( _finder_talker.send_flags |  | 
					
						
							|  |  |  | 		AppleEvents.kAECanInteract | AppleEvents.kAECanSwitchLayer) | 
					
						
							| 
									
										
										
										
											1996-09-20 15:26:20 +00:00
										 |  |  | 	return _finder_talker | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | def launch(file): | 
					
						
							|  |  |  | 	"""Open a file thru the finder. Specify file by name or fsspec""" | 
					
						
							|  |  |  | 	finder = _getfinder() | 
					
						
							|  |  |  | 	fss = macfs.FSSpec(file) | 
					
						
							| 
									
										
										
										
											2000-08-20 19:56:13 +00:00
										 |  |  | 	return finder.open(fss) | 
					
						
							| 
									
										
										
										
											1996-09-20 15:26:20 +00:00
										 |  |  | 	 | 
					
						
							|  |  |  | def Print(file): | 
					
						
							|  |  |  | 	"""Print a file thru the finder. Specify file by name or fsspec""" | 
					
						
							|  |  |  | 	finder = _getfinder() | 
					
						
							|  |  |  | 	fss = macfs.FSSpec(file) | 
					
						
							| 
									
										
										
										
											2000-08-20 19:56:13 +00:00
										 |  |  | 	return finder._print(fss) | 
					
						
							| 
									
										
										
										
											1996-09-20 15:26:20 +00:00
										 |  |  | 	 | 
					
						
							|  |  |  | def copy(src, dstdir): | 
					
						
							|  |  |  | 	"""Copy a file to a folder""" | 
					
						
							|  |  |  | 	finder = _getfinder() | 
					
						
							| 
									
										
										
										
											2000-09-22 12:17:14 +00:00
										 |  |  | 	if type(src) == type([]): | 
					
						
							|  |  |  | 		src_fss = [] | 
					
						
							|  |  |  | 		for s in src: | 
					
						
							|  |  |  | 			src_fss.append(macfs.FSSpec(s)) | 
					
						
							|  |  |  | 	else: | 
					
						
							|  |  |  | 		src_fss = macfs.FSSpec(src) | 
					
						
							| 
									
										
										
										
											1996-09-20 15:26:20 +00:00
										 |  |  | 	dst_fss = macfs.FSSpec(dstdir) | 
					
						
							| 
									
										
										
										
											2000-08-20 19:56:13 +00:00
										 |  |  | 	return finder.duplicate(src_fss, to=dst_fss) | 
					
						
							| 
									
										
										
										
											1996-09-20 15:26:20 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | def move(src, dstdir): | 
					
						
							|  |  |  | 	"""Move a file to a folder""" | 
					
						
							|  |  |  | 	finder = _getfinder() | 
					
						
							| 
									
										
										
										
											2000-09-22 12:17:14 +00:00
										 |  |  | 	if type(src) == type([]): | 
					
						
							|  |  |  | 		src_fss = [] | 
					
						
							|  |  |  | 		for s in src: | 
					
						
							|  |  |  | 			src_fss.append(macfs.FSSpec(s)) | 
					
						
							|  |  |  | 	else: | 
					
						
							|  |  |  | 		src_fss = macfs.FSSpec(src) | 
					
						
							| 
									
										
										
										
											1996-09-20 15:26:20 +00:00
										 |  |  | 	dst_fss = macfs.FSSpec(dstdir) | 
					
						
							| 
									
										
										
										
											2000-08-20 19:56:13 +00:00
										 |  |  | 	return finder.move(src_fss, to=dst_fss) | 
					
						
							| 
									
										
										
										
											1996-09-20 15:26:20 +00:00
										 |  |  | 	 | 
					
						
							|  |  |  | def sleep(): | 
					
						
							|  |  |  | 	"""Put the mac to sleep""" | 
					
						
							|  |  |  | 	finder = _getfinder() | 
					
						
							|  |  |  | 	finder.sleep() | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | def shutdown(): | 
					
						
							|  |  |  | 	"""Shut the mac down""" | 
					
						
							|  |  |  | 	finder = _getfinder() | 
					
						
							|  |  |  | 	finder.shut_down() | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | def restart(): | 
					
						
							|  |  |  | 	"""Restart the mac""" | 
					
						
							|  |  |  | 	finder = _getfinder() | 
					
						
							|  |  |  | 	finder.restart() | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def main(): | 
					
						
							|  |  |  | 	print 'Testing launch...' | 
					
						
							|  |  |  | 	fss, ok = macfs.PromptGetFile('File to launch:') | 
					
						
							|  |  |  | 	if ok: | 
					
						
							|  |  |  | 		result = launch(fss) | 
					
						
							|  |  |  | 		if result: | 
					
						
							|  |  |  | 			print 'Result: ', result | 
					
						
							|  |  |  | 		print 'Press return-', | 
					
						
							|  |  |  | 		sys.stdin.readline() | 
					
						
							|  |  |  | 	print 'Testing print...' | 
					
						
							|  |  |  | 	fss, ok = macfs.PromptGetFile('File to print:') | 
					
						
							|  |  |  | 	if ok: | 
					
						
							|  |  |  | 		result = Print(fss) | 
					
						
							|  |  |  | 		if result: | 
					
						
							|  |  |  | 			print 'Result: ', result | 
					
						
							|  |  |  | 		print 'Press return-', | 
					
						
							|  |  |  | 		sys.stdin.readline() | 
					
						
							|  |  |  | 	print 'Testing copy...' | 
					
						
							|  |  |  | 	fss, ok = macfs.PromptGetFile('File to copy:') | 
					
						
							|  |  |  | 	if ok: | 
					
						
							|  |  |  | 		dfss, ok = macfs.GetDirectory() | 
					
						
							|  |  |  | 		if ok: | 
					
						
							|  |  |  | 			result = copy(fss, dfss) | 
					
						
							|  |  |  | 			if result: | 
					
						
							|  |  |  | 				print 'Result:', result | 
					
						
							|  |  |  | 			print 'Press return-', | 
					
						
							|  |  |  | 			sys.stdin.readline() | 
					
						
							|  |  |  | 	print 'Testing move...' | 
					
						
							|  |  |  | 	fss, ok = macfs.PromptGetFile('File to move:') | 
					
						
							|  |  |  | 	if ok: | 
					
						
							|  |  |  | 		dfss, ok = macfs.GetDirectory() | 
					
						
							|  |  |  | 		if ok: | 
					
						
							|  |  |  | 			result = move(fss, dfss) | 
					
						
							|  |  |  | 			if result: | 
					
						
							|  |  |  | 				print 'Result:', result | 
					
						
							|  |  |  | 			print 'Press return-', | 
					
						
							|  |  |  | 			sys.stdin.readline() | 
					
						
							|  |  |  | 	import EasyDialogs | 
					
						
							|  |  |  | 	print 'Testing sleep...' | 
					
						
							|  |  |  | 	if EasyDialogs.AskYesNoCancel('Sleep?') > 0: | 
					
						
							|  |  |  | 		result = sleep() | 
					
						
							|  |  |  | 		if result: | 
					
						
							|  |  |  | 			print 'Result:', result | 
					
						
							|  |  |  | 		print 'Press return-', | 
					
						
							|  |  |  | 		sys.stdin.readline() | 
					
						
							|  |  |  | 	print 'Testing shutdown...' | 
					
						
							|  |  |  | 	if EasyDialogs.AskYesNoCancel('Shut down?') > 0: | 
					
						
							|  |  |  | 		result = shutdown() | 
					
						
							|  |  |  | 		if result: | 
					
						
							|  |  |  | 			print 'Result:', result | 
					
						
							|  |  |  | 		print 'Press return-', | 
					
						
							|  |  |  | 		sys.stdin.readline() | 
					
						
							|  |  |  | 	print 'Testing restart...' | 
					
						
							|  |  |  | 	if EasyDialogs.AskYesNoCancel('Restart?') > 0: | 
					
						
							|  |  |  | 		result = restart() | 
					
						
							|  |  |  | 		if result: | 
					
						
							|  |  |  | 			print 'Result:', result | 
					
						
							|  |  |  | 		print 'Press return-', | 
					
						
							|  |  |  | 		sys.stdin.readline() | 
					
						
							|  |  |  | 		 | 
					
						
							|  |  |  | if __name__ == '__main__': | 
					
						
							|  |  |  | 	main() | 
					
						
							|  |  |  | 	 |