Build a Text String Based on a Variable Value, But NOT Using the Variable Value Itself

Hello!

I have 18 or so clients, each has a 3-letter acronym ABC, XYZ, etc. Each client has a folder for tickets, but the path used does NOT use the acronym, it uses the full client name like:

/Users/[myUserName]/Documents/MyCompany/Clients/Clients-Frequent/[ClientName]/tickets/

So, a ticket for, say, Acme Corp gets a ClientAcronym variable of ACM, and the Save Dialog needs to be directed to:

/Users/[myUserName]/Documents/MyCompany/Clients/Clients-Frequent/Acme Corp/tickets/

My objective is to find a clever way to save to the correct client ticket folder using the ClientAcronym variable. I currently use the following method using TextExpander snippet expansion which works, but seems a bit sketchy with snippet expansion timing etc.

Each client has a text expansion snippet which is the 3-letter ClientAcronym plus the letter "t", so the snippet:

ACMt

expands to:

/Users/[myUserName]/Documents/MyCompany/Clients/Clients-Frequent/Acme Corp/tickets

When I write the ticket document, I populate the ClientAcronym variable from the contents of the text file I'm writing. I have a macro that:

  1. Brings up the Save dialog
  2. Brings up the Go to Path dialog using Command+Shift+G
  3. Types in the client name acronym using the ClientAcronym variable
  4. Adds the letter "t"
  5. Types a space to expand the snippet to the destination path for that client
  6. Types return

Voila! - the save dialog box is in the correct directory as dictated by the ClientName variable.

I'm thinking I must be missing a basic method of flow control that does:

If ClientAcronym = ACM, then path = .../Acme Corp/tickets/
If ClientAcronym = WIN, then path = .../Widget Industries/tickets/

Etc, etc. for 18 clients...

I know I could make this easier by having the client folder use the acronym as its folder name:

For ANY client the path is:  .../[clientAcronym]/tickets

But I have the thorny problem of transforming the ClientAcronym to the Client Name used in the folder path.

Is there a way to create a table to do the transformation from ClientAcronym to the proper client name, save it as a new variable ClientName, then build the path with that?

OR use a string of If/Then statements?

Any thoughts much appreciated!

Thanks,

  • eric

Hey Eric,

So your objective is to save the new ticket to the correct client folder?

What software are you using to create the ticket?

-Chris

Thank you, ccstone!

Yes, if variable ClientAcronym is "ACM", use folder path with client name Acme Corp
etc.

Ticket notes written in BBEdit

Have a look at the below - at the part where it says:

If a menu items is separated in to two parts with two underscores (_), then the first part is used as the result, while the second part is displayed in the menu. For example, “B__Good|F__Fail|C__OK|B__Good|A__Excellent”.

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

In your case you would do it the other way around i.e.

Acme Corp__ACM

Thank you, Zabobon!

I'm not following how this would help outside the context of creating an input form. I get my ClientAcronym by copying the text from my text document.

I missed that bit - in fact I think you didn’t mention that detail :slightly_smiling_face: But there will be a KM way (probably a switch case depending on what is in the Clipboard).

Oh, I found I could easily take the ClientAcronym variable and update variable ClientTicketPath using the Switch of Variable command, with one condition per ClientAcronym. Works great!

2 Likes

Hey Eric,

BBEdit is one of the most AppleScriptable apps available for the Mac.

It's not necessary to brute-force the macOS UI when you can simply create your ticket and then using AppleScript pick up the name of the ticket from the document and fully save it without resorting to the UI at all.

Here's a simple proof-of-concept macro:

Save Front Document as a New Ticket v1.00.kmmacros (5.8 KB)

Macro-Image

Keyboard Maestro Export

  • Install the macro.
  • Make sure the macro and its parent macro group are enabled.
  • Run the macro.
  • You'll end up with a file here:
    image

NOTE -- I'll need to see the ticket format to help you parse the front BBEdit document for the information needed to create the ticket file.

This macro will run almost instantly, and you wouldn't believe the flexibility you'll have by learning to script BBEdit.

-Chris

1 Like

Sweet! Thank you! I'm just finishing a busy week, and seeing this. I'm going to start pulling it apart now.

My tickets are formatted with the start time and end time first, followed by the 3 letter client acronym. (My actual variable name for that is ClientCode.) Then I have a blank line, then a bunch of text for ticket notes. So it would look like the following:


10:30-14:45 ACM

:: Update SSL certificate on firewall

  • etc etc etc

I have a script that saves startTime endTime clientCode ticketNotes etc., then enters them in a ticketing system.

But the problem I'm trying to solve right now is saving the new text file to the right folder. It grabs the last 3 characters of the first line and stores to var ClientCode and I need it to store to the ticket folder in the correct client.

In this case:

/Users/[myUserName]/Documents/MyCompany/Clients/Clients-Frequent/Acme Corp/tickets/

The solution that is working now uses the Switch of Variable as described above, but I need to get better at AppleScript and your solution looks quite elegant.

Thanks again!

Hey Eric,

Run this with the ticket data you demonstrated above to achieve the result below (only the MDD part – the Heinlein part was just another test).

BBEdit -- Save Front Document as a New Ticket v2.00.kmmacros (11 KB)

Display-Macro-Image

Keyboard Maestro Export

Result:

I suspect you'll want to do something different with the ticket name, but you'll have to tell me exactly how you want that to work.

I've made this very user-friendly in that:

  • The base client folder is easy to change.
  • The code - client name table is easy to change.
    • Personally I'd offload this to a file, so I could edit it with BBEdit.

In any case – you now have a fairly simple example of how powerful BBEdit and AppleScript are in combination – facilitated by Keyboard Maestro.

-Chris

WOWZA! I had thought the ball was in my court until I finished my homework. So sorry I've been away and missed this incredible reply. I'll have a blast unraveling this and getting it into my workflow.

Many, many thanks!

  • eric
1 Like