Copy file to protected directory using AppleScript

Solution 1:

Got it...

tell application "Finder"
    set posixSource to (POSIX file "/Users/darwin/Desktop/test.txt" as alias)
    set posixDest to (POSIX file "/Applications/My Special App.app/Contents/" as alias)
    duplicate file posixSource to folder posixDest with replacing
end tell

... and the one-liner:

tell application "Finder" to duplicate file (POSIX file "/Users/darwin/Desktop/test.txt" as alias) to folder (POSIX file "/Applications/My Special App.app/Contents/" 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.