# omni-schema-src.R -rw-r--r-- 2.2 KiB View raw
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
library(tidyverse)

omnijs <- function(cmd, wait = 0.10) {
  browseURL(
    sprintf("omnigraffle:///omnijs-run?script=%s", URLencode(cmd))
  )
}

omni_schema_src <- function(src) {
  
  db_tbls <- src_tbls(src)
  
  start <- 0
  max_len <- 0
  sz <- 9
  space_over <-  10 * sz
  
  map_chr(db_tbls, function(tbl_name) {
    
    suppressWarnings(
      tbl(src, tbl_name) %>% 
        head(1) %>% 
        collect() %>% 
        as.data.frame() %>% 
        summarise_all(pillar::new_pillar_type) %>% 
        gather(variable, class) -> schema
    )
    
    rows <- as.character(glue::glue('{schema$variable} <{schema$class}>'))
    
    map_chr(1:nrow(schema), ~{
      as.character(glue::glue("\ntable.graphicAt({.x}, 0).text = '{rows[.x]}'\n"))
    }) %>% 
      paste0(collapse = "\n") -> sch
    
    if (start != 0) start <<- start + space_over
    
    max_len <<- max(nchar(rows))
    
    glue::glue("

cnvs = document.windows[0].selection.canvas
rect = new Rect({start}, 0, {max_len} * {sz}, 26)

graphic = cnvs.addShape('Rectangle', rect)

// graphic properties
graphic.strokeThickness = 0.25
graphic.strokeColor = Color.black

// solid properties
graphic.fillColor = Color.white
graphic.fillType = FillType.Solid

// solid text properties
graphic.text = ''
graphic.textSize = 12
graphic.textColor = Color.black
graphic.textHorizontalAlignment = HorizontalTextAlignment.Left
graphic.textVerticalPlacement = VerticalTextPlacement.Middle
graphic.fontName = 'Monaco'
graphic.textHorizontalPadding = 10
graphic.textVerticalPadding = 0

// create the base table
table = new Table(graphic)
table.columns = 1
table.rows = {nrow(schema) + 1}

// adjust header row and column size
table.setRowHeight(36, 0)
table.graphicAt(0, 0).text = '{tbl_name}'
table.graphicAt(0, 0).textHorizontalAlignment = HorizontalTextAlignment.Center
table.graphicAt(0, 0).fillColor = Color.black
table.graphicAt(0, 0).textColor = Color.white
table.graphicAt(0, 0).textSize = 14

{sch}

") %>% as.character() -> out
    
    start <<- start + (max_len * sz)
    
    out
    
  }) -> schema_out
  
  omnijs(paste0(schema_out, collapse = "\n\n"))
  
}

src_sqlite("~/Library/Application Support/Knowledge/knowledgeC.db") %>% 
  omni_schema_src()