copy a image/binary file on a mac
Ben Rubinstein
benr_mc at cogapp.com
Sat Jan 19 13:18:00 EST 2002
on 19/1/02 4:01 PM, Klaus Major at k_major at osnabrueck.netsurf.de wrote:
> (long description of how to copy a binary file on a Mac, without losing
> the type and creator codes snipped)
This is perfectly good as far as it goes, but will still lose anything that
might be in the resource fork of the file. If you're going to go to that
much trouble to make a mac-specific file copy routine, you might as well get
the Finder to do it for you, which is a complete solution.
Some gotcha's along the way: AppleScript of course doesn't understand about
the Revolution use of the Unix filename conventions, so you have to switch
them; and as far as I could tell, there isn't directly a way to tell the
Finder to copy a file with right destination and name. So you have to copy
it, then rename it.
The following script works as far as I can tell, with two exceptions I can
foresee - see below. It has the advantage over the variations of using "put
URL ... into URL" that it doesn't require Rev to have enough memory to load
the whole file at one lump; it preserves the type and creator codes, but
also other information like the file date; and it preserves the resource
fork, which I don't think is possible using any solution entirely in
Transcript.
function copyFileMac srcPath, dstPath
-- first need to work out whether the destination is available
-- (and whether the the source file is valid)
if there is not a file srcPath then
return "Source file does not exist"
else if there is a file dstPath then
return "Destination file exists!"
-- an alternative implementation would delete the existing file
end if
-- we also only deal with absolute paths, only with files
if (first char of srcPath <> "/") or (first char of dstPath <> "/") then
return "invalid source or destination path (must be absolute)"
else if (last char of srcPath = "/") or (last char of dstPath = "/")
then
return "invalid source or destination path (must be files)"
end if
-- separate file names from folder paths, find out if source
-- and destination are in the same folder, check source folder
-- exists
set the itemDelimiter to "/"
put last item of dstPath into dstName
delete last item of dstPath
if there is not a folder dstPath then
return "invalid destination path (folder doesn't exist)"
end if
put ((item 1 to -2 of srcPath) = dstPath) into sameFolder
put resPathToMacPath(dstPath) into dstPath
put resPathToMacPath(srcPath) into srcPath
-- create the applescript
put "tell application" && quote & "Finder" & quote & return into asCom
if sameFolder then
put "duplicate file" && quote & srcPath & quote & return after asCom
else
put "copy file" && quote & srcPath & quote \
&& "to folder" && quote & dstPath & quote & return after asCom
end if
put "set newFile to the result" & return after asCom
put "set the name of newFile to" && quote & dstName & quote \
& return after asCom
put "end tell" & return after asCom
-- do the work
do ascom as applescript
return the result
end copyFileMac
function resPathToMacPath p --> p
delete first char of p -- "/"
-- can't just do [replace "/" with ":" in p], because
-- wouldn't handle real "/" in file/folder names
set the itemDelimiter to "/"
put empty into macPath
repeat for each item i in p
replace ":" with "/" in i
put ":" & i after macPath
end repeat
delete first char of macPath -- ":"
return macPath
end resPathToMacPath
One exception I can foresee is that quotes within the path will not be
handled correctly. I imagine that this could be handled - I don't know
enough about how characters can be escaped in AppleScript, or how variables
can be passed in from Transcript. Secondly, because it first copies the
file into the destination directory, then renames it, it would fail if the
destination directory already has a file with the same name as the source
(but not the destination name). This could obviously be scripted round.
Ben Rubinstein | Email: benr_mc at cogapp.com
Cognitive Applications Ltd | Phone: +44 (0)1273-821600
http://www.cogapp.com | Fax : +44 (0)1273-728866
More information about the use-livecode
mailing list