Help with a macro to move file to folder based on keyword variable

Hi there,

I would like to create a macro that, upon executing in Preview, prompts the user to enter a keyword and then, based on that keyword, moves the current file to a predetermined folder.

I tried to adapt the following macro Watched Folder Tag & Sort - #4 by Aaron, but thanks to @Nige_S guidance, I realise that %TriggerValue% is the wrong approach.

With the cheeky help of Claude Opus, we put together the following macro, which (perhaps unsurprisingly) does not work, haha.

Move Document to Keyword Folder.kmmacros (6.0 KB)

When I run the macro it prompts me for a keyword but then after taking the keyword nothing happens. I think it is because it is not identifying the current file I am working on correctly. I'm not sure how to point KM to the current open file? Any help or suggestions would be much appreciated.

Simple. You can't pass variable to shells using %Variable% syntax. You have to use $KMVAR_VarName syntax. And there may be a problem with the quotes. Let me think about that part.

https://wiki.keyboardmaestro.com/action/Execute_a_Shell_Script

The quotes seem fine, but there's another problem. You are assigning something to FolderPath, then you are overwriting FolderPath with a loop. Your loop isn't using the value of FolderPath, it's assigning all the files from your Finder Selection INTO that variable.

And then I think there's another problem. You are trying to move a file named "Amber". I don't think that's what you intend to do. I think you should be moving %Variable%FolderPath% instead.

There might be additional problems, I can't tell just by looking. If you fix these three issues, the problems might all be solved. Or we might have to fix more things.

I'm not sure I would solve this problem this way. Let me think of what I might do to solve it.

There's nothing wrong with your general approach. You can do whatever you want. I would probably assign one Stream Deck button to move all the selected files to Folder1, then have a different Stream Deck button move selected files to Folder2. To me that's more efficient.

Allow me to combine my above approach with a slightly different way of coding this. Here's what I would do:

This approach looks slick and neat. BUT I DIDN'T TEST IT. So there's a 33% chance it has a bug. It's mainly meant to show you that there are other approaches.

Thanks @Airy for the prompt reply. This looks good; however, I was envisaging more keywords than I have buttons on my Stream Deck (I was starting small while I work out the kinks). Is there any way to merge our approaches?

Let me think about that now. You didn't mention that. Also, you didn't mention how many buttons your Stream Deck has. I think there are models from 6 to 32. I use Stream Deck Mobile which has up to 64 buttons on the iPad at all times.

I think I have a way. Instead of using Prompt for User Input, I think you should use Prompt with List. This action displays a long list of text items, which could be your folder names. Sadly it's limited to 14 being displayed at once, but that shouldn't be a problem. I presume your folder names have a path and a "common" name that is unique. In that case, I would create a Prompt With List action that looks like this:

image

Notice how each line in this action contains a common name at the front, followed by a comma then a path name. I think this is the key to success, because when this action opens to the user, the user can either scroll to the appropriate name, or click the first letter of the name. Then the one that was selected is passed back to a variable (in the screenshot above, it's passed to the System Clipboard, which can be changed to a variable. The subsequent action can strip out the part before the comma leaving you with only the path, which it can store in the same variable I used above, LocalDestination.

Let me patch up my existing macro to show you what it would look like, in about 5 minutes. Of course, you can ignore anything I suggest, it won't bother me.

Okay here's my modified idea. Notice how clean this app is: it's only four actions! I did a quick test, and it worked in my test. You should probably do a few tests yourself.

Move Selected Files to a Folder Chosen from a List Macro (v11.0.3)

Move Selected Files to a Folder Chosen from a List.kmmacros (3.2 KB)

Of course, you have to use real paths.

1 Like

Thanks @Airy. I think I'm having some trouble with using the real paths. I got the following error:
image

This is how I have adapted your macro...
Move Selected Files to a Folder Chosen from a List.kmmacros (3.2 KB)

Is it because I have spaces in some of my pathnames?

Let me look. I've never had spaces in my folders.

No, I created a folder with a space and it worked fine. Therefore my only guess is that you made an error when you typed your path. Since I can't see your paths from here (i.e., the actual paths on your hard drive), all I can suggest is that you triple check them.

Oh, I see the problem. You didn't use the same paths from your original macro. In your original macro you said:

~/Library/CloudStorage/OneDrive-USQ/Personal/Home 93 Whitmore/Amber Electricity

But in your new macro you said:

~/Home 93 Whitmore/Amber

You have to use the correct paths! My paths were just examples, they weren't meant to be copied.

1 Like

This isn't what you wanted, but in case it gives you any useful ideas, here is what I did to have a way to move files/folders to a few set destinations.

  1. Create a macro for the first destination.
  2. Copy that macro for the other destinations, changing the path as appropriate but keeping the same hotkey trigger for each.
  3. Prefix the macro name with a convenient code, e.g. MN for "Move to New ƒ", MO for "Move to Old ƒ".

The result is a conflict palette giving a choice of destination. Total keystrokes: 3.

The hotkey that I use is my general-purpose one, which I use within macro groups to provoke a conflict palette.

1 Like

Before going any further...

There seems to be a general assumption that the file being looked at in Preview is still selected in the Finder -- I don't think that's necessarily true, and a different approach is needed.

1 Like

Ignoring all the fine work done above, I took a fresh look at this, starting from scratch. If I understand the request right, I think this is a relatively straightforward macro (either two or three actions, depending—see below). The attached macro, set to run only in Preview when you press Control-M, first displays a pop-up list of keywords:

When you select a keyword, the frontmost Preview document is moved to the destination associated with the chosen keyword. I think this is what you were looking for?

Here's the macro, and some important notes follow:

Download Macro(s): Move document to chosen folder.kmmacros (31 KB)

Macro screenshot

Macro notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.
System information
  • macOS 14.5
  • Keyboard Maestro v11.0.3

And the important notes:

  1. There are two full versions of the macro included, with one version disabled.

    Version one, the one that's enabled, works when all the destination folders share the same root path, i.e. your user's Documents folder, or some particular folder on a mounted volume. In this version of the macro, there's a variable (the green box in the macro) where you enter the path to the folder that contains all the destination folders. Do note include a trailing slash.

    That's the only difference (other than which variables are involved) between the two macros.

    Version two works when every keyword could point to a completely different location on your drives. To use version two, disable (or delete) the "Version 1" group, and enable the "Version 2" group.

  2. For this to work, none of your paths or folder names can contain items named with two consecutive underscores. That's because the macro uses a feature of the Prompt With List action to make your objective pretty easy to accomplish, and that feature relies on double underscores. If you have such paths or folder names, the macro can be adjusted, but as it's more complicated, I didn't include it in this version.

  3. You need to edit the Prompt With List action in whatever version of the macro you're using. The structure of the list is like this:

    Version 1: folder name__keyword
    Version 2: full path to folder__keyword

    In version 1, because the base path is stored in a variable, all you need to enter in the Prompt With List box is the name of each destination folder, followed by two underscores, then the keyword you want displayed.

    In Version 2, you enter the full path to each folder, then two underscores, followed by the keyword.

    To add more keywords in the future, just add more entries to the relevant Prompt With List action.

How does this work?

The Prompt With List action has a feature where, when it sees double underscores, whatever is to the left of those underscores is returned as the value of the selection the user makes, while whatever is to the right is displayed as the selection for the user to make.

So the macro displays your keywords, but returns either the name of the target folder (version 1) or the full path to the target folder (version 2) when a keyword is selected. Then a Move or Rename action is used to move the frontmost Preview document (thanks to the %FrontDocumentPath% token) to the destination associated with the selected keyword.

Please let me know if you have any questions;

-rob.

2 Likes

Thank you so much folks for your help and ideas - it is very much appreciated! As always, I learnt a lot through the process. Thanks again!