1

I have an excel workbook open, with old file names in column A and new file names in column B. I am attempting to use the applescript below to rename my files. It runs without errors, but doesn't rename the files. Am I missing something? Any help is much appreciated.

set theDirectory to "/Users/admin/Desktop/test/copied images"
tell application "Microsoft Excel"
    tell active sheet
        tell used range
            set rc to count of rows
        end tell
        set theList to get value of range ("A1:B" & rc) as list
        repeat with theItem in theList
            try
                do shell script "mv " & theDirectory & "/" & quoted form of item 1 of     theItem & space & theDirectory & "/" & quoted form of item 2 of theItem
            end try
        end repeat
    end tell
end tell
  • There is a space in the path of theDirectory. You should apply quoted form to theDirectory & "/" & item 1 of theItem. Likewise for the destination – Anthony Kong Nov 22 '17 at 7:38
0

Why your script seems to run without errors

Your script runs without errors because it swallows any error that may occur in the do shell script line.

Temporarily remove the surrounding try and end try lines to un-mute the errors. This will likely lead to an error message that ends with No such file or directory.

Why your script does not rename files

The issue is that your script hands the value of theDirectory directly to the shell command without quoting it. Because that value contains spaces, the shell interprets the directory &[…]theItem part as two separate arguments, which causes mv to fail.

Solution

In the do shell script line, replace both occurrences of

theDirectory

with:

quoted form of theDirectory

(Optional) Making your script more robust

Additionally, I suggest that you either lose the surrounding try/end try altogether, or alternatively insert an on error statement to handle possible errors that can occur in the do shell script part.

Writing scripts that don’t swallow errors means you’ll save time in the long run.

  • Thank you for the detailed explanation, this was really helpful. I now get an error - "the variable theDirectory is not defined" – Carlos Rivera Nov 22 '17 at 13:46
  • had a sytax error i fixed in the first line. I now get an error I can't decypher - microsoft excel got an error: mv [-f] -i| -n] [-v] source target mv [-f] -i| -n] [-v] source ... directory. – Carlos Rivera Nov 22 '17 at 13:55
  • What does it say when you temporarily log your shell command line (instead of executing it with do shell script)? – Synoli Nov 23 '17 at 8:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .