Moving a file using AppleScript not working after changing the file name

Viewed 288

I'm using AppleScript to rename a file and move it to a folder. This is executed using a voice command. It doesn't move the file to the folder, it presses Enter, renames the file to "myFile", presses Enter again.

However, if I execute this a second time, or if the file is named "myFile", It will work. I think the code that moves the file doesn't know, or is not updated on the file name. I don't know how to fix this. AppleScript is not my thing.

tell application "System Events"
    key code 36
    keystroke "myFile"
    key code 36
end tell

tell application "Finder"
    move POSIX file "/Users/joe/Desktop/myFile.csv" to POSIX file "/Users/joe/Desktop/TestFolder" with replacing
end tell
3 Answers

With the file selected, try this:

tell application "Finder"

    set itemlist to the selection
    set theFile to (item 1 of itemlist) as alias
    set name of theFile to "myFile.csv"

    move POSIX file "/Users/joe/Desktop/myFile.csv" to POSIX file "/Users/joe/Desktop/TestFolder" with replacing

end tell

If the file you want to move is currently selected in Finder and you would like to be able to set the new name… this solution may work for you

property moveToFolder : (path to desktop as text) & "TestFolder"

set newName to text returned of (display dialog "Name Your File" default answer ¬
    "myFile.csv" buttons {"Cancel", "OK"} ¬
    default button 2 cancel button 1 with title "Name Your File")

tell application "Finder"
    set originalFile to item 1 of (get selection) as alias
    set theFile to (move originalFile to alias moveToFolder) as alias
    if (exists of alias (moveToFolder & ":" & newName)) then ¬
        delete alias (moveToFolder & ":" & newName)
    set name of theFile to newName
end tell

Don't GUI script actions that can be natively commanded through AppleScript.

To rename and move a file using System Events:

tell application id "sevs"
    set base_folder to folder "~/Desktop/"
    set f_old to "myOldFile.csv" -- file to move (original name)
    set f_new to "myFile.csv"    -- new file name
    set dir to "TestFolder"      -- destination folder

    tell the base_folder 
        set the name of the file named f_old to f_new
        move the file named f_new to the folder named dir
    end tell
end tell

To rename and move a file using Finder:

tell application id "MACS" --OR: "com.apple.Finder"
    set base_folder to folder (POSIX file "/Users/joe/Desktop/")
    set f_old to "myOldFile.csv" -- file to move (original name)
    set f_new to "myFile.csv"    -- new file name
    set dir to "TestFolder"      -- destination folder

    tell the base_folder 
        set the name of the file named f_old to f_new
        move the file named f_new to the folder named dir with replacing
    end tell
end tell

Essentially, having broken the paths down as I have, both scripts are the same except for the value assigned to the variable base_folder, for which Finder needs to be made explicitly aware it's dealing with a filepath in posix notation. Alternatively, in the specific case where the base folder is the desktop folder, both Finder and System Events should be able to understand this:

set base_folder to the path to the desktop folder
Related