Move All Folders in Source Folder to Different Folder [Example]


Use Case

  • Normally I like to use native, non-scripting, KM Actions when I can, as long as they are effective and reasonably efficient.
  • The main challenge in this case was identifying the subfolder which ended in a specific suffix. The AppleScript for this is easy:
    • set subFolderList to folders of mainFolderAlias whose name ends with subFolderSuffix
  • In this case, I could not find the equivalent KM Actions to perform the same function as the below AppleScript.
  • If anyone knows of a non-scripting solution, please feel free to post below.


MACRO:   Move All Folders in Source Folder to Different Folder [Example]

~~~ VER: 1.0    2019-06-04 ~~~


Move All Folders in Source Folder to Different Folder [Example].kmmacros (19 KB)
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.

** Macro built in response to this request:**
How Do I Move Items in SubFolder to Main Folder?




  • Move All Items in SubFolder with Suffix to Main Folder
    • SubFolder identified by a given sufix
    • Must have only ONE subfolder with this suffix
    • Items with the same name must NOT exist in Main Folder


  1. First, make sure you have followed instructions in the Macro Setup below.
  2. Trigger this macro.


  • Carefully review the Release Notes and the Macro Actions
    • Make sure you understand what the Macro will do.
    • You are responsible for running the Macro, not me. ??
  1. Assign a Trigger to this maro.
  2. Move this macro to a Macro Group that is only Active when you need this Macro.
  3. ENABLE this Macro.
    (all shown in the magenta color)
    • Set Variable "Local__MainFolder"
      • POSIX path to folder
    • Set Variable "Local__SubFolderSuffix"
      • Must have only ONE subfolder with this suffix


  1. KM 8.2+
  2. macOS 10.11.6 (El Capitan)

TAGS: @Finder @Files @Folder @AppleScript


  • Any Action in magenta color is designed to be changed by end-user


  • To facilitate the reading, customizing, and maintenance of this macro,
    key Actions are colored as follows:
  • GREEN -- Key Comments designed to highlight main sections of macro
  • MAGENTA -- Actions designed to be customized by user
  • YELLOW -- Primary Actions (usually the main purpose of the macro)
  • ORANGE -- Actions that permanently destroy Variables or Clipboards,
    OR IF/THEN and PAUSE Actions


  • While I have given this a modest amout of testing, and to the best of my knowledge will do no harm, I cannot guarantee it.
  • If you have any doubts or questions:
    • Ask first
    • Turn on the KM Debugger from the KM Status Menu, and step through the macro, making sure you understand what it is doing with each Action.


AppleScript To Move Items in SubFolder

property ptyScriptName : "Move All Items from SubFolder to Main Folder"
property ptyScriptVer : "1.0"
property ptyScriptDate : "2019-06-04"
property ptyScriptAuthor : "JMichaelTX"

  • Move All Items in SubFolder with Suffix to Main Folder
     • SubFolder identified by a given sufix
     • Must have only ONE subfolder with this suffix
     • Items with the same name must NOT exist in Main Folder

  • "OK" on the first line
  •  TBD on subsequent lines
  • OR, "[ERROR]" & error details
KM VARIABLES REQUIRED: (must be set before calling this script)
  • Local__MainFolder
  • Local__SubFolderSuffix
  1.  macOS El Capitan 10.11.6+
      (may work on Yosemite 10.10.5, but no guarantees)
  2.  Mac Applications
        • Keyboard Maestro 8.2+

REF:  The following were used in some way in the writing of this script.
use AppleScript version "2.5" -- El Capitan (10.11) or later
use scripting additions

## Some Scripts may work with Yosemite, but no guarantees ##
#  This script has been tested ONLY in macOS 10.14.5 (Mojave)

property ptyCurrentApp : path to frontmost application as text -- use for dialogs
property LF : linefeed
property CR : return

--  Will be either actual results, or Error message
set scriptResults to "TBD"

  ### Requires Keyboard Maestro 8.0.3+ ###
  set kmInst to system attribute "KMINSTANCE"
  tell application "Keyboard Maestro Engine"
    set mainFolder to getvariable "Local__MainFolder" instance kmInst
    set subFolderSuffix to getvariable "Local__SubFolderSuffix" instance kmInst
  end tell
  if ((mainFolder = "") or (subFolderSuffix = "")) then
    error "The following KM Variables must bet set before calling this script:" & ¬
      LF & "  • " & "Local__MainFolder" & ¬
      LF & "  • " & "Local__SubFolderSuffix"
  end if
  set mainFolderAlias to POSIX file mainFolder as alias
  tell application "Finder"
    set subFolderList to folders of mainFolderAlias whose name ends with subFolderSuffix
    if ((count of subFolderList) = 1) then
      set oSubFolder to item 1 of subFolderList
      move items of oSubFolder to mainFolderAlias
      error "You MUST have one, and only one, subFolder that ends with: " & subFolderSuffix
    end if
  end tell
  set scriptResults to "OK"
  --~~~~~~~~~~~~~ END TRY ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
on error errMsg number errNum
  if errNum = -128 then ## User Canceled
    set errMsg to "[USER_CANCELED]"
  end if
  set scriptResults to "[ERROR]" & return & errMsg & return & return ¬
    & "SCRIPT: " & ptyScriptName & "   Ver: " & ptyScriptVer & return ¬
    & "Error Number: " & errNum
end try
--~~~~~~~~~~~~~~~~END ON ERROR ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

return scriptResults

How Do I Move Items in SubFolder to Main Folder?

Hi! I think this is close to what I need, but I'm sorry - I'm struggling to work out what to change and how. I want to hit a key combination and move anything on the desktop to a specific folder (the same one every time). I changed the first purple box to the desktop, but I can't work out how to change the second one. I'm sure this is super basic - I'm sorry!



Hey Charlotte,

Don't apologize. @JMichaelTX is doing something a bit more complicated than the thread title suggests.

Here's a macro that allows you to use the direct path to the source-folder and the destination-folder.

You can use either a tilde-based path, as I have done – or a full POSIX Path.

The orange actions are the user-settings.


Move Contents of Source Folder to Destination Folder with AppleScript v1.00.kmmacros (8.3 KB)


Hey JM (@JMichaelTX),

This is one of those places where I dislike using Keyboard Maestro native actions, because it's not intuitive and no fun at all to create the necessary steps.

Find a Folder in a Directory By Its Suffix v1.00.kmmacros (7.8 KB)

I don't particularly like using vanilla AppleScript for this either, because in a folder with many files/folders whose clauses can be pretty slow to glacial .

If I know in advance the source folder will have less than 100 or so items I may resort to a whose clause, but it's still relatively slow on my older hardware with macOS 10.12.6 (Sierra).

This AppleScript takes 22 seconds to run on a folder with 100 items in it (on my system).

tell application "Finder"
   set sourceFolder to target of front window as alias
   set itemList to (items of sourceFolder whose name ends with "sufx") as alias list
end tell

By contrast this shell script is very quick and efficient:


find "$sourceDir" -depth 1 -type d -iname "$folderSuffixStr"

We can also do VERY quick with AppleScriptObjC:

# Auth: Christopher Stone
# dCre: 2016/05/08 07:01
# dMod: 2019/06/16 17:25
# Appl: AppleScriptObjC
# Task: Non-Recursively Find Files Using a Regular Expression ; RegEx ; Handler ; Insertion-Location.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @ASObjC, @Finder, @Find, @Files, @RegEx
use framework "Foundation"
use scripting additions

# Front Finder Window (or Desktop if no windows are open).
tell application "Finder" to set sourceFolder to insertion location as alias

set sourceFolder to POSIX path of sourceFolder
set folderSuffix to "sufx"

set foundItemList to its findFilesWithRegEx:("(?i)^.+" & folderSuffix & "$") inDir:sourceFolder

on findFilesWithRegEx:findPattern inDir:sourceFolder
   set fileManager to current application's NSFileManager's defaultManager()
   set sourceURL to current application's |NSURL|'s fileURLWithPath:sourceFolder
   set theURLs to fileManager's contentsOfDirectoryAtURL:sourceURL includingPropertiesForKeys:{} options:(current application's NSDirectoryEnumerationSkipsHiddenFiles) |error|:(missing value)
   set theURLs to theURLs's allObjects()
   set foundItemList to current application's NSPredicate's predicateWithFormat_("lastPathComponent matches %@", findPattern)
   set foundItemList to theURLs's filteredArrayUsingPredicate:foundItemList
   set foundItemList to (foundItemList's valueForKey:"path") as list
end findFilesWithRegEx:inDir:



It WORKED!! (I know, you knew that was going to happen, but I wasn't sure I could implement what you gave me). A thousand thank you's :slight_smile: