Copying a file that was just created

In FileMaker 8 I save a backup copy of my file, then I run the script below which is suppose to take that backup copy and copy it to a folder on my laptop. The script works with other files but it doesn’t always work with the backup file I just created. I get the error message “The operation could not be completed”. I assume the problem is FileMaker not letting go of the file it just created so I tried putting a delay in the script, I also tried creating a second dummy backup file thinking FileMaker would hang on to that file while I copied the real backup file; neither approach worked.
Any suggestions?

tell application "Finder"
	copy file "Mac OSX:test.fp7" to folder "Laptop:my folder"
end tell

maybe you could try this:


set sourceFile to quoted form of POSIX path of "Mac OSX:test.fp7"
set destinationFolder to quoted form of POSIX path of "Laptop:my folder"
do shell script "cp " & sourceFile & space & destinationFolder

Thanks Dom, that did the trick.
I’m curious though, I assume doing a shell script is like executing a command in DOS back when Win3.1 was around, so why does it work but a normal tell finder to copy something doesn’t? It wouldn’t surprise me to have to do this on Win, but on the Mac?
Is there a chance I’m going to cause file corruption by doing this CP thing (I assume it stands for copy)?

You assume correctly, FP.

To check out the command in detail, type man cp in a Terminal window - or take a peek at the online manual page for cp(1).

But, as Jacques says, you should really try Finder’s ‘duplicate’ command - since ‘copy’ currently does nothing, and is apparently destined to be used (at some point in the future) for copying selected items to the clipboard.

Not to beat a dead horse, but what do you mean by copy doesn’t work? The script I’m currently running in 10.3.9 is

FileCopy("Mac OSX:Test.fp7", "Laptop:myfolder")

on FileCopy(FileCopyFromPath, FileCopyToFolder)
	tell application "Finder" to copy file FileCopyFromPath to folder FileCopyToFolder
end FileCopy

The script works for copying files between my computers with the exception of the FileMaker file that I just saved a copy of and then try to copy to my laptop.

Understood, FP. The ‘copy’ command has worked in the past as a synonym for ‘duplicate’. However, many considered it confusing - especially since the term is also used for one or two other operations. When you upgrade to Tiger, I think you’ll find any existing scripts that use the command will break. In 10.4, ‘copy’ no longer appears in Finder’s AS dictionary for this purpose - and, if used in a script, fails silently. Just to confirm, this from the latest Finder AS dictionary (Finder 10.4.4, AS 1.10.3, OS 10.4.5):

Using ‘duplicate’ may not fix your particular problem (especially if ‘copy’ still works generally for you). But it might be worth a try - and, for those scripts that currently use ‘copy’ successfully, it’s certainly worth considering a gradual move over to ‘duplicate’ now…
:slight_smile:

FP,

the difference between AppleScript’s ‘copy’ or ‘duplicate’ and the shellcommand (UNIX, not WIN!!) is, that the Applescript commands will check if the file is already opened for writing before processing - ‘cp’ doesn’t.
But as Kai and Jacques said - it’s a good advise to try with applescript’s duplicate first, since it’s safer. Copying a file while another application writes to it might lead to corrupted data :frowning:

D.

One small thing I noticed - the command as shown in Dominik’s example above:


do shell script "cp " & sourceFile & space & destinationFolder

Will make a copy of the file in the new location, but it gives all appearances of being a ‘new’ file. Adding the “-p” flag in the command will preserve the file creation/modification dates, etc. So the command would look like:


do shell script "cp -p " & sourceFile & space & destinationFolder

We depend on modification dates quite a bit so that one caught my eye. Might not matter much to other folk. :wink:

The fact that ‘cp’ doesn’t check to see if the file is in use is a good thing to know, I can’t have this file go bad on me, database files are very problematic that way, so I need to use the duplicate command.

I’ve been testing the duplicate command and came to the conclusion the problem seems to be the OS, not FileMaker. Here’s what’s happening:

  • I duplicate the backup file, it already existed from last time (everything works)
  • from within FileMaker I save a copy of the file
  • I run the duplicate script again from Script Editor (error "The operation could not be completed)
  • quit FileMaker, run script again (same error)
  • open the folder where the backup file was created
  • run script again (it works)

I assume I need to force the OS to somehow update it’s list of files in that folder, any suggestions?

Just found the ‘update’ command in the Finder dictionary, that fixed the problem.