Move Selected Finder Item(s) to folder?

Okay I’ve seen a lot of discussion in this general area but was not able to find an answer to my question.

I want to create a macro that takes the item or items currently selected in the finder and move them to a SPECIFIED folder.

It’s been frustrating because I’ve found screenshots here that seem to show a macro that does this task, but invariable in those cases the poster has not uploaded the actual macro. And also invariably I find I can’t recreate the macro from the screenshot.

Anyway, I’m sure this is an easy problem for a seasoned Keyboard Maestro wizard or Applescripter and I’d be very grateful for some help!

As I just replied in your other post, see:

Hey Aaron,

You searched the forum for move item finder ?

Here’s one thread worth looking at.

If you’ll tell me what folder you want to specify I’ll change the AppleScript in post #3 to suit.

-Chris

If you have seen posts, or screenshots and there is no macro just ask the poster for the macro. I’d be happy to send you the separate move actions that are all assigned to one keyboard shortcut (That I responded to you about in the other thread), or to even make specific ones you’d like.

In fact I will go out on a limb here and say that if you indicate what the name of the folder is and the path to that folder on your system, someone will likely whip it up and post it for you before you can blink an eye.

Personally, I prefer that people build their macros from screenshots because that means you learn how to build the macro, which is more important than the macro itself (with exceptions for really complex macros).

If you see an image of a macro and you cannot build it yourself, ask how to build it.

4 Likes

Interesting thought, and probably a good idea.

Also, for actions that have a significant amount of text, we might include that separately from the screenshot(s), so they can copy and paste.

Absolutely. I am talking about general questions with simple answers.

And I am not in any way suggesting people should refrain from posting the kmactions/kmmacros file as well (especially since Share to Forum makes it so easy to do both). It’s more that I would strongly encourage readers to create simple three or four action macros themselves rather than import it, because that will benefit them more than just double clicking a file. It will also train people up on the small details that are important, regex/string searching for example, which would not necessarily be noticed if you don’t have to manually re-create the macro.

2 Likes

Hello! So it’s been awhile, but I figured I’d post in the same thread in case people are interested.

So, unfortunately these macros for moving selected files to specific folders didn’t function well. It seemed they usually worked, but items would often end up in the wrong folder. I couldn’t figure out the reason for sure, but it seems that if a file wasn’t finished being moved, it might follow the next file into the new folder. Again, I’m not sure if this is what happened, but things were definitely going to the wrong folders.

Is there perhaps an AppleScript that could be triggered and edited for different folders?

thanks!

Hey Aaron,

Sure, that's easy.

See this post to get a tool to create a relative-alias for your destination folder.

Putting relative-aliases on the Clipboard

Here's pretty much the simplest possible script:

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

# This is a relative-alias
set destinationFolder to alias ((path to downloads folder as text) & "YourFolderName")

tell application "Finder"
   set finderSelectionList to selection as alias list
   if length of finderSelectionList = 0 then error "No files were selected in the Finder!"
   move finderSelectionList to destinationFolder
end tell

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

Here's the same script with better error-checking:

------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2017/03/19 16:40
# dMod: 2017/03/19 16:43
# Appl: Finder
# Task: Move the selected items to a destination folder
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Finder @Move, @Selected, @Items, @Destination, @ccstone
------------------------------------------------------------------------------

try
   
   # This is a relative-alias
   set destinationFolder to alias ((path to downloads folder as text) & "YourFolderName")
   
   tell application "Finder"
      set finderSelectionList to selection as alias list
      if length of finderSelectionList = 0 then error "No files were selected in the Finder!"
      move finderSelectionList to destinationFolder
   end tell
   
on error e number n
   set e to e & return & return & "Num: " & n
   if n ≠ -128 then
      try
         tell application (path to frontmost application as text) to set ddButton to button returned of ¬
            (display dialog e with title "ERROR!" buttons {"Copy Error Message", "Cancel", "OK"} ¬
               default button "OK" giving up after 30)
         if ddButton = "Copy Error Message" then set the clipboard to e
      end try
   end if
end try

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

-Chris

Could you please be specific about which macros are NOT working properly for you? Please post a link to the macro topic.
If there is a bug in the macro that affects you, then it will probably affect others as well.
So, we need to get the bugs fixed.

Thanks.

Here's a screenshot of one of the macros:

Thanks!

I’ll see if I can make that work in Keyboard Maestro.

I’m not entirely clean on what are the AppleScript commands and what are the placeholder names.

"YourFolderName" is a placeholder, everything else is part of the script.

Hey Aaron,

If you select an item in the Finder and run this script:

Putting relative-aliases on the Clipboard

It will assemble this part of the code for you and places it on the Clipboard:

alias ((path to downloads folder as text) & "YourFolderName")

You can run that script from the Script Editor.app to see how it works.

Essentially it takes a known location (path to...) and converts the resulting alias to a string.

That string is an HFS path.

From that anchor-point the script appends the rest of the path (from the anchor) to the file or folder.

The “alias” in front of that turns the full HFS path into an AppleScript alias.

Once you've selected a few items in the Finder and run the script to see how the paths differ, place it into an Execute an AppleScript action in a Finder-specific Keyboard Maestro macro – give it a keyboard shortcut P (or whatever is mnemonic for you) – and go-to-town.

--> is representative of a result in AppleScript.

set hfsPathStr to ((path to downloads folder as text) & "Test Folder:")

--> "Mercury:Users:myUserName:Downloads:Test Folder:"

set destFldrAlias to alias hfsPathStr

--> alias "Mercury:Users:myUserName:Downloads:Test Folder:"

Most Mac-users are terribly confused by path strings when first exposed to them. Don't let it bother you if you're one of them.

You have to use them a bit, before they start to feel comfortable. It probably took a year before using them became second nature to me – and that was back in the days of only HFS paths. When OSX came around so did POSIX Paths and a whole new set of issues, and again it took me a while to get comfortable with them.

-Chris

Hey Folks,

If you’re going to run an AppleScript in Keyboard Maestro TEST it FIRST in the Script Editor.app (or Script Debugger if you have it).

Open the Log History window in the Script Editor and see what the script does.

Don’t just run it from the black box of an Execute an AppleScript action in Keyboard Maestro.

If you’re unfamiliar with AppleScript this won’t mean a lot to you at first, but it will give you some feel for what is happening.

If you have a little understanding of what’s happening in the code, you can comment-out segments of code – so you can see what is happening at a certain point.

Script Debugger makes this sort of thing MUCH easier, because it has a real debugger and offers a live view of variables as they change.

But with a little understanding and effort you can discover quite a lot about AppleScript code using Apple’s Script Editor.app.

-Chris

1 Like

[quote=“ccstone, post:9, topic:4662”]

# This is a relative-alias
set destinationFolder to alias ((path to downloads folder as text) & "YourFolderName")

tell application "Finder"
   set finderSelectionList to selection as alias list
   if length of finderSelectionList = 0 then error "No files were selected in the Finder!"
   move finderSelectionList to destinationFolder
end tell
...
```[/quote]

Unfortunately I got the following error message:

File alias MacHD:Users:aaronseeman:Downloads:/Volumes/Main External 1.5 TB/Test AppleScript Move wasn’t found.

Num: -43

Hey Aaron,

That's because you tried to make a relative alias from a non-boot-drive item.

I don't test for that in my script, because I never make that mistake.  :wink:

Use the plain alias script instead of the relative-alias script for any non-boot-volume items.

PUTTING FILE ALIASES ON THE CLIPBOARD

-Chris

Every developer's famous last words. :wink:

Just kidding, Chris. Most of your stuff is flawless.
Unfortunately, us users are not. :wink:

1 Like

Hey JM,

No developer can foresee all the mischief users will get up to. :alien:

That script has been in the wild for many years, and this is the first time the external disk issue has been reported.

It's easy to test for the startup disk, so the next time I revise the script I'll add the check.

-Chris

2 Likes

This seems to take an item or items selected in the Finder and moves them to a specified folder.

I don't know why as I don't know what %path% means. Just poking around in the dark and found this. Hopefully I don't stumble across any sleeping bears or deep crevices...:open_mouth:

Why does this work? Under what circumstances would it not work? If this solves the request, why are more complicated solutions being offered?

Does the drive (boot or non-boot) the selected item is in and the drive being moved to matter as it seemed to for Duckmandu's circumstance?

2 Likes