Replacing file with same name when moving

Hello everyone, I absolutely love KM, but I don’t know much about coding. As of right now, I have an action that moves files from Finder & Path Finder to a specific folder. However, if there’s a file in that folder with the same name, it doesn’t ask to replace or merge. Is there any way I can add the ability to automatically overwrite the file or merge? Sorry, probably day 1 basics, but I have to plead ignorance!

Thank you!

Hey Aaron,

Yes, it can be done via AppleScript.

What does your macro look like?

If you don't know how to post a macro or images to the forum see this page.

-Chris

If you want to replace the file that already exists, you can just do something like:

  • Trash File: Destination
  • Move File Source to Destination

Turn off any notification or aborting behaviour of the Trash File action.

I don’t know what you specifically mean by “merge”.

This is my macro as of now. If I could have it replace the file in that folder that'd be great. When I say "merge" I mean if there's another file in folder with the same name, it keeps both but gives the new or the old file a unique suffix or something like that. Deleting the file that's in the folder with the same name is probably the easiest method. Thanks for your help!

Keyboard Maestro “Move To Server Trash” Macro

Hi Chris!

Thanks for the response. I posted the macro to the conversation. I appreciate the help!

Hey Aaron,

Okay, I see the scope of the problem. I'll look at tomorrow after I've had coffee.

-Chris

Hi Chris, no rush but were you able to take a look at the script to see how I could replace files with the same name? Thanks!

Hey Aaron,

Sorry about that – I didn’t set a good enough reminder.

This AppleScript when run from an Execute an AppleScript action will move the selected items in EITHER the Finder or Path Finder to the designated location.

IF there is a COLLISION the destination item(s) will be REPLACED.

I know you prefer to rename collisions, but that is much more complicated.

When I do collision-control I usually employ a date-based serial number for the sake of simplicity.

I can add a routine to do that if you like.

-Chris

------------------------------------------------------------------------------
# Auth: Christopher Stone <scriptmeister@thestoneforge.com>
# dCre: 2017/02/15 18:30
# dMod: 2017/02/15 19:16
# Appl: Finder, Path Finder, System Events
# Task: Move selected items in the Finder OR Path Finder to a designated folder with REPLACING.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Finder, @Path_Finder, @System_Events, @Move, @Selection, @Designated
------------------------------------------------------------------------------
--» User Settings
------------------------------------------------------------------------------
# POSIX Path – preferably tilde-based POSIX Path if possible.
------------------------------------------------------------------------------
set destinationFolderPath to "~/test_directory/Destination_Directory/"

------------------------------------------------------------------------------
--» Main
------------------------------------------------------------------------------

set destinationFolderAlias to makeAlias(destinationFolderPath)
set appName to getFrontmostAppName()

if appName = "Finder" then
   set itemList to getFinderSelectionAsAliasList()
else if appName = "Path Finder" then
   set itemList to getPathFinderSelectionAsAliasList()
end if

if itemList ≠ {} then
   tell application "Finder"
      move itemList to destinationFolderAlias with replacing
   end tell
end if

------------------------------------------------------------------------------
--» HANDLERS
------------------------------------------------------------------------------
on getFinderSelectionAsAliasList()
   tell application "Finder"
      set finderSelectionList to selection as alias list
      if length of finderSelectionList = 0 then error "No files were selected in the Finder!"
      return finderSelectionList
   end tell
end getFinderSelectionAsAliasList
------------------------------------------------------------------------------
on getFrontmostAppName()
   set frontAppAlias to path to frontmost application
   set {oldTIDS, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {":", ".app:"}}
   set frontAppName to item -2 of (text items of (frontAppAlias as text))
   set AppleScript's text item delimiters to oldTIDS
   return frontAppName
end getFrontmostAppName
------------------------------------------------------------------------------
on getPathFinderSelectionAsAliasList()
   tell application "Path Finder"
      set pfSelectionList to selection
      if pfSelectionList ≠ missing value then
         repeat with i in pfSelectionList
            set (contents of i) to POSIX path of (contents of i)
         end repeat
      else
         error "Nothing is selected in Path Finder!"
      end if
   end tell
   repeat with i in pfSelectionList
      set (contents of i) to alias POSIX file (contents of i)
   end repeat
   return pfSelectionList
end getPathFinderSelectionAsAliasList
------------------------------------------------------------------------------
on makeAlias(posixPathStr)
   tell application "System Events"
      set posixPathStr to POSIX path of disk item posixPathStr
   end tell
   return alias POSIX file posixPathStr
end makeAlias
------------------------------------------------------------------------------

This is great! Is it possible to add the date / time to the end of the file or folder if there’s a file or folder named the same thing in the folder I’m moving it to? If not no big deal at all. Sometimes I just don’t want to replace something necessarily but I need both to be in that folder. Does that make sense? Either way THANK YOU for your help!

Hey Aaron,

Yes. It’s a trifle complicated to do, because I'd rather use the File Manager than the Finder (for speed).

But I'll fool with it some this evening.

-Chris