MacOS – Applescript Move current file to Folder

applescriptfilefinderfoldersmacos

I want to create a script that takes the current file selection and moves it to an already existing folder. I will then duplicate this code for 9 other folders and assign them all keyboard shortcuts.

My application is rather specific and unfortunately I need to use Finder to quickly monitor audio samples rather the Terminal. (I am wading thru thousands of audio samples, deleting and sorting them into sub folders).

The idea is very simple:

tell application "Finder"
    move selection to alias "Users:Jordan:Desktop:0"
end tell

This code works; however, it then proceeds to move the parent folder of the target file into the destination folder. This action makes the script counterproductive and useless. I tried the same idea in automator to no avail either. I have made long scripts to try and stop the tell from moving the folder too, but nothing has worked.

It's beyond me why such a simple function can be so buggy and difficult and why no matter how many variables I employee to make the selection static, or how many conditionals or delays I add, the script still moves the file in folder A to folder B THEN the folder A into folder B.

How do I move file in folder A into folder B without moving folder A into it as well?

Best Answer

This might be a bit of a sledgehammer approach by Applescript compared to Automator's 'move' command, but as that's how the OP approaches it...

To do it as an Automator Service, so it's easy to hotkey it…

'Service receives selected 'files or folders' in 'Finder''.

on run {input, parameters}   

    tell application "Finder"
        set selected_items to selection
        set theFolder to ((((path to desktop folder) as text) & "test") as alias)
        repeat with x in selected_items
            move x to theFolder
        end repeat      
    end tell    

    return input
end run