Automate "Move File" macro creation

I’m exploring automating the creation of new move selected Finder files macros to easily make lots of move to locations macros.

I trigger these move macros from a palette displayed in Finder:
2019-10-27_08-27-04
Ideally, I’d navigate to a location and trigger the macro. The macro copies the file location path, switches to KM, selects a template or create from a stored internal template, edit the action’s perimeters with the file path on the clipboard and names the macro with the folder’s name from the file path.

I found this AS that gets the file path:

tell application "Finder"
set theWin to window 1
set thePath to (POSIX path of (target of theWin as alias ))
set the clipboard to thePath
end tell

An Activate an App action switches to KM.

Then it gets not easy…

@ccstone’s AS script will select a macro that perhaps could be duplicated and edited with the path on the clipboard. And then somehow go back to the title to rename.

@DanThomas’s “[KMFAM] Favorite Actions and Macros” can create new macro’s with preset actions so I see possible pieces but can’t assemble the puzzle.

Help?

------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2014/08/21 21:37
# dMod: 2017/11/17 04:46
# Appl: Keyboard Maestro
# Task: Edit 'Generic Test 1' Macro of macro group 'Test Group'.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Keyboard_Maestro_8, @KM8, @Editor, @Edit, @Macro
------------------------------------------------------------------------------
try
---------------------------------------------------------------------------
# User Settings:
---------------------------------------------------------------------------
set macroGroupName to "Test Group"
set macroName to "Generic-Test 01"
---------------------------------------------------------------------------
tell application "Keyboard Maestro"
------------------------------------------------------------------------
# Make sure the designated macro group and macro exist.
# Create one or both of them as necessary.
------------------------------------------------------------------------
if (exists of macro group macroGroupName) = false then
set newGroup to make new macro group with properties {name:macroGroupName}
select newGroup
end if
if (exists of macro macroName) = false then
tell macro group macroGroupName
set newMacro to make new macro with properties {name:macroName}
select newMacro
return --» script ends here IF a new macro is created.
end tell
end if
------------------------------------------------------------------------
# Select the macro group if necessary.
------------------------------------------------------------------------
if selected of macro group macroGroupName = false or length of ( get selected macro groups) > 1 then
select macro group macroGroupName
end if
------------------------------------------------------------------------
# Select the macro for editing.
------------------------------------------------------------------------
editMacro macroName
------------------------------------------------------------------------
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
------------------------------------------------------------------------------

I'm not following your train of logic to create a new macro for every location.
Can't this be easily handled by data?

If you want to have a standard set of folders to move from, and/or, move to, just put the folder list in a KM global variable (or even a text file), and use that as a selection list.

My logic is formed and limited by my understanding, by what I've distinguished so far. As far as I know the ONLY way to get a new move to location on a list on a palette is to use a separate one of these
image
For each location I want on the Palette. The list I've pictured above has 20 locations which required 20 individual actions like this with each action housed in its own named macro which name appears on the palette. How else could it possible be done??? This is all I got so far. My limited set of distinctions, what I've distinguished so give me nothing else. I literally do not see any other way. No way, not possible, does not exist. Nowhere in sight, not even remotely possible. Can't be done. Crazy talk. Clear?

YOU have a different set of distinctions. One of them includes something called a "KM Global Variable". I do not have that distinction tho hopefully I will by the end of this.

I case you're wondering, it the word "just" I'm reacting to so strongly. It feels VERY diminishing when used this way. Small word can have big impacts. Leaves me feeling like an idiot or stupid and I'm clear I'm not. When I look at your various post I see you use the word a lot. Now I know your intent is NOT to diminish and in fact you are being really generous by giving me ANY of your time and attention which you do not owe me or anyone. So please do not hear this as an attack but rather as feedback to how your words land or occur for me over here. I do appreciate very much your teaching contributions and hope to benefit from them here. :smiley: Onward!

Step one, check out the wiki...



5d.png)

Hmm, interesting but doesn't tell me how to "just put a folder list or text file into a global variable."
Now text file I sorta get. I imagine it could be a TextEdit doc or a Pages doc with a list of file pathways to each of the folder I want to move selected Finder items to. I'd guess I'd have to highlight an item on the list to get it into the "Global Variable" and would need to maybe have a macro to save the file paths to the list as creating the list that way would be messy and tedious and take a lot of time. AND as the list would change over time, perhaps a lot, the list would need to be edited to keep it from getting too long to easily scroll through.

On to the Forum...

Did a search for "how to use global variable". Lots of results!

This mentions Global Variables but doesn't tell me now to "just put a folder list or text file into a global variable."

Trigger Script with Global Variable

A mention but not helpful.

This seems kinda on target but doesn't sound a simple as what you seem to be pointing to.

This has the idea of calling a macro from another giving it a globally feeling and also mentions how variables are available for all macros making all variables global. BUT if all variables are global what's the difference between variables and global variables? Why not just call them all just variables? Why add confusion by sticking global in it UNLESS a global variable really is a different animal. I'm thinking that there IS such an animal and will keep hunting!

This is interesting in that it brings up the idea of scope. So maybe a variable becomes a global variable when it's scope is global BUT ALL variables are global in scope as this thread hammers in so there is no reason to use the global to define a variable as they ALL are BUT clearly there is something called a global variable and I'm not just chasing Unicorns! The hunt continues...

"global variable space" and local variable not being written to disk, hmmm... So the persistence of a variable has something to do with a variable being global. So there seems to be at least two dimensions to consider. One is if a variable can be accessed by more than the macro it first occurs in and two if the variable persists beyond its first use by being written to disk. Elusive and secretive creatures!

Searching the Forum for "What is a folder list?" surfaced

Which kinda is like what you're pointing to but doesn't shed much light.

This seems promising but I don't how to use in to build a list of file paths.

That looks like about it for help from the Forum. On to the WWW!

Well, Googling "how to use a global variable in keyboard maestro" brought up:

which is right on point but occurs to ME as tail chasing. There's no explanation of how to " "just put a folder list or text file into a global variable." Sigh, nothing in that black bag for me...

Well this looked promising but stopped in the middle of the process. It talked about creating a text file and somehow the variable would use that text file. Sounds A LOT like what you are point to! Still, I have no idea how to put it together AND get it to show up in a palette or would I no longer use a palette and somehow select an item in the text file THEN trigger a macro that copies the selected text and use that as the variable for where to move the selected Finder item? That doesn't seem very easy or as elegant as clicking items on a palette.

This talks about getting a value from a list so I suspect he's seen the Unicorn as well!

Ok, after five pages of Google results the results don't resemble the question so I'll stop there.

While the solution seems to occur for you with "just" like clarity and simplicity it ain't so clear or simple to me who doesn't have the benefit of the distinctions you've created. Perhaps you can elucidate further how to use a "global variable" in conjunction with a "Folder List" or text file (how would the text file look/be structured?). Perhaps an example or two would illuminate the matter so I could distinguish something that illuminates the matter for me. Gotta get some light in this dark cave on mine...

Thank you for your time and attention to this :smiley:

Hi @BernSh,

I'm afraid I can't respond in depth to your post (not that I'm the one it's directed at anyway) but I thought I could at least show you an example macro that uses a variable (local in this case, but a global one could be used as well) to offer a list of folders to use as file destinations:

Move Selected Files to Folder Chosen from List.kmmacros (2.4 KB)
image

See the "Set Variable" action at the top? When you run the macro, the "Prompt With List" action shows every folder path written in that list, similar to a palette:

31%20PM

When you choose a folder path from that prompt, the macro then uses that choice to move the currently selected files to that chosen folder. The advantage to a method like this is that it's much easier to edit the list of folders in a variable then it is to create separate macros for every folder. I know this stuff isn't always intuitive or obvious, but I promise you it can be learnt, so I hope you'll stick with it (and that this example helps shed a few rays of light on this particular method :slightly_smiling_face:)

1 Like

Hey, thanks, cool!

When I get back from work I'll dive into this. Looks like just the thing! Can the list shown from "Prompt With List" action be made to display in any other way? I'm wondering if it's a single column list only it'd get pretty long and have to scroll after awhile. I'll explore the "Prompt With List" action. Haven't looked at it before.

Thanks again!

No problem. You can change the prompt's width and whether or not it sorts alphabetically, but that's currently the only display options it offers. However, instead of scrolling up and down, you can also type to filter the list down to what you want, use the arrow keys to move up and down it, and either click or press the Enter key to make a choice. In fact, the prompt won't display anything past 20 or so results, so typing then becomes the only way to filter the list at that point. That's not a problem if the keyboard is your primary method of interacting with your Mac, but if you prefer to use the mouse that could admittedly be an issue.

I think the issue here is that you are not using the best tool for the job. KM Palettes were not designed in a way that supports this type of workflow. That's why I mentioned:

That is exactly the method that @gglick provided you above:

You can use either Global or Local variable depending on your use case. If you want the list of folders to be available in other Macros, then use a Global variable. Else a Local variable would be preferred so that your Global variable space is not polluted.

The Prompt With List action does not provide a much customization as some of us would like, but it works very well. At a macro level it does the same thing as a palette: Provide a popup window to select an item.

If you need features the Prompt with List does not have, you can use a great macro by: MACRO: Spotlight Search Prompt by @DanThomas.

Since this macro makes use of the KM Custom HTML Prompt action, you can customize it as much as you like, limited only by your imagination and HTML/JavaScript skills.

Good luck. I hope one of the options will work for you.

1 Like

Thank you @JMichaelTX.

I apologize if my frustration was unpleasant for you in anyway. I really appreciate your ongoing support and sharing of your knowledge. You are a generous and good man and a large part of what makes this forum the outstanding place it is.

This is good to know! It resets my sights and expectations. Very useful.

Dan's Spotlight-like Prompt seems like a good interface for filing if I made macros named by the destination folder and use his prompt to call up the macros. It'd save calling up overworked Palettes.

The largest hurdle might be setting up a semi-automated way to easily and quickly make a series of similar macros with only the destination parameter changing. Dan's MACRO: [KMFAM] Favorite Actions and Macros seems to have possibilities.

As always, lots to explore and learn :male_detective:

Fun! :partying_face:

I see at least two ways of easily doing this:

  1. Maintain separate KM Global Variables for source folders and destination folders.
    • Add entires to each as needed
  2. Maintain one KM Global Variable that has both the source folder path and the destination folder path on the same line, but separated with a TAB.
    • Then you can use a KM Search with RegEx to extract the source and destination paths into separate Local variables when you want to actually make the move.

@DanThomas' macro is great and provides the most flexibility. But I would start with using the KM Prompt with List because it is the simplest to use. After you get that working, then you could switch to Dan's macro, if you really need it.

No worries. :smile:
I completely understand frustration and generally ignore all such remarks.

My dear man, you are asking a baby to climb a ladder. Most unlikely tho nice breadcrumbs to stumble upon later. Keep those ladders coming! They'll come in handy someday. :wink:

If you would, please show me what it would look like to have the destination folder list (the source is always the desktop) in a text file and how the macro accesses and uses the text file? How would I indicate within the list of file paths which file path to use? Would I select it, trigger the macro, and the macro would copy it and then somehow uses the clipboard as the variable for "to location" parameter within the move action?

I CAN image managing a frequently changing list in a text document with some macros.

Thank you for your understanding and ignoring such noise is probably a good idea.

Nah, just asking you to take a few steps on a small step-stool. :wink:

But first, let's get the requirements clear.
Have you tried the macro provided by @gglick?

If not, please do try it.

And then let us know what, if anything, you don't like about it, or, better said, in what way does it NOT meet your needs?

Tried it. It works and not as well as a palette as follows.

Background~

Intent: Automate all filing to point of transparent.

Obviously the tools to realize this are decades away so this is exploring available options.

Currently: Manually setting rules within Hazel is best and Hazel is offline until updated for Catalina. Obvious solution is wait for update.

Explored KM palettes but as you pointed out they are not designed for this workflow.

Also explored watch folder action but it fails when there is a same name file and would need to be scripted to generate a unique name when encountering this condition. This is too far above my skill level to consider.

The prompt with list lacks the followings:

-the high number of actions required to generate and update the variable list.

-the list displays the complete pathway set as a variable which is hard to read as the list gets long

As you pointed out the HTML route would allow any list display but cost of waiting for Hazel update is far lower than investment in the HTML option and most of this needed filing is not income work urgent.

Ideally, a standard navigation window would allow destinations to be selected and auto added or one click added to the pick list negating the numerous actions needed to add to the folder list within the prompt with list macro. Also, the list could be sorted by date added or alphabetically and would have a button to quickly access an adjustable recent folder list or folders tagged with a Finder tag or a Finder smart list could be displayed as either the primary or alternative pick list.

I know this a is a lot and thought I’d put the whole thought on the table to support this discussion.

If you think Hazel is the best solution, then I'd just wait for the update. I would expect it to be very soon since the Hazel developers are clearly behind the eight ball.

Maybe I'm missing something.

If I understand you correctly, you want a bunch of macros that differ only in the destination folder that selected files get sent to? That seems rather simple to do in a single macro similar to the examples you've been given.

  • If you keep the list of destination folders in a text file and read that into a variable for use with Prompt With List, it's not a high number of actions to add a new line to the file

  • Prompt With List can display a different value to the user that what it actually uses behind the scenes, so e.g. you the selection list could display Deeply Nested Folder but the variable would get the value ~/Documents/Important Folder/Child Folder/Deeply Nested Folder to use to perform the actual move.

1 Like

Ok, how would a text file of destination pathways be made and formatted? Can it be a TextEdit doc?

How exactly does that get read into a variable? Does it need to be located somewhere and that location mapped into the macro so it can get it? How would a selection be made? How does the macro know which path is selected?

How does the prompt with list work with that variable?

An example would be helpful.

Thank you!

Likely going to go this way. Let me see what using a text file to hold the variable looks like before marking this as the solution. TY!!

Yes, it can, although using TextEdit as opposed to a plain text editor like BBEdit does complicate things a bit for the macro since it uses RTF for its default format. It is definitely possible, though. For formatting, each destination folder needs to be on its own line, and if you want the prompt to show something other than the folder path, like a custom name, you will need to follow @roosterboy's advice and append the name to the file path with two underscores, like this:

~/Documents/Important Folder/Child Folder/Deeply Nested Folder__Deeply Nested Folder

There's an action for that called, appropriately enough, Read a File.

Every file is located somewhere on your system, so that's not an issue. The macro does need to know what that location is, but that's what the above mentioned Read a File action is for.

There's a selection function built into the Read a File action to select a file or folder for the action in the same way you would open or save any file in any standard Mac app.

Once the file is read its contents are stored in either a clipboard or a variable, so from that point on the macro can reference the clipboard or variable rather than the file path. File paths can of course be stored in their own variables, but that isn't relevant to the Read a File action.

Prompt with List gives you several options for what to use as the list's source, with a variable being one of them.

Here you go. This macro is similar to the one I uploaded before, only it uses an RTF TextEdit doc for its source:

Move Selected Files to Folder Chosen from List (File Version).kmmacros (3.9 KB)

The TextEdit doc is formatted like this:


And the resulting prompt looks like this:

The reason the macro reads the list file to the clipboard first and then filters it with Remove Styles is because it's an RTF TextEdit doc. If it were a plain text file, those steps could be skipped completely and the macro could start with a Prompt With List action that reads from the file directly:

23%20AM

While I was at it, I went and made a companion macro that should make it easier to add new folders to this file:

Add New Destination Folder to Folder List.kmmacros (7.4 KB)

This macro uses the current Finder selection to add a new folder to the list file (and will alert you if a folder is not currently selected). If the Finder is not frontmost, it will present you with an Open File dialog to let you choose a folder that way. Either way, once a folder is selected, the macro will then prompt you for a custom name, presenting the folder's name by default:

It will then format the new folder entry with the folder's path, followed by two underscores and the custom name you entered, then append it to the list file. Again, there are more actions needed to append an RTF file than there are for a plain text one, but RTF can certainly work.

Hopefully this answers your questions, and the example macros prove both useful for your needs and helpful in understanding how this all works.

1 Like

Gabe...

WOW!

It’s Christmas, winning the lottery, and a dream come true all rolled into a macro discussion. Damn! If I ever run a company, you are going to run customer service!

I’m blown away by how exactly you responded to my request, getting and providing the exact level of explanation I need to take advantage of what you offered. It‘s a model for others IMHO.

I’m going to move on cause it’ll be strange if I don’t but I will remember this. Thank you :pray:.

I’m traveling and will be back later next week and will play with all you’ve provided and report back. Seeing the possibility of macros interacting with documents (I’ll go plain text TextEdit or use BBEdit plain text to keep it simply) and I am really excited to explore what I might do with the macro folder making OMG :exploding_head:!

I already experience being complete with just your communication but this the KM forum not the leadership being of service forum so I have to at least appear to stay focused on macro making and why not? Macros are amazing too :wink:.

1 Like

Noodlesoft has released Hazel 4.4, bringing compatibility with macOS 10.15 Catalina by handling certain new privacy protections and adding support for the new Music app. The file automation and cleanup utility now requires a minimum of 10.12 Sierra, introduces “tons” of workarounds for a variety of preference pane-related bugs in Catalina, resolves an issue in Catalina where numbers would get reformatted with a decimal (“1” would become “1.0”), corrects a problem with passing numeric attributes to AppleScripts as arguments, removes Growl support, and fixes issues with the Sort Into Subfolder action.