Get Fragment URL in Safari

This macro helps building a fragment URL for a web page. (An URL that points to a section heading on a web page.)

A fragment URL consists of the base page URL and the URL fragment with the fragment identifier. Typically it looks like this:

https://wiki.keyboardmaestro.com/manual/Single_Page_Version#Macro_Actions

The part after the # is the fragment identifier.

These URLs are especially nice for sharing information, since they scroll the page automatically to the section of interest.

How to use the macro

  1. Open a web page in Safari
  2. Select a section heading (or a part of it) with the mouse
  3. Press the macro hotkey (default ⌘F3) and wait 2 seconds

The complete URL (base URL+#fragment) is now on your clipboard and ready to be pasted.

Alternatively:

  1. Open a web page in Safari
  2. Press the macro hotkey (with nothing selected)

In that case the macro collects all fragment identifiers from the page and presents you with a prompt where you can select one.

Techniques

When the macro finds an identifier that matches your selection, it will take the first one and not look further. So be clever when selecting a section heading:

  • Select the entire heading, or just one word, or more words.
  • Select something unique: do not select a single word that appears in several headings on the page (“the”).
  • Your selection may contain Spaces; the macro converts Spaces to “_” or “-” or “”.
  • Do not select non-letter chars like * or ? or », because it is uncertain how/if they are represented in the identifier in the html source.

Anyway, if the search fails, it’s not the end of the world. If the macro doesn’t find any match, it will present you with all identifiers of the page. Just as if you had selected nothing. (See above.)

Failure will happen

  • Some web pages just don’t have any identifiers. You’ll get a message in that case.
  • Many pages have identifiers only for some headings. If you happened to select a heading without identifier the macro will show you all identifiers it has found, so you can select the nearest one.
  • Some old pages may use name="…" instead of id="…". The macro does not search for name=.

Other

When the macro has successfully built a fragment URL it will open that URL in a new Safari tab. This way you see immediately if you got the right identifier.

If you want to disable that behavior just delete or disable the very last (green) action in the macro.


Get Fragment URL.kmmacros (15.2 KB)

8 Likes

Very clever. Thank you.

That is so so useful. Thank you so much for this.

Update (0.9.3)

Here is a little update for this exciting macro:

Get Fragment URL 0.9.3.kmmacros (17.5 KB)

Requires KM8+

Changes

  • Instead of the traditional User Prompt the macro now uses a Prompt with List.
  • Selecting from a Prompt with List is more comfortable than selecting with the popup button of the traditional prompt.
  • If there are more than one direct hits you can now select from a list.
  • This allows you to make “less strict” selections. (The old version just took the first hit without any further prompt.)
  • If no ID can be found that fits your selection, the macro still displays a list with all IDs on the page.
  • Search is now case-insensitive.
  • IDs containing double underscores are handled properly.
  • Changed all variables to local variables.

IDs in the List Prompt:

Have fun!

2 Likes