Gravatar

Luke Campbell

« luke.s.campbell@gmail.com »

AppleScript and Copying

21 January 2012

Oh my lord! I cannot begin to tell you how much of a pain I have experienced trying to do the most simple task one can do! What I wanted to do was simply to copy a file to a destination while having a window display the status of the transfer (using Finder's copying window). Seeing as how I've had some experience working on a Mac, the obvious choice is Applescript.

I have a local NFS server that has various files I work with and I wanted to be able to see a window of the file copying to the local file system, on Linux the solution was quite simple however on here the solution was a little obfuscated in Applescript. After several hours of tinkering and tweaking I came up with this:

gcopy

    #!/bin/bash

    osascript<<EOF
    tell application "Finder"
      set sfile to (POSIX file "$PWD/$1")
      set sdest to (POSIX file "$2")
      duplicate file sfile to folder sdest with replacing
    end tell
    EOF

Obviously there's room for a LOT of improvement here. I think that with some assistance from my friend Ruby I might be able to make something really nice, unfortunately Applescript doesn't like me one bit!

The things to watch out for this difference between Posix paths and HFS Paths (which Applescript) understands. Here is a nice tutorial on dealing with folders in Applescript, as well as the POSIX conversion technique I adopted from here.