2

I’m trying to do a very simple thing — move (or duplicate) a file using JavaScript for Automation introduced with OS X Yosemite.

So far I have something like this.

finder = Application("Finder")

finder.move(Path("/Users/user/Source/file.pdf"), {
    to: Path("/Users/user/Destination/file.pdf"),
    replacing: true
})

The result is not great.

Error -1728: Can't get object.

Of course I can just use something like doShellScript("mv source destination") but Finder + JAX solution seems to be better.

3 Answers3

3

Finder’s move and duplicate actions work just fine with JXA Path objects. The reason your code fails it that the to argument these actions expect is a path to a folder while you are providing the path to a file. This will work:

finder = Application("Finder")

finder.move(Path("/Users/user/Source/file.pdf"), {
    to: Path("/Users/user/Destination/"),
    replacing: true
})
kopischke
  • 3,393
  • 1
  • 21
  • 40
0

Yeah, it's a mess with the JXA and the Finder. I think the problem lies in the Finder loving Aliasses etc. against the nontyped variables in JavaScript. First I thought, the problem was that the target file does not exist and then the Path()-call can't return a variable type file. But even if you create an empty target file with that name, the script fails (but with another error message...)

The only way I found out was to use the JXA-ObjC-Bridge as descriptor in the JXA release notes:

ObjC.import('Cocoa')
error = $()
fMa = $.NSFileManager.defaultManager
fileMoved = fMa.moveItemAtPathToPathError('/Users/user/Source/file.pdf','/Users/user/Destination/file.pdf', error)
if (!fileMoved) {
    $.NSBeep();
    // or do something else depending on error.code
}

I think it's a more elegant way than using shell scripting, but that is only a feeling ;-)

Cheers, Michael / Hamburg

ShooTerKo
  • 2,242
  • 1
  • 13
  • 18
  • Yep, it is a shame. Actually, I got another code to work at least somehow: `Application("Finder").duplicate(Path("/Users/user/Source/file.pdf"))` will create a file named `file copy.pdf`. Another one `Application("Finder").duplicate(Path("/Users/user/Source/file.pdf"), {to: Path("/Users/user/Destination/file.pdf")})` will fail with `Error -1728: Can't get object`. Looks like an issue with the destination path. –  Mar 28 '15 at 23:26
  • It is: the destination path needs to be a path to a folder, not to a file (`mv` semantics do not apply here). See [my answer](http://stackoverflow.com/a/41086329/990363). – kopischke Dec 11 '16 at 12:53
-1

This script works using Finder objects with the move command:

var Finder = Application("Finder")
var homeDirectory = Finder.startupDisk.folders["Users"].folders["user"]

var sourceFile = homeDirectory.folders["Source"].files["file.pdf"]
var destinationFolder = homeDirectory.folders["Destination"]

Finder.move(sourceFile, { to: destinationFolder })

It also works for the duplicate command.

syntaxera
  • 254
  • 3
  • 5
  • Though Finder uses a different path representation model, it can coerce AppleScript aliases and their JXA equivalents, Path objects to that just fine. The issue is OP is providing a path to a file where they should be providing a path to a folder. See [my answer](http://stackoverflow.com/a/41086329/990363). – kopischke Dec 11 '16 at 12:53