File links/URLs within BBEdit documents

733 views
Skip to first unread message

Craig Heilman

unread,
Jan 10, 2022, 7:24:29 PM1/10/22
to bbe...@googlegroups.com
In my local note files (text but with .md file extension), I often include links to other files using the file URL scheme (“<file:///User/me/…>”) as I can then easily navigate to those files by simply Cmd-clicking anywhere in the URL. For several years I’ve been using Path Finder as a Finder replacement and it provides a context menu item of “Copy Path -> URL” which makes it pretty easy to get a file URL in the proper format (i.e., it automatically encodes spaces etc.).

I’m transitioning to a new MacBook Pro (M1 Max - sweet!) and moving from Mojave to Monterey in the process. I’m also considering moving from Path Finder to ForkLift or similar or even just the plain old Finder. I know I can get the *path* to any file in the Finder or ForkLift but to use it as a URL in BBEdit would require massaging the copied data.

Bringing this back to BBEdit - is there anything in BBEdit that can simplify this process? I like the idea of starting with the path because that doesn’t lock me into any of the Finder/Finder replacements but how to make this as close to copy/paste as possible?

Thanks,

Craig

P.S. anyone using hook (https://hookproductivity.com) for stuff like this?

--
Craig Heilman
Bugaboo Software



Bruce Van Allen

unread,
Jan 10, 2022, 8:26:35 PM1/10/22
to bbe...@googlegroups.com
Hi Craig,

If you click the little document icon upper left in the BBEdit window,
you'll see that it provides a "Location" link, with a button to copy it
to the clipboard. That handles the encoding. Only works on a file saved
to disk, obvs :)

I haven't looked, but that might also be available as an Applescript
property, in case you wanted to script making the links in your docs.

HTH

Craig Heilman wrote on 2022-01-10 3:45 PM:
> ringing this back to BBEdit - is there anything in BBEdit that can
> simplify this process? I like the idea of starting with the path because
> that doesn’t lock me into any of the Finder/Finder replacements but how
> to make this as close to copy/paste as possible?
>


--
    - Bruce

_bruce__van_allen__santa_cruz__ca_

Christopher Waterman

unread,
Jan 10, 2022, 8:29:08 PM1/10/22
to bbe...@googlegroups.com
Craig,

So you are want to open urls to files and folders from an open document in bbedit?
Assuming I got that right, there is an 'Open URL' contextual menu. Just right (ctrl) click on a url and have at it.

There is also Edit > Copy Path > Copy URL to get an escaped version of the open file’s URL.

Also 'CMD I' will open an info panel on the open doc that you can copy the path from.

Does that help?
— Chris(topher)?



--
This is the BBEdit Talk public discussion group. If you have a feature request or need technical support, please email "sup...@barebones.com" rather than posting here. Follow @bbedit on Twitter: <https://twitter.com/bbedit>
---
You received this message because you are subscribed to the Google Groups "BBEdit Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bbedit+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bbedit/011C58C7-5691-4137-A6F0-D7B057A53440%40bugsoft.com.

Craig Heilman

unread,
Jan 10, 2022, 10:50:38 PM1/10/22
to bbe...@googlegroups.com
Hi Chris,

Yes, the ‘Open URL’ command is what I have been using but I bypass the contextual menu by simply Cmd-clicking anywhere in the URL.

Good to know about the Edit -> Copy Path -> Copy URL command and the info panel so there’s lots of ways to get a file URL of the current BBEdit document from within that document. As I noted in my response to Bruce, I also need a way to get file URLs for non-text documents or locations such as PDF, Word docs, jpgs, folders, etc. but I suspect I need to go outside BBEdit for that.

Thanks,

Craig


Craig Heilman

unread,
Jan 10, 2022, 10:50:38 PM1/10/22
to bbe...@googlegroups.com
Hi Bruce,

Hmm, on my system (BBEdit 14.0.4 under Monterey 12.1) the little document icon is in the upper right of the BBEdit window but I found it eventually. Yes, that’s helpful knowing there’s a way to get a file URL (“location”) directly. I see that Cmd-I is another way to get to that information.

I guess I should have added that I also like to include links to non-text documents or locations (e.g. PDF, Word, jpg, folders, etc.) so I need to get file URLs for those entities as well.

Thanks,

Craig


--
This is the BBEdit Talk public discussion group. If you have a feature request or need technical support, please email "sup...@barebones.com" rather than posting here. Follow @bbedit on Twitter: <https://twitter.com/bbedit>
--- You received this message because you are subscribed to the Google Groups "BBEdit Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bbedit+un...@googlegroups.com.

Bruce Van Allen

unread,
Jan 10, 2022, 11:25:08 PM1/10/22
to bbe...@googlegroups.com

Craig Heilman wrote on 2022-01-10 7:44 PM:
> Hmm, on my system (BBEdit 14.0.4 under Monterey 12.1) the little
> document icon is in the upper *right*

Sorry, my bad - mine's on the right, too.

For non-BBEdit files, what would be your preferred steps?

E.g., select the file in the Finder, and invoke a script to grab its
path and then handle any necessary encoding, add file:/// protocol, etc.

Or maybe something connected with the Open... menu command?

Drag the file over a script icon?


--
    - Bruce

_bruce__van_allen__santa_cruz__ca_

Christopher Waterman

unread,
Jan 11, 2022, 2:31:47 AM1/11/22
to bbe...@googlegroups.com
Craig,

If you drag and drop a text file on to an open bbedit doc it will insert the contents at the cursor.
But if you hold the cmd key it will insert and select a path.

You could then run a script to encode it.

Here is an example that works for me but I haven’t tested much.
Install it into BBEdits Scripts folder if want to give it a try.


Make File URI.applescript
—————————————

tell application "BBEdit"
tell window 1
set _path to selection as text
set _cmd to "ruby -rURI -e "
try
set _uri to do shell script _cmd & quoted form of ("puts URI.join( \"file:///\", URI.escape(\"" & _path & "\".chomp) )")
on error
set _uri to _path
end try
set selection to _uri
end tell
end tell

——————————————

It will change this (the dropped path):
/Users/chris/Desktop/untitled text 10.txt

Into this:
file:///Users/chris/Desktop/untitled%20text%2010.txt

Food for thought:

BBEdit has something called Attachment Scripts that allow you to invoke a script on an event.
Page 343 from the user manual ( found in the Help menu ).

I don’t think they have an attachment point for dropped text files, they recently added one for dropped image files.
But you could have an attachment script run a text filter and convert all the paths in the doc into file URIs when you save or something.

Best of luck with your workflow,
Chris(topher)?



Jeffrey Jones

unread,
Jan 11, 2022, 9:14:34 AM1/11/22
to bbe...@googlegroups.com
On 2022 Jan 10, at 18:45, Craig Heilman <cra...@bugsoft.com> wrote:

Bringing this back to BBEdit - is there anything in BBEdit that can simplify this process? I like the idea of starting with the path because that doesn’t lock me into any of the Finder/Finder replacements but how to make this as close to copy/paste as possible?

Isn't it simply a matter of prepending the absolute path with "file://"? It seems like a simpleText Filter or script could do that.

Craig Heilman

unread,
Jan 11, 2022, 10:07:33 AM1/11/22
to bbe...@googlegroups.com
Well my preferred steps for any type of file would be just drag and drop, using one or more modifier keys as required, and the magic of generating the full “<file:///Users/craigh/rest of path to file>” and inserting in the BBEdit document would happen under the hood…

Craig

--
This is the BBEdit Talk public discussion group. If you have a feature request or need technical support, please email "sup...@barebones.com" rather than posting here. Follow @bbedit on Twitter: <https://twitter.com/bbedit>
--- You received this message because you are subscribed to the Google Groups "BBEdit Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bbedit+un...@googlegroups.com.

Craig Heilman

unread,
Jan 11, 2022, 10:07:40 AM1/11/22
to bbe...@googlegroups.com
The path may include spaces which need to be encoded (e.g., “%20”) so it’s not quite that simple…

Craig

--
This is the BBEdit Talk public discussion group. If you have a feature request or need technical support, please email "sup...@barebones.com" rather than posting here. Follow @bbedit on Twitter: <https://twitter.com/bbedit>
---
You received this message because you are subscribed to the Google Groups "BBEdit Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bbedit+un...@googlegroups.com.

Craig Heilman

unread,
Jan 11, 2022, 10:29:39 AM1/11/22
to bbe...@googlegroups.com
Hi Chris,

This script looks promising. How would you add the enclosing “<“ and “>” to make the file URI into a clickable markdown link?

I’ll take a look at the user manual for Attachment Scripts.

I suppose I could try looking at this from another angle - i.e., do the processing when trying to follow the link? For example:

Type backticks to enclose the dropped path as code to prevent the markdown processor from interpreting underscores, etc.:

``

Drop the file path between the backticks using drag and drop with the Cmd key (note my filename has underscores and spaces):

`/Users/craigh/Documents/my_test file.pdf`

Follow the link by clicking anywhere in the path (delimited by the backticks) with whatever key modifiers (Cmd, Opt, etc.) have be configured and have a script grab the entire path, encode it, then follow it like a link - i.e., the same as if you Cmd-click on the following:


The advantage of this approach is that I don’t have to do anything special on the front end to get the path into my BBEdit document and it works for any file type or folder…

Thanks,

Craig
 

--
This is the BBEdit Talk public discussion group. If you have a feature request or need technical support, please email "sup...@barebones.com" rather than posting here. Follow @bbedit on Twitter: <https://twitter.com/bbedit>
---
You received this message because you are subscribed to the Google Groups "BBEdit Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bbedit+un...@googlegroups.com.

Christopher Waterman

unread,
Jan 11, 2022, 8:52:58 PM1/11/22
to bbe...@googlegroups.com
Hey Bruce,

Here is script to encode the paths with '< >’ brackets.

------------------------------

tell application "BBEdit"
tell window 1
if (length of selection) = 0 then return
set _path to selection as text
set _cmd to "ruby -rURI -e "
try
set _uri to do shell script _cmd & quoted form of ("puts URI.join( \"file:///\", URI.escape(\"" & _path & "\".chomp))")
on error
set _uri to _path
end try
set selection to "<" & _uri & ">"
#set _sel_start to (characterOffset of selection)
#set _sel_end to (length of selection) + _sel_start
#select (characters (_sel_start + 1) thru (_sel_end - 2))
end tell
end tell

——————————————————

I commented out a section that will select the path and not the brackets.


On Jan 11, 2022, at 7:29 AM, Craig Heilman <cra...@bugsoft.com> wrote:

Follow the link by clicking anywhere in the path (delimited by the backticks) with whatever key modifiers (Cmd, Opt, etc.) have be configured and have a script grab the entire path, encode it, then follow it like a link - i.e., the same as if you Cmd-click on the following:


You could have a script do something like this; getting the path between the ticks and opening it. But you would have to invoke it through the script menu, the script palette, or a key command.

— Chris(topher)?

Bruce Van Allen

unread,
Jan 11, 2022, 8:56:37 PM1/11/22
to bbe...@googlegroups.com
Nice. Wasn't my question, but this is the type of thing I was thinking
might work for Craig.

Christopher Waterman wrote on 2022-01-11 5:52 PM:
> Hey Bruce,
>
> Here is script to encode the paths with '< >’ brackets.
> *
> *
> *------------------------------*
> *
> *
> tell application "BBEdit"
> tell window 1
> if (length of selection) = 0 then return
> set _path to selection as text
> set _cmd to "ruby -rURI -e "
> try
> set _uri to do shell script _cmd & quoted form of ("puts URI.join(
> \"file:///\ <file:///%5C>", URI.escape(\"" & _path & "\".chomp))")
> on error
> set _uri to _path
> end try
> set selection to "<" & _uri & ">"
> #set _sel_start to (characterOffset of selection)
> #set _sel_end to (length of selection) + _sel_start
> #select (characters (_sel_start + 1) thru (_sel_end - 2))
> end tell
> end tell
>
> ——————————————————
>
> I commented out a section that will select the path and not the brackets.
>
>
>> On Jan 11, 2022, at 7:29 AM, Craig Heilman <cra...@bugsoft.com
>> <mailto:cra...@bugsoft.com>> wrote:
>>
>> Follow the link by clicking anywhere in the path (delimited by the
>> backticks) with whatever key modifiers (Cmd, Opt, etc.) have be
>> configured and have a script grab the entire path, encode it, then
>> follow it like a link - i.e., the same as if you Cmd-click on the
>> following:
>>
>
> You could have a script do something like this; getting the path between
> the ticks and opening it. But you would have to invoke it through the
> script menu, the script palette, or a key command.
>
> — Chris(topher)?
>
> --
> This is the BBEdit Talk public discussion group. If you have a feature
> request or need technical support, please email "sup...@barebones.com"
> rather than posting here. Follow @bbedit on Twitter:
> <https://twitter.com/bbedit>
> ---
> You received this message because you are subscribed to the Google
> Groups "BBEdit Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to bbedit+un...@googlegroups.com
> <mailto:bbedit+un...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/bbedit/382411DC-ECA0-47E1-807B-4D52EEEB3480%40rustydogink.com
> <https://groups.google.com/d/msgid/bbedit/382411DC-ECA0-47E1-807B-4D52EEEB3480%40rustydogink.com?utm_medium=email&utm_source=footer>.


--
    - Bruce

_bruce__van_allen__santa_cruz__ca_

Christopher Waterman

unread,
Jan 11, 2022, 9:26:28 PM1/11/22
to bbe...@googlegroups.com
*Sigh* I’m not smart. That was meant for Craig.

>
> Nice. Wasn't my question, but this is the type of thing I was thinking might work for Craig.

— Chris(topher)?

Christopher Waterman

unread,
Jan 12, 2022, 2:38:44 AM1/12/22
to bbe...@googlegroups.com
Craig,

I remembered I have a script I use for checking if the cursor is in a string, and realized it would be easy to change it for your needs. At least as I understand them.

Here is a script that will follow a path that is in between back-ticks.

It will open the file or folder according to your systems defaults as it is just using the bash command ‘open’.
The ‘open’ command doesn’t need an escaped URI, a quoted path will do.

You can ⌘-drop a file into a markdown file, the path will be selected. Type a back-tick and it should wrap the path in back-ticks.
If you have ‘Insert matching delimiters while typing’ & ’Surround selected text when typing matching delimiters’ set in you preferences under ‘Completion'.

When you want to open the path just place the cursor on it (inside the back-ticks) and run the script.

If the path is incorrect it will beep, same if your cursor isn’t between back-ticks.

It would be cool to be able to hold a modifier key and click a path like a link, but there isn’t a way I know of to do that. You can set the script to a keyboard shortcut of your choosing though.

Open Ticked Path.applescript
-----------------------------------

on isPath()
tell application "BBEdit"
tell text window 1
set output to {hover:false}
set cursor to (characterOffset of selection) - 1
set cursorLine to startLine of selection
set searchStart to (characterOffset of line cursorLine)
set searchEnd to searchStart + (length of line cursorLine) - 1
if searchStart ≥ searchEnd then return output
set searchRange to characters searchStart thru searchEnd
set filePath to "`[^><|:&]+`"
set go to true
repeat while go
set filePathMatch to find filePath searching in searchRange options {search mode:grep}
if found of filePathMatch then
set match to found object of filePathMatch
set matchStart to (characterOffset of match)
if cursor < matchStart then set go to false
set matchEnd to matchStart + (length of match) - 1
if cursor = searchEnd and matchEnd = searchEnd then set go to false
if cursor ≥ matchStart and cursor < matchEnd then
set output to {hover:true, str_start_char:matchStart, str_end_char:matchEnd}
set go to false
else
try
set searchRange to characters (matchEnd + 1) thru searchEnd
on error
set go to false
end try
end if
else
set go to false
end if
end repeat
return output
end tell
end tell
end isPath

tell application "BBEdit"
tell text window 1
if hover of my isPath() is false then return beep
set pathStart to (str_start_char of my isPath()) + 1
set pathEnd to (str_end_char of my isPath()) - 1
set rawPath to characters pathStart thru pathEnd as text
try
do shell script "open " & the quoted form of rawPath
on error
beep
end try
end tell
end tell

______________________


— Chris(topher)?



jj

unread,
Jan 12, 2022, 12:50:07 PM1/12/22
to BBEdit Talk
An easy way to get the URI of a document from the finder on macOS Monterey:

  • Right-click on the document on the Finder
 
  • In the popup-menu select Share > Reminders
 
  • A modal window will open where you can copy the document URI
 
HTH,

Jean Jourdain



Screen Shot 2022-01-12 at 18.45.19.png

Screen Shot 2022-01-12 at 18.44.13.png

Christopher Stone

unread,
Jan 15, 2022, 7:45:12 PM1/15/22
to BBEdit-Talk
On Jan 12, 2022, at 11:50, jj <goo...@mixio.com> wrote:
An easy way to get the URI of a document from the finder on macOS Monterey:


This is easy enough to automate...

-Chris

--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2022/01/15 18:42
# dMod: 2022/01/15 18:42 
# Appl: Finder
# Task: Get File-URLs of Selected Items in the Front Finder Window.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Finder, @File-URLs, @Selected, @Items
--------------------------------------------------------

tell application "Finder"
    set finderSelectionList to selection as alias list
    if length of finderSelectionList = 0 then error "No files were selected in the Finder!"
    set theItem to item 1 of finderSelectionList

    

    repeat with i in finderSelectionList
        set contents of i to URL of i
    end repeat

    

end tell

set {oldTIDS, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {""}}
set the clipboard to finderSelectionList as text
set AppleScript's text item delimiters to oldTIDS

--------------------------------------------------------

Craig Heilman

unread,
May 5, 2022, 4:49:34 PM5/5/22
to bbe...@googlegroups.com
Resurrecting this old thread as it got lost in my inbox (sigh).

This latest script from Chris Stone (thanks Chris!) does 90% of what I’m looking for but since I’m an Applescript idiot, I’m looking for a little more assistance:

1. How can this script be modified to enclose the URLs with “<“ and “>” so that they are markdown links?

2. How can this script be modified to work for items selected in Finder or in Path Finder?

For reference, I saved a version of this script as an application and then assigned it a keyboard shortcut via Keyboard Maestro (which brings up another stupid question which is how to find all unused keyboard shortcuts?).

Thanks,

Craig


--
This is the BBEdit Talk public discussion group. If you have a feature request or need technical support, please email "sup...@barebones.com" rather than posting here. Follow @bbedit on Twitter: <https://twitter.com/bbedit>
---
You received this message because you are subscribed to the Google Groups "BBEdit Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bbedit+un...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages