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


Edited – 2023/02/11 06:40 CDT

Improvements made to: Paste Clipboard as Markdown – Fenced Code Block v1.01
  • 2023/02/10 06:33
    • Added variable “local_CodeType”, so users can change it if they wish.

  • 2023/02/11 05:56
    • Added Browser-Find to select the script type text.
      • Should work in all modern web browsers.
      • If problems occur disable the orange group at the bottom of the macro.

  • 2023/02/11 06:18
    • Added delete past system clipboard to remove markdown from the clipboard.

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 format 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 Syntaxes Supported on the Forum:

Language syntax modules
applescript
awk
bash
cpp
css
diff
http
javascript
json
makefile
markdown
objectivec
perl
plaintext or text
python
ruby
sql
swift
xml

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


How To Use

  1. Select and copy the script/text you want to post.
  2. Switch to the KM Forum post you are editing, and position the cursor where you want the script to be pasted.
  3. Trigger this macro.
  4. Edit the script-type text as needed.

* Note – this macro should not be used where exact formatting of the pasted text is critical.


Download: Paste Clipboard as Markdown – Fenced Code Block v1.01.kmmacros (18 KB)

Macro Image


Keep in mind that you can also use the forum editor's toolbar when appropriate:

image


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

1 Like

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

3 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!