Copy or move files using Finder, but from Terminal

To copy or move file to a different folder using Terminal, the obvious way is to use the cp and mv commands.

But for users who often put important information in the “Comments” section of the “Get Info” window of their files and folders, cp and mv have a drawback that comments are sometimes lost.

The only reliable way I have found so far to copy/move files using Terminal and preserve comments is to use AppleScript.

I have several snippets for that, but currently they work just to demonstrate the idea, and that is to say, they are not a script that you can use with arbitrary targets and destinations like cpx <target> <destination>. Here they are, just in case, and though I will be very appreciated for help to convert them into a script that will work like cpx <target> <destination>, this is not what my question is about.

Suppose you have

~/
|- dir1/
   |- dir2/
   |  |- dir3/
   |  |- file
   |
   |- dir4/
      |- dir5/

Then:

to copy the file to dir3, use:

osascript << SCRIPT
set currentFolder to "$(pwd)" as POSIX file as text
tell application "Finder"
  duplicate file (currentFolder & "file") to folder (currentFolder & "dir3:")
end tell
SCRIPT

to copy it to dir4, use either

osascript << SCRIPT
set currentFolder to "$(pwd)" as POSIX file as text
set parentFolder to "$(dirname $PWD)" as POSIX file as text
tell application "Finder"
  duplicate file (currentFolder & "file") to folder (parentFolder & "dir4:")
end tell
SCRIPT

or

osascript << SCRIPT
set currentFolder to "$(pwd)" as POSIX file as text
tell application "Finder"
  set parentFolder to (container of folder currentFolder) as text
  duplicate file (currentFolder & "file") to folder (parentFolder & "dir4:")
end tell
SCRIPT

And here is my question: Maybe I overcomplicate things and instead of using AppleScript, I can use something similar to open -a <app> <file>?

# this works. this is used to open a file using a specific app
open -a <yourapp> <yourfile>
# this doesn't work and just a demonstration of my idea
copy -a finder <target> <destination>
1 Like