Help with some Shell Scripts

I'd like some help combining two simple shell scripts, and a couple of regex search & replaces, into one shell script. I'll try to make this as simple as I can.

The purpose of this is to take the "StyledText" from a Comment Action's XML, and convert it to HTML. My existing code works.

Here's the steps:

  1. Save "StyledText" data, which is actually a base64-encoded string, to disk file "%Variable%Local_base64TempFilePath%".

  2. Convert from base64 to text, using a shell script:
    base64 -d -i "$KMVAR_Local_base64TempFilePath"
    -- into variable "Local_rtfText"

  3. Run two regexes to remove cruft from start and end of Local_rtfText. Each search & replace replaces the matched text with nothing:
    ^([^{]*)
    }[^}]*$

  4. Save to disk file "%Variable%Local_rtfTempFilePath%".

  5. Convert to HTM, using a shell script:
    textutil -convert html "$KMVAR_Local_rtfTempFilePath" -stdout

I think the steps 2, 3, and 5 could be combined into one shell script. Preferably with some error checking, but that's probably not feasible, and in theory this shouldn't fail (ha ha).

Click to see an image of the macro

image

Convert StyledText to HTML.kmmacros (8.0 KB)

I know some of you guys live for this kind of a challenge, so I'm hoping this is tempting enough. :stuck_out_tongue:

Is there a reason why you aren't just "Filter: Decode Base64"ing the variable rather writing a temp file then shelling out? And you could then run textutil on your variable via stdin:

Convert StyledText to HTML v2.kmmacros (5.8 KB)

Image

Or is the idea that moving as much as possible from "native" KM actions to a single shell script might be more performant?

There actually is a reason, and it's because I want to do entire thing from JXA. With that said, I have to admit I didn't even think of that Filter option. :roll_eyes: Not that it'll help this specific situation, but I have another macro this will help in. So, thanks!

Then -- do you actually need the cruft-trimming? This suggests not, and gets everything done in a single shell action:

Convert StyledText to HTML (shell).kmmacros (4.4 KB)

Image

It seems that, at least for a "simple" rtfd package, textutil is smart enough to deal with things itself.

5 Likes

You, my friend, are a perfect example of the greatness of this forum. I can't thank you enough. I'm smiling from ear-to-ear!

Thanks again.

3 Likes