Easiest Solution to Move One or More Files and/or Folders Selected in the Finder to Another Folder

I thought maybe this would work :

Easy enough. Just modify the macro to do as you want.

Hey Nikita,

You need to read that code more closely. It doesn't have any commands to move files.

This is about as simple as it gets:

--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2016/09/05 17:22
# dMod: 2016/09/05 17:25 
# Appl: Finder
# Task: Move selected items in the Finder to a specified destination.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Finder, @Move, @Selected, @Finder, @Items, @Specified, @Destination
--------------------------------------------------------

# Your destination folder – requires a POSIX Path:
set destinationPath to "~/test_directory/FOLDER-01/"

# Convert “destinationPath” to an alias.
tell application "System Events" to set destinationPath to POSIX path of disk item destinationPath
set destinationAlias to alias POSIX file destinationPath

# Move the items selected in the Finder.
tell application "Finder"
   set selectedItemList to selection as alias list
   move selectedItemList to destinationAlias
end tell

--------------------------------------------------------

It can be made slightly more simple by using HFS paths and aliases from the start, but these sometimes confuse people – so I used a POSIX Path in the first script.

--------------------------------------------------------
set destinationAlias to alias ((path to home folder as text) & "test_directory:FOLDER-01:")

# Move the items selected in the Finder.
tell application "Finder"
   set selectedItemList to selection as alias list
   move selectedItemList to destinationAlias
end tell
--------------------------------------------------------

-Chris

3 Likes

Wow,

Thank you, this was indeed very simple. This script is extremely handy. :heart:

2 Likes

That simple? Wow. Thanks Chris!

2 Likes

@ALYB, of course if you want to use that script in KM, then you must:

  1. Set the destinationPath in a KM Variable
  2. Modify the script to read that KM Variable
  3. Put the script in an Execute AppleScript Action
  4. And you probably should add some error checking.
  5. Add any notifications you may want to confirm move, or report errors

The core of this macro is just as simple, but it does much more:
MACRO: [FINDER] Move Selected Finder Items to Choose Folder [Example]

Here's the core Action:

If anyone wants to modify the process, you don't need to know any scripting. For example, if you want to use a fixed Destination Folder, then all you need do is disable the Prompt for User, and add a simple Set Variable to Text action.

The main advantage of using a script for this is if you need to move many files (>> 100), then the script may be faster.

Questions?

Untrue.

There is no need whatsoever to take those steps.

The destination path can be changed to whatever the user wants within the AppleScript itself.

-Chris

I think maybe you overreacted to the word "must". IMO to use a folder path set in the macro from any source other than hard-coded would require the path be passed to the script via a KM Variable.

IMO, it is not a good practice to hard code paths and similar data in a script that you plan to use in a KM Macro. Doing so might require some users who are not familiar with AppleScript to have to make a change to the script itself, which could lead to an unintended error.

AppleScript is a great resource and works well with Keyboard Maestro. However one of KM's great features is that it makes automation easy for those who don't know how, or don't want to write/change scripts. Changing a script written by by someone else can be tricky.

The great thing here is that we have several options for solution to this requirement of moving files.

Let's just throw in a tiny bit of JXA for completeness:

var app = Application.currentApplication()
var Finder = Application("Finder")

app.includeStandardAdditions = true

var selection = Finder.selection()

var fol = app.pathTo("home folder").toString() + "/Desktop/new folder"

selection.map(f=>f.move({ to: Path(fol) }))
2 Likes

@ccstone it seems like this should work to move the contents of an entire folder to another just using AppleScript in Keyboard Maestro but it doesn't.

# Your destination folder – requires a POSIX Path:
set destinationPath to "/Library/Application Support/Avid/Audio/Plug-ins"


# Convert “destinationPath” to an alias.
tell application "System Events" to set destinationPath to POSIX path of disk item destinationPath
set destinationAlias to alias POSIX file destinationPath

# Move the items selected in the Finder.
tell application "Finder"
  set selectedItemList to "/Library/Application Support/Avid/Audio/Mobile iLok"
  move selectedItemList to destinationAlias
end tell

@skillet, I think there are some errors in the script that you repurposed, but IAC I would just use this simple KM solution:

1 Like

I couldn't get anything to copy with this until I removed %Varible

As far as I can tell these are identical other than I changed "move" to "copy."

I'm sure this is obvious to you guys but I was able to make sense of what was happening with the error message that wasn't showing in the notification until I went to Notification Center and saw what was happening.

image

This is a typo in Peter's macro. It should be:
%Variable%Path%

I just tested this running Keyboard Maestro 9.0.3 on macOS 10.14.6 (Mojave) and it worked fine:

MACRO:   Copy All Files From One Folder to Another [Example]

**Requires: KM 8.2.4+   macOS 10.11 (El Capitan)+**
(Macro was written & tested using KM 9.0+ on macOS 10.14.5 (Mojave))

#### DOWNLOAD Macro File:
<a class="attachment" href="/uploads/default/original/3X/f/6/f6a948d99e3b559183f6d239c3edc9b6593ce459.kmmacros">Copy All Files From One Folder to Another [Example].kmmacros</a>
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**


---


<img src="/uploads/default/original/3X/4/6/464c989cae678b86fba245d1ae61e9715a0bbcda.png" width="596" height="533">
1 Like

Oh thanks Michael.

My apologies for asking another question but I just want to make sure I am clear on how things are working. What does the word %Variable% do different than if I just did %Path% or in your macro example %FilePath%. Isn't the percent signs at the start and end indicating that it is a variable?

Technically, this is called a Variable token, and "%Variable%" is part of the syntax. Sometimes you can get away without using "%Variable" at the beginning, but not always.

Gotcha, thank you very much for the help and explanation. It's amazing to see all the help you have given people all over this forum, very appreciated, I have learned a lot from you including all your work on the Wiki.

2 Likes

Hey Folks,

This macro adds the ability to create the destination folder on the fly – including intermediary folders – if necessary.

-Chris


Move Selected Items in the Finder to a Given Destination Folder v1.00.kmmacros (9.2 KB)
Keyboard Maestro Export

3 Likes

Thanks @ccstone this works wonderfully. It is now in production, thanks for sharing.

1 Like

Hey Folks,

Point to note:

As of Keyboard Maestro 8 the New Folder Action is capable of creating intermediate folders.

I.E. if any parent folders between the start and the end of the path don't exist they will be created along with the new folder.

Without “intermediate folders” switched ON the action will fail if any parent-level folders in the path to the desired new folder don't exist.

I still prefer doing this job with AppleScript, because it enables UNDO in the Finder and the KM action doesn't.

But – now I've discovered the action can create intermediates I'm much more likely to use it when undo isn't important to me.

-Chris


Make New Folder -- With Intermediate Folders If Needed v1.00.kmmacros (6.1 KB)
Keyboard Maestro Export

2 Likes

AI to the rescue I was able to figure out what needed to change when referencing a path retreaving a variable in Keyboard Maestro. The old code is commented out.

Execute an AppleScript Action (v10.2)

Execute an AppleScript.kmactions (1.2 KB)

Also for what it is worth if you use this code and move between drives it will keep the original where it is and copy. If you want the file to actually move you can use the following AppleScript.

set destinationAlias to alias ((path to home folder as text) & "test_directory:FOLDER-01:")

# Move the items selected in the Finder.
tell application "Finder"
  set selectedItemList to selection as alias list
  repeat with itemRef in selectedItemList
    duplicate itemRef to destinationAlias
    delete itemRef
  end repeat
end tell