Photoshop, AppleScript & the original folder

I have a script that needs to copy a file from one directory to another and replace the existing file. Is first moving the file to trash the using duplicate the only way? Is this supposed to work?


set destpath to destdir & ":" & filename
set sourcepath to sourcedir & ":" & filename
move destpath as alias to trash
duplicate sourcepath as alias to destpath as alias

It seems to be having a problem using my path strings as folders. TIA.

Try doing a search for “replacing” without the quotes in the “AppleScript | OS X” forum…

NOTE: Srcoll down to Rob’s solution in both of the below links…

http://bbs.applescript.net/viewtopic.php?t=5190&highlight=replacing

http://bbs.applescript.net/viewtopic.php?t=4840&highlight=replacing

Thanks for the links. I tried the search using just “replacing” earlier but there were so many hits and the subject was no help on them that I gave up.

I think the “with replacing” corrects a problem I have not gotten to yet. I believe my problem is referencing the folder correctly in the ‘duplicate’ statement.


set dest_path to folder ("level 1:level 2:" & level_3) of disk "mydisk"
duplicate myfile to folder dest_path with replacing

I get an error that it cannot make some data into expected type. TIA.

Will something like this work?

or with a variable…

[This script was automatically tagged for color coded syntax by Script to Markup Code]

That is what I started off with thinking simple is best. However, I got a different error (I don’t recall what it was) but my book told me that I can’t use a disk in the path but have to use ‘of disk “mydisk”’. I am able now to access the folder contents using a path built that way but when I try to duplicate the file I get error I indicated earlier.

Is “mydisk” a different Volume, that is – is the file being moved to another hard drive or partition? Or is the file being moved to another folder on the same Volume?

Correct.

Then the second script posted above should work, just use the name of the Volume as the first item in the path.

[This script was automatically tagged for color coded syntax by Script to Markup Code]

NOTE: When you move a file from one Volume to another it’s copied, so the original file will still exist on Volume1.

You could get around that by using a do shell script…

do shell script "mv $HOME/desktop/memstat.txt /Volumes/VolumeName/$HOME"

$HOME is a terminal variable for the users home directory. The file will be moved and it will replace any file with the same name.

Please test it on a copy of a file – not the original --, moving files via the shell can eliminate the resource fork of a file rendering it useless. Sometimes it’s better to use “ditto -c --rsrc” instead of “mv”, you can also use CpMac if you have Apple’s Developer Tools installed.

I’ve gone full circle. Your first suggestion is pretty much where I started. It tells me it “can’t make some data into expected type.” This is where my book says, for version 10.3, I can’t use a disk in the path but must use ‘of disk’ instead.

I haven’t done anything with shell scripts but if that is the only way to get it to work (seems absurd that Applescript can’t do it), then I’ll write the whole think as a script and abandon Applescript.

P.S. To your shell script suggestion. Why can I just use ‘cp’?

Regarding the original question, copy a file to a destination replacing the existing one is a very easy task. In Finder’s terminology, we need a “duplicate”:

set fileToCopy to alias "path:to:file.txt" --> original file
set destinationFolder to alias "path2:to2:" --> destination folder

tell application "Finder" to duplicate fileToCopy to destinationFolder with replacing

I find “file references” in the Finder not-very-good, as it can accept lots of different inputs, as you can see here:
http://macscripter.net/faq/finder.php?id=P18
It will also accept “hierarchical” file references (its own objects), as for example:

I allways prefer “alias” as the best file specification. There are many reasons, as described in the mentioned faq, and my experience is that other file specifications will not allways work OK.

Since the arrive of OS X, Finder scripting, as I understand it, has been a little… buggy? unstable? terribly slow? So, for a simple file-move I allways use a “do shell script”, as Greg points. As Greg also said, if you move data to a different volume you may use “ditto” instead of “cp” or “mv”, as “ditto” will preserve the resource fork and metadata of your files (if you need it).

Thanks for all the suggestions but they all result in the same error, “can’t make some data into expected type.”

What is the value of the level_3 variable? After you run the following line, what is the value of dest_path?

tell application "Finder" to set dest_path to folder ("level 1:level 2:" & level_3) of disk "mydisk"

– Rob

‘level_3’ is ‘General’. When I ‘display dialog dest_path as string’ I get:
‘mydisk:level 1:level 2:General’.

P.S. Adding ‘:’ to the end of the ‘dest_path’ doesn’t seem to matter.

Got it!!! :stuck_out_tongue:
Given the previous discussion, can anyone see my stupid mistake (I wonder why no one picked it up earlier :))?


move file source_path to folder dest_path with replacing

I haven’t consumed enough coffee to detect what was wrong but I’m glad it’s working now. :wink:

Happy holidays!
Rob

OK, here’s the solution:


move source_path to dest_path with replacing 

Ahhh, the old “trying to move a string to another string” dilemma! :wink:

– Rob