Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save doraemoncito/c896304d343c7dc8a1f4fc380fe71b19 to your computer and use it in GitHub Desktop.
Save doraemoncito/c896304d343c7dc8a1f4fc380fe71b19 to your computer and use it in GitHub Desktop.

Save all links in the current Safari session to the macOS clipboard

Copy all links from Safari to the clipboard as a list of URLs in JSON format

Open automator and create an application script called safe-safari-session-to-clipboard-json.app.

  1. Add a JavaScript step with the following content

    function run(input, parameters) {
        var _urlList = [];
        var _safari = Application('Safari');
        _safari.includeStandardAdditions = true;
        _safari.activate();
        _safari.windows().forEach(function (_window) {
            _window.tabs().forEach(function (_tab) {
                _urlList.push({"name": _tab.name(), "url": _tab.url()});
            });
        });
        // Pretty print the stringified JSON object before returning it. For a condensed JSON string simply remove the last
        // two parameters from the method call below. I.e. return JSON.stringify(urlList);
        return JSON.stringify(_urlList, null, 2);
    }
  2. Add a copy to clipboard step

To test this application within Automator, add the “Get Specified Finder Items” action to the beginning of your workflow without forgetting to remove or disable the action before running the workflow outside of Automator.

Example output:

[
 {
    "name": "Teach, Learn, and Make with Raspberry Pi – Raspberry Pi",
    "url": "https://www.raspberrypi.org/"
  },
  {
    "name": "Home - BBC News",
    "url": "https://www.bbc.co.uk/news"
  }
]

Copy all links from Safari to the clipboard as a list of URLs in Markdown format

Open automator and create an application script called safe-safari-session-to-clipboard-markdown.app.

  1. Add a JavaScript step with the following content

    function run(input, parameters) {
        var _urlList = [];
        var _safari = Application('Safari');
        _safari.includeStandardAdditions = true;
        _safari.activate();
        _safari.windows().forEach(function (_window) {
            _window.tabs().forEach(function (_tab) {
                _urlList.push("- [" + _tab.name() + "](" + _tab.url() + ")");
            });
        });
        return _urlList.join("\n");
    }
  2. Add a copy to clipboard step

To test this application within Automator, add the “Get Specified Finder Items” action to the beginning of your workflow without forgetting to remove or disable the action before running the workflow outside of Automator.

Example output:

- [Teach, Learn, and Make with Raspberry Pi – Raspberry Pi](https://www.raspberrypi.org/)
- [Home - BBC News](https://www.bbc.co.uk/news)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment