Okay, here's a Bash version that uses a fixed table-width.
No real advantage over Drang's – just AWTDI.
-Chris
Make Two Column Table with . Leader (Shell) v1.00.kmmacros (6.4 KB)
Okay, here's a Bash version that uses a fixed table-width.
No real advantage over Drang's – just AWTDI.
-Chris
Make Two Column Table with . Leader (Shell) v1.00.kmmacros (6.4 KB)
I like the walrus operator, as I’ve always thought Python’s handling of regexes is too verbose. But you have to be running Big Sur (or have a version of Python installed outside of /usr/bin
to have it available. I wanted a script that would run on Catalina, too.
That's a good caution. My "expect the then current release of my code to require 3.8 or higher from March 1" is consumable for all - but a pain for some.
It's a balance between impetus to move forwards and making impossible demands of users.
(But I'm in Titus Andronicus Mode again , digressing too much.)
Hey Folks,
This one was entirely for fun.
I set out to create the most obfuscated, tersified AppleScript I could for the task.
I wouldn't use this in a production script of course.
-Chris
Make Two Column Table with . Leader (AppleScript) v1.00.kmmacros (9.4 KB)
A perennial problem for developers.
Homebrew and clear instructions solves that.
I use MacPorts myself and HB for a very few things not available on MP.
Since MP is a bit more complex and difficult to use, I usually recommend HB to users who aren't command line savvy.
-Chris
Throwing a wrench,
@drdrang if I ever need to add some text AFTER "Working", how I'd do that?
ie:
8 - 10am Working, saw video of people with Cat filter on on video
nice, gotta say it's somewhat readable to hoomans
As in (ignoring other associations) third column (and thus up to Nth column) ?
This one uses a fixed-width for field 1 of the table including the . leaders.
Field 2 is flexible but must begin with ‘Working’ or ‘Meeting’.
-Chris
Make Two Column Table with . Leader (Perl) v1.00.kmmacros (7.3 KB)
If you can accept having SPACE filled text, then the simple bash column
command will do.
But if you want exactly what you have posted, then you will need a couple of RegEx Search/Replace Actions to replaces the SPACES with dots. Maybe one of the Shell Script gurus will know how to make the bash column
command use a different fill character.
Below is just an example written in response to your request. You will need to use as an example and/or change to meet your workflow automation needs.
Please let us know if it meets your needs.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-~~~ VER: 1.0 2021-02-10 ~~~
Requires: KM 8.2.4+ macOS 10.11 (El Capitan)+
(Macro was written & tested using KM 9.0+ on macOS 10.14.5 (Mojave))
Create Text Table from TAB Delimited Text [Example].kmmacros
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.
Author.@JMichaelTX -- based on script by @ccstone
PURPOSE:
HOW TO USE
MACRO SETUP
REQUIRES:
TAGS: @TextTable @Regex @ShellScript @Example
USER SETTINGS:
ACTION COLOR CODES
==USE AT YOUR OWN RISK==
Depends.
Do you still want the Working and Meeting to line up and let the extra stuff after that trail off to various lengths? If so, just add (.*)
to the end of the regular expression and add the 3rd captured group to the end of the output, i.e.,
#!/usr/bin/python3
import re
import fileinput
entry = re.compile(r'^(.+)(Meeting|Working)(.*)$')
for line in fileinput.input():
m = entry.match(line)
if m:
print(f'{m.group(1):.<21s} {m.group(2)}{m.group(3)}')
That turns
8 - 10am Working, saw video of people with Cat filter on on video
7 - 9am Working
9 - 11am Meeting with people I hate
11 - 11:30am Meeting
11:30am - 12:30pm Meeting
12:30pm - 1pm Working
2 - 5pm Working
into
8 - 10am ............ Working, saw video of people with Cat filter on on video
7 - 9am ............. Working
9 - 11am ............ Meeting with people I hate
11 - 11:30am ........ Meeting
11:30am - 12:30pm ... Meeting
12:30pm - 1pm ....... Working
2 - 5pm ............. Working
If that's not the kind of output you want, the answer will probably be more complicated.
(If you put .*
after Working and Meeting in the second capture group, there's no need for a third group, and the print
line could go back to what it was. I added the third group because I thought it made the code clearer. Now I'm not sure there's any value in it.)
wow
﹡Insert sunglasses reaction meme ﹡
thanks everyone!
It was a good question and harvested well, not only because it clearly showed:
but also, I think, because it touches on some very general and recognisable problems that everyone bumps into:
I wonder if it would be useful to have a (custom or buillt-in) KM action for tabulation of delimited rows ?
"Emplace".
Okay, I'll try again...
-Chris
--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2021/02/12 06:10
# dMod: 2021/02/12 06:37
# Appl: AppleScript
# Task: Create a Fixed-Width Table with Data 03.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Create, @Fixed_Width @Table
--------------------------------------------------------
set theData to paragraphs 2 thru -2 of "
7 - 9am Working
9 - 11am Meeting
11 - 11:30am Meeting
11:30am - 12:30pm Meeting
12:30pm - 1pm Working
2 - 5pm Working
"
property tableWidth : 29
property dotPad : "........................................"
set AppleScript's text item delimiters to {" "}
repeat with i in theData
tell i to set contents of i to (its text items 1 thru -2 as text) & space & (text 1 thru (tableWidth - 2 - ((length of (text items 1 thru -2 of i as text)) + (length of (last text item of i)))) of dotPad) & space & (its last text item)
end repeat
set AppleScript's text item delimiters to linefeed
return theData as text
--------------------------------------------------------
If I set the shell script action to display text in a window, everything looks great:
However, if I save the script result to a variable and then use the Display Text in a Window action, I get:
If I paste the misaligned output here, it looks fine:
Local__Test: [test1]
--------------------------------------------------------------------
Local__LongTestVariableName: [test2]
--------------------------------------------------------------------
So what gives?
Change the font of the display text action to a monospaced font.
I typically use Menlo 14.
Nice! Thanks Chris!
Here's an evolution of the same concept:
Align Text as Columns.kmmacros (23 KB)
Example input:
Instrument: Name: Wife: Years Married
Rhythm Guitar: John: Yoko: 69-80
Bass: Paul: Linda: 67-98
Lead Guitar: George: Pattie: 66-77
Drums: Ringo: Maureen: 65-75
Output:
Instrument Name Wife Years Married
Rhythm Guitar John Yoko 69-80
Bass Paul Linda 67-98
Lead Guitar George Pattie 66-77
Drums Ringo Maureen 65-75
Options:
Set to 0
, a single space will separate columns. Any positive value will add addional spaces.
Set the delimiter that will differentiate column bounds.
Thanks @noisneil for creating this macro. I don't need it right now, but I know I will need it one day.
ChrisQ