Help with moving and zipping a file created by marco

Hi all,

Total novice on Keyboard Maestro, but even the few basic macros I’ve created so far have made me love it!

I work mostly in Logic Pro X and have a specific task that I’d love to make into a macro, but I don’t even know where to begin….

Would really appreciate any help!

  1. I have a macro that creates a file called (for example) ‘MIDI’ in a folder called (for example) ‘Project’
  2. I want to select the file I have just created and move it to a folder named ‘Bounces’ which is also contained in the folder called ‘Project’
  3. I then want to select the ‘Bounces’ folder and zip it, then rename the resulting archive.

Thanks a lot

Evan

2. Move a File

This is very easy to do using the File Actions action (KM Wiki).
Assuming that you have the POSIX path from Step #1, you can just use that in the Move File action.

3. Zip Folder

I recommend using the Bash ditto command since it will produce the same results as the Finder "compress" command, thus retaining all of the resources and extended attributes of the files.

Here is an example macro, which you will need to adapt for your use.


MACRO:   Zip (Compress) Folder Using Ditto [Example]

~~~ VER: 1.0    2018-03-26 ~~~

DOWNLOAD:

Zip (Compress) Folder Using Ditto [Example].kmmacros (5.6 KB)
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.


image

Questions?

Thanks so much JMichael - I’ll have a play later today and come back with any questions!

Appreciate the help!

Hi!
Just tried adapting your macro a little bit but I'm getting ditto: can't get real path for source error.
Here is my setup:

  1. I'm watching a folder for adds an item
  2. Then set the file name
    • Set Variable “ZIP_ArchiveName” to Text
    • %TriggerValue%.zip
  3. And then use the ditto command in the Execute Shell Script action
    ditto -ck --keepParent "$KMVAR_TriggerValue" "$KMVAR_ZIP_ArchiveName"

It looks like it's ditto's error, but I'm not sure how to troubleshoot it. Any help would be appreciated!

=== EDIT ===

Well, actually I'm not sure. I just tried it with tar
tar -czf "$KMVAR_ZIP_ArchiveName" "$KMVAR_TriggerValue"
And it gives me
Cannot stat: No such file or directory error
So I guess I'm doing something wrong.

I don't see where you defined a KM Variable "TriggerValue".
If you intended for that to be the KM token %TriggerValue%, then you need to set a KM Variable, SourceFilePath, to %TriggerValue%.

Then your ditto command would be:
ditto -ck --keepParent "$KMVAR_SourceFilePath" "$KMVAR_ZIP_ArchiveName"

1 Like

Aaah! I thought the token %TriggerValue% can be used as a variable.
Now when I set as
* Set Variable “OriginalFile” to Text
* %TriggerValue%
everything works.

Thank you!

1 Like

Oh, btw another quick question if you don't mind?
If I want to delete the original file once it's been archived with:
* Delete File “%TriggerValue%”
* Notify on failure.
do I need to set some kind of delay, to make sure the file does not get deleted before it is archived?
Or KM would not proceed from the archive shell script action unit it is done (shell returns 0)?

I'm not a shell script expert, but I believe that KM would not move from the shell script Action until the script had completed.

This is probably OK, but I prefer to set a KM variable to the token at the top of the Macro, and then just use that Variable throughout the Macro. If you use a descriptive name like "SourceFilePath" then it is clear everywhere in the Macro what you are referring to.

1 Like

This is probably OK, but I prefer to set a KM variable to the token at the top of the Macro, and then just use that Variable throughout the Macro. If you use a descriptive name like "SourceFilePath" then it is clear everywhere in the Macro what you are referring to.

Absolutely agree. I'm pretty paranoid about naming my vars/functions/classes in code, so I've been wanting to read up on KM variables, and organize them somehow in some kind of hierarchy (groupName_macroName_varName or something) because right now my KM vars are a complete mess, lol.

It is good you are thinking about this.
Here are some best practices that I follow:

  1. Always use Local or Instance Variables unless absolutely necessary to use a Global Variable.
    These variables are:
    • Unique to each Macro, so there is never any conflict with other Macros
    • Auto-deleted when the Macro terminates, so there is no clutter in your Variable list.
    • A major downside is that they are NOT available in the Variables Panel, Value Inspector, or Debugger.
  2. Global Variables are only needed for:
    • Use of same Variable by multiple Macros
    • Need to reuse same Variable in multiple executions of same Macro
  3. Use this Naming Convention for Global Variables:
    • Use prefix of "DND_", meaning Do Not Delete
    • Then a "Macro ID" prefix to identify which Macro it is used in, followed by two underscores.
    • For example, if I had a Macro named "Move and Zip Files", I would name a Global Variable like this: "DND_MAZ__FilePath"
    • The two underscores are use so that if the variable is used in a Prompt, all that is displayed are the characters to the right of the underscores.

I have used this method for years now, and it has served me well, with rare exception.
Of course, you need to develop a system that works for you.

1 Like