#	TransferCkid - Move the ckid resource from fromFile to toFile.
#
#	Usage:	TransferCKID fromFile toFile
#
#	TransferCKID moves the ckid resource (if it exists) from the
#	first file to the second file. 
#
#	© Apple Computer, Inc. 1988 - 1993
#	All rights reserved.

Set Exit 0
Unset CaseSensitive

#	Check the parameters

IF {#} ≠ 2
	Echo "### Usage - {0} fromFile toFile" > dev:stderr
	Exit 1
END
	
#	Check if files exist

FOR name in "{1}" "{2}"
	IF "`Exists -f "{name}"`"!=""
		Set curPath `Files -fr "{name}"`
		Set path"{Name}" "{curPath}"
		Set wind"{Name}" 0
		FOR i in `Windows`
			IF "{curPath}" =~ /"{i}"/		## Make NOT case-sensitive
				Set wind"{Name}" 1
			END
		END
	ELSE	
		IF "`Exists -d "{name}"`" == ""
			Echo "### {0}" - File "'{name}'" does not exist. >> dev:stderr
		ELSE
			Echo "### {0}" - File "'{name}'" is a folder. >> dev:stderr
		END
		Exit 2
	END
END
	

	#	Transfer the ckid
	
	Echo "Include ∂"{1}∂" 'ckid';" | Rez -m -a -o "{2}" -noResolve	## copy resource into destination
	IF {Status} == 0
		Echo "Delete 'ckid';" | Rez -a -o "{1}"		## delete it from source
		FOR name in "{1}" "{2}"						## refresh relevant windows for projector icon.
			(evaluate "`set wind"{name}"`" =~ /set ?«0,1»wind{name}?«0,1» (≈)®1/) >> Dev:Null
			IF "{®1}"								## is this an open window?
				Execute `Format "{name}"` ∑dev:null	## Force the shell to recognize that the file's been changed
				Save "{name}"						## Force the shell to save the file to disk
				(evaluate "`set path"{name}"`" =~ /set ?«0,1»path{name}?«0,1» (≈)®2/) >> Dev:Null
				IF "{®2}" =~ /"{active}"/			## is this our frontmost window?
					Close "{name}"
					Open "{name}"
				ELSE
					Close "{name}"
					Target "{name}"
				END
			END
		END
	END
