Copy file to protected directory using AppleScript I'm trying to simulate Finder's file copy, but to a protected directory, using AppleScript.  The goal is to copy a single file but elevate when needed, like Finder will behave when doing it manually.

Initially I tried to use do shell script [...] with administrator privileges

This didn't work, I believe due to sandboxing, osascript the shell commands could read from my Desktop directory when run as me, but were blocked when run as administrator.


I tried to use the copy command, but quickly found that it's NOT for copying files.  This is confusing, many solutions are still using it.
I'm reading that duplicate is likely the keyword I should be focusing on but I'm struggling with using POSIX file and when using alias.

An example of a test without elevation:
set posixSrc to (POSIX file &quot;/Users/darwin/Desktop/test.txt&quot;) as alias
set posixDst to (POSIX file &quot;/Users/darwin/Desktop/test2.txt&quot;) as alias

tell application &quot;Finder&quot;
    duplicate posixSrc to posixDst
end tell

An example of a test with elevation:
set posixSrc to (POSIX file &quot;/Users/darwin/Desktop/test.txt&quot;) as alias
set posixDst to (POSIX file &quot;/Applications/My Special App.app&quot;) as alias

tell application &quot;Finder&quot;
    duplicate posixSrc to posixDst
end tell

Unfortunately, between POSIX and alias, I'm struggling to figure it all out.  Some errors I receive:
error &quot;Finder got an error: Handler can’t handle objects of this class.&quot; number -10010

error &quot;Can’t make \&quot;/Users/darwin/Desktop/test2.txt\&quot; into type alias.&quot; number -1700 from &quot;/Users/darwin/Desktop/test2.txt&quot; to alias

error &quot;Finder got an error: AppleEvent handler failed.&quot; number -10000 from file (file &quot;Macintosh HD:Users:darwin:Desktop:test.txt&quot;)

error &quot;Finder got an error: AppleEvent timed out.&quot; number -1712

I'm currently testing these with Script Editor but a solution in Terminal is welcome as well.
Got it...
tell application &quot;Finder&quot;
    set posixSource to (POSIX file &quot;/Users/darwin/Desktop/test.txt&quot; as alias)
    set posixDest to (POSIX file &quot;/Applications/My Special App.app/Contents/&quot; as alias)
    duplicate file posixSource to folder posixDest with replacing
end tell

... and the one-liner:
tell application &quot;Finder&quot; to duplicate file (POSIX file &quot;/Users/darwin/Desktop/test.txt&quot; as alias) to folder (POSIX file &quot;/Applications/My Special App.app/Contents/&quot; as alias) with replacing

Some important distinctions from failed attempts:

You can't alias a file which doesn't yet exist.  For destination, use the destination's parent folder instead.
You can't copy into the root of an Application bundle (e.g. My Special App.app).  You must copy into the My Special App.app/Contents instead.
Pay special attention to the keywords file and folder as they're provided to the duplicate command.
When a problem occurs, it can deadlock Script Editor.  Wrap your duplicate call in ignoring application responses [...] end ignoring to speed up testing which results in timeouts.
When converting to a one-liner you'll need to use the keyword to after the application name.