How to set a path and filename partly through the use of variables

I would be grateful to know for the best way to do the following.

Background information

  1. I name client invoices in a consistent way, year, job number and the word ‘invoice’. For example: 2019_001 - invoice.pdf, 2019_002 - invoice.pdf

  2. The invoices are all filed in an invoice folder under a folder for a job.

For example:

  1. /Users/victor/vwdata/notary/2019/2019_001 (Jones)/invoice/2019_001 - invoice.pdf

  2. /Users/victor/vwdata/notary/2019/2019_002 (Smith)/invoice/2019_002 - invoice.pdf

  3. /Users/victor/vwdata/notary/2019/2019_003 (May)/invoice/2019_003 - invoice.pdf

  4. /Users/victor/vwdata/notary/2019/2019_004 (Hammond)/invoice/2019_004 - invoice.pdf

These are all filed in a consistent way (through the use of Hazel rules and Keyboard Maestro macros for folder creation).

What I would like to do

Occasionally I need to print or attach an invoice. I would like to start by creating a prompt where I would only need to type in the number of the invoice and then the macro would then generate the path for the file (through the use of a variable?).

For example, if I wished to print invoice '2019_001 - invoice.pdf' then the prompt would just ask for “001” (to a variable) and the rest of the macro would do the rest to generate: /Users/victor/vwdata/notary/2019/2019_001 (Jones)/invoice/2019_001 - invoice.pdf

The part I am stuck on is how to take account of/handle where the name of the client appears, since this varies in length for each client.

I am not sure if this is possible to do with Keyboard Maestro (or what function etc) I need to use or whether I need AppleScript to do so (and what code is necessary to use.

Any help would be gratefully received.

Try the methods here:

Thank you for the replies, but neither work in quite the way I would like.

I would be grateful for any other suggestions or solutions. Even if the answer it is not possible.

Easy enough I think. Here's an example to get you started. ==You will need to revise and extend as needed to meet your specific needs.==

Based on your statement, the macro uses this naming convention for the file path, which you can change:
/Users/victor/vwdata/notary/%Year%/%Year%_%Seq% (%Client%)/invoice/%Year%_%Seq% - invoice.pdf


Example Output

Given This Data
image

Note: The first time the macro is run, these field will be blank, and you will have to pick from a popup list, or enter. Subsequently, the previous values will be shown.

This FilePath is Created
image

MACRO:   Generate File Path Based on Naming Convention and User Input [Example]


#### DOWNLOAD:
<a class="attachment" href="/uploads/default/original/3X/6/3/6313dff345d64ad112c611f679163ced1c0b461c.kmmacros">Generate File Path Based on Naming Convention and User Input [Example].kmmacros</a> (14 KB)
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**

---

### ReleaseNotes

Author.@JMichaelTX

**PURPOSE:**

* **Generate File Path Based on Naming Convention and User Input**

**NOTICE: This macro/script is just an _Example_**

* It has had very limited testing.
* You need to test further before using in a production environment.
* It does not have extensive error checking/handling.
* It may not be complete.  It is provided as an example to show you one approach to solving a problem.

**How To Use**

1. Trigger this macro.
   * It will then prompt you for the Client, Year, and Invoice Seq#
   * Then create the file path based on your file naming convention and this data

**MACRO SETUP**

* **Carefully review the Release Notes and the Macro Actions**
  * Make sure you understand what the Macro will do.  
  * You are responsible for running the Macro, not me.  ??
.
1. Assign a Trigger to this maro..
2. Move this macro to a Macro Group that is only Active when you need this Macro.
3. ENABLE this Macro.
.
* **REVIEW/CHANGE THE FOLLOWING MACRO ACTIONS:**
(all shown in the magenta color)
   * Set Variable “Local__ClientList”
   * Create FilePath Using Naming Convention and User Data
     * Modify file path naming convention as needed

TAGS: @Path @Files

USER SETTINGS:

* Any Action in _magenta color_ is designed to be changed by end-user

ACTION COLOR CODES

* To facilitate the reading, customizing, and maintenance of this macro,
      key Actions are colored as follows:
* GREEN   -- Key Comments designed to highlight main sections of macro
* MAGENTA -- Actions designed to be customized by user
* YELLOW  -- Primary Actions (usually the main purpose of the macro)
* ORANGE  -- Actions that permanently destroy Variables or Clipboards,
OR IF/THEN and PAUSE Actions


**USE AT YOUR OWN RISK**

* While I have given this limited testing, and to the best of my knowledge will do no harm, I cannot guarantee it.
* If you have any doubts or questions:
  * **Ask first**
  * Turn on the KM Debugger from the KM Status Menu, and step through the macro, making sure you understand what it is doing with each Action.

---

![image|601x1134](upload://b7v0RBiYSV9J5YPRoBkw4aRy5aH.png)

---

### Questions?
1 Like