Sandboxing, and opening [label](file://...) urls?

If, in WriteRoom 4.0 Preview (415), I try to open ( ⌘:right_arrow_curving_left: ) a markdown link to a local file:// url, I get two message alert boxes.

  • Finder icon: The application “WriteRoom” does not have permission to open “01_Intro.indd”.
  • Transparent box: The link “file:///Users/…/01_Intro.indd” couldn’t be opened. No application is available to open it

I can open the same link in Bike 2 (Preview) 311 as long as an enclosing folder has been added to the Settings > Sandbox Extended Sandbox Folders list.

Might there be any way of securing permission to follow local file links, to files within particular folders, from WriteRoom 4.0 ?

In the meanwhile, FWIW, I find that I can open either:

  1. the nearest markdown [label](url) string to the text cursor (if the cursor is collapsed), or
  2. all urls in all lines containing the selection (if the selection is extended)

by assigning ⌘:right_arrow_curving_left: to the following Keyboard Maestro macro, which uses:

  • The writeroom CLI (WriteRoom > Install Command Line Tool...)
  • jaq to process json from the CLI with the jq language
  • xargs to pass any harvested urls to the command line’s open command.

Open URL(s) in selected WriteRoom 4.0 Preview (415) line(s).kmmacros.zip (2.1 KB)


Expand disclosure triangle to view script source
{
  $KMVAR_local_WRCLI get document -o json
  $KMVAR_local_WRCLI get selection -o json
} | $KMVAR_local_JAQ -r -s '
  .[0].text as $doc |
  .[1].start as $start |
  .[1].end as $end |
  
  ($doc[0:$start] | rindex("\n") // -1) + 1 as $line_start |
  ($doc[$end:] | index("\n") // length) as $rem |
  ($end + $rem) as $line_end |
  
  $doc[$line_start:$line_end] as $block |
  
  if $start == $end then
    ($start - $line_start) as $cursor |
    $block | [match("\\[[^\\]]*\\]\\(([^)]+)\\)"; "g")] |
    if length > 0 then
      map(
        .offset as $o | 
        (.offset + .length) as $e |
        (
          if $cursor >= $o and $cursor < $e then 0
          elif $cursor < $o then $o - $cursor
          else $cursor - ($e - 1) end
        ) as $dist |
        {url: .captures[0].string, dist: $dist}
      ) | sort_by(.dist)[0].url
    else
      empty
    end
  else
    $block | [match("\\[[^\\]]*\\]\\(([^)]+)\\)"; "g")][] | .captures[0].string
  end
'  | xargs open


Slightly Rube Goldberg or Heath Robinson approach, which sometimes means I have just missed a much simpler permission switch :slight_smile:

1 Like

I will looking to fixing the multiple alert boxes first, clearly that can be improved. I’m less sure about Bike’s extend sandbox solution at this stage… it would work, but seems a hack.

I think maybe the long term “clean” solution is a multi-file workspace. Then WriteRoom opens the containing folder and links within workspace open automatically. Of course problem is that isn’t going to get implemented for a while.

1 Like

Latest release fixes this up I think. It reveals the file in the finder when it can’t open it directly. For direct open your Keyboard Maestro solution is still needed, but this is at least a much better experience then before I think.

1 Like

Thank you – and quick response !

One tiny edge detail – not sure if it’s by design, or just an off-by-one index issue:

  • If the cursor is selecting the final ) of a markdown link string, the contained url is opened as expected, but
  • if the cursor is selecting the initial [, then the opening doesn’t happen – the cursor first needs to be moved one more position to the right.

PS I’ve updated (above) the Keyboard Maestro macro for direct opening in an application.

It now aims to open, in the relevant application, either:

  1. the url of the nearest markdown [label](url) string to the text cursor (if the cursor is collapsed), or
  2. all urls in all lines containing any part of the selection (if the selection is extended).