Insert Call Info Via AppleScript Using Keyboard Maestro Variables

Howdy folks, in the never-ending quest to streamline some of my macros, I've come across something that has me stumped.

I am trying to use an AppleScript to insert text into a TextEdit file using 4 different Keyboard Maestro variables. I have figured out how to get the variables in the AppleScript itself, and how to paste one of them in...but I want to paste all four, each one on a new line. Those of you who are familiar with my previous posts these last few months will recognize these variables no doubt.

I want to do this because the AppleScript can insert this text into the TextEdit window without calling it to the front, meaning it will not interrupt my workflow when the macro runs. (Previously I would call the window to the front and simply paste from the clipboard)

I can't for the life of me figure out how to write the script so it inserts all four variables, each one on a new line. I've read the wiki pages on scripting and variables, searched the forum here, as well as several others I browse but haven't found what I'm looking for.

Any help is appreciated!

P.S. Thanks @ccstone for your upload macro to forum macro you sent me the other day. It's the first time I've used it and I love it!

Insert call info via AppleScript using Keyboard Maestro variables Macro (v9.2)

Insert call info via AppleScript using Keyboard Maestro variables.kmmacros (2.4 KB)

Try this:
Insert call info via AppleScript using Keyboard Maestro variables v2.kmmacros (2.5 KB)

That did it, thanks so much! I figured it was something simple but I couldn’t find anything that specified how to place text on separate lines. I was trying RegEx and delimiters and nothing was working :laughing:

1 Like

Hey Guys,

This is how I'd write that script.

-Chris

--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2021/10/01 19:23
# dMod: 2021/10/01 19:23 
# Appl: Keyboard Maestro Engine, TextEdit
# Task: Telephone Call Record from Keyboard Maestro Variables v1.50
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @KMF, @ccstone, @cdthomer, @Keyboard_Maestro_Engine, @TextEdit, @Telephone, @Call, @Record
# Vers: 1.50
--------------------------------------------------------
property LF : linefeed
--------------------------------------------------------

set {oldTIDS, AppleScript's text item delimiters} to {AppleScript's text item delimiters, LF}
tell application "Keyboard Maestro Engine"
   set callLogRecord to {¬
      getvariable "LB Caller", ¬
      getvariable "Site Name", ¬
      getvariable "Reference Number", ¬
      getvariable "Agent Name", ¬
      "", ¬
      ""} as text
end tell
set AppleScript's text item delimiters to oldTIDS

# Written long-form, because it's more readable, and 
# you never know when you'll have to insert more code.
tell application "TextEdit"
   tell front document
      tell its text
         make new paragraph at end with data callLogRecord
         set its font to "Menlo Regular"
         set its size to 14
      end tell
   end tell
end tell

--------------------------------------------------------

(*

CHANGE NOTES:

   - 2021/10/01 19:32
      - Refactored a bit for efficiency.

*)

--------------------------------------------------------

But – TextEdit scripting is pretty nasty, and I avoid it whenever possible.

I'd rather use BBEdit any day if styled-text is not an issue. It's way more scriptable than TextEdit, and the syntax is way more rational. (Even in the freeware “Lite” version.)

When I have to use a styled-text editor I usually use Jedit Ω.

I've used MATSUMOTO Satoshi's Jedit rich-text editor series for somewhere around 20 years now.

-Chris

Hey Chris, thanks for providing your version as well. What are the pros/cons of the way you scripted it?

I always hear about BBEdit…and thinking forward to other ways I might implement AppleScript has me wanting to download it to try out since you say it’s more scriptable.

Readable and easy to edit/change.

Of course if you don't understand coercing a list to text, you'll scratch your head a bit over set callLogRecord to {¬.

Most people are unaware of the vertical list notation I'm using. It allows one to visualize the end product.

Here's an example script for BBEdit.

--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2021/10/01 21:21
# dMod: 2021/10/01 21:21
# Appl: BBEdit, Keyboard Maestro Engine
# Task: Telephone Call Record from Keyboard Maestro Variables.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @KMF, @ccstone, @cdthomer, @Keyboard_Maestro_Engine, @TextEdit, @Telephone, @Call, @Record
# Vers: 1.00
--------------------------------------------------------
property LF : linefeed
--------------------------------------------------------

set {oldTIDS, AppleScript's text item delimiters} to {AppleScript's text item delimiters, LF}
tell application "Keyboard Maestro Engine"
   set callLogRecord to {¬
      getvariable "LB Caller", ¬
      getvariable "Site Name", ¬
      getvariable "Reference Number", ¬
      getvariable "Agent Name", ¬
      "", ¬
      ""} as text
end tell
set AppleScript's text item delimiters to oldTIDS

tell application "BBEdit"
   set newDoc to make new document with properties {name:"Call Log"}
   tell newDoc
      set after its text to callLogRecord
      select insertion point after its text
      set bounds of its window to {0, 45, 720, 900}
   end tell
end tell

--------------------------------------------------------

-Chris

I had seen vertical notation before but am unfamiliar with how to write it, so thank you for this example. I'm going to play around with that to see how I like it.

It appears that this will insert the text after any existing text that is already in the document...is that correct?

Affirmative.

Any way to accomplish the same in TextEdit? I downloaded BBEdit and have been tooling around with it but it honestly looks like major overkill for my work notes...

You did try (and read) my script in Post #4?

  • Sure it is, but the lite version is free – so who cares?

  • You need better access to a regular expression savvy text editor anyway.

  • You should try out the Pattern Playground in the pro version.

  • You should try out the notes organizer in the pro version. I wouldn't use it for calls, but it's great when you're collating text to process later.
    ⠀⠀- New feature – not yet fully fleshed-out.

-Chris

Dang, I did, and didn’t realize that it was written that way. I copied it, tested it, and immediately made some adjustments and since I wasn’t actually working (and didn’t have any actual notes already written in that file), didn’t realize it was scripted to insert the info at the end of the document. Thanks for pointing that out and for your patience. It seems like half the questions I ask you you’ve already answered and I simply missed it :sweat_smile:

I’ll keep tooling around with BBEdit since I always like getting to know new software. Thanks again!

1 Like

Hey Chris,

Vertical list notation has the advantage of being more readable and easier to edit and sort that the flat – unless you're dealing with a long list.

set theList to items 1 thru -2 of {¬
   "one", ¬
   "two", ¬
   "three", ¬
   "four", ¬
   "five", ¬
   ""}

I don't like the terminator on the same line as the last item.

You could also write it like this:

set theList to items 2 thru -2 of {"", ¬
   "one", ¬
   "two", ¬
   "three", ¬
   "four", ¬
   "five", ¬
   ""}

For text items I'm more likely to use this format:

set theList to paragraphs 2 thru -2 of "
one
two
three
four
five
"

You wouldn't what to use the list assignment technique for this job, but here's an example of that:

tell application "Keyboard Maestro Engine"
   
   set {¬
      lbCaller, ¬
      siteName, ¬
      refNum, ¬
      agentName, ¬
      null} to {¬
      getvariable "LB Caller", ¬
      getvariable "Site Name", ¬
      getvariable "Reference Number", ¬
      getvariable "Agent Name", ¬
      ""}
   
end tell

-ccs