How to Post Code (Scripts) on the KM Forum; and Macro to Paste AppleScript in a Code Block

Hey Folks,

Many people posting AppleScripts and other code-blocks (see list below) on the forum don't know the best way to format it is like so:

```applescript
set volumes_ to {"vol 1", "vol2"} -- volume names
tell application "Finder"
  repeat with vol_ in volumes_
    eject disk vol_
  end repeat
end tell
```

** The ` character is commonly known as a Back quote or Backtick and usually shares the Tilde key on U.S. Keyboards.

The forum's software understands the “applescript” (and other) tag names along with the backtick fencing and produces nicely rendered output like so:

set volumes_ to {"vol 1", "vol2"} -- volume names
tell application "Finder"
  repeat with vol_ in volumes_
    eject disk vol_
  end repeat
end tell

Unfortunately the syntax-lexer plugin produces overly wide tabs and frequently makes the code more difficult to read, so my macro replaces each leading tab with 2 spaces to make the code a little more compact and readable and then wraps it with the appropriate AppleScript tags (the user may change the tag as needed).

When compiled in the Applescript Editor or in Keyboard Maestro Execute an AppleScript action those spaces are transformed back to tabs.

Since I paste a lot of AppleScripts into the forum I have this macro bound to V

-Chris


Programming language syntax modules currently available on the Keyboard Maestro forum.

applescript
awk
bash
cpp
css
diff
http
javascript
json
makefile
markdown
objectivec
perl
plaintext
python
ruby
sql
swift
xml

[List Edited – 2019/03/06 15:28 CST]


Macro: Paste AppleScript as Markdown (in a Code Block) v1.00

Paste AppleScript as Markdown v1.00.kmmacros (4.6 KB)

How To Use

  1. Select and copy the script you want to post
  2. Switch to the KM Forum post that you are editing
  3. Position the cursor where you want the script to be pasted
  4. Trigger this macro.

7 Likes

This is nice I had no idea the forum supported that. I guess there would be too many buttons if they had one for AppleScript, JavaScript, Perl, Python, Ruby, Shell and Swift. Do all of these have their own forum compilers and how in the world did you know this?

I posted a macro some time ago that works for any language. . It is triggered by a typed string km.sb.<lang>, where <lang> is “as” for AppleScript and “js” for JavaScript and JXA, or whatever language you want to use.

See

MACRO: Paste Script Block in Forums and Apps

Thanks Michael, I just downloaded and ran it. It has taken about all I have to just learn AppleScript and I am not even really good at that. I’ll keep this on hand if I ever paste any other code into my forum posts. You guys constantly amaze me.

1 Like

And then how do you make the keyboard formating on your shortcuts?

Hey Jimmy,

Here’s that syntax for the forum:

<kbd>⇧</kbd><kbd>⌃</kbd><kbd>⌥</kbd><kbd>⌘</kbd><kbd>L</kbd>

L

-Chris

2 Likes

I’ve added a list of available Programming language syntax modules to post #1.

-ccs

Thanks for this, Chris; I've been wondering how you've been accomplishing that. I just made a quick macro to make it a little easier to use this myself, so here it is in case someone else finds it useful too:

Format Keys.kmmacros (3.6 KB)
37 PM

2 Likes

Hey Gabe,

I have several, but they’re all in Typinator.  :wink:

-Chris

Do I highlight the text first and then run the macro?

@bocciaman, if you are referring to the macro by @ccstone, then, as you can see, the macro STARTS with processing the data on the Clipboard:

So you would need to first select and copy the script you want to post, switch to the KM Forum post that you are editing, position the cursor where you want the script to be pasted, and then trigger the macro.

1 Like

Got it, so copy first. Thank you!