0

Safari, have the Bookmark all tabs, function in the menu Bookmarks -> Add Bookmarks for these N tabs. I looking for a way how to do this automatically with applescript in all opened Safari windows.

With other words, want

  1. choose some bookmark folder, let says "Favorites -> Sessions"
  2. cycle over all Safari windows
  3. if the window has only one TAB simply bookmark it into the above folder
  4. otherwise
    • call the Bookmarks -> Add Bookmarks for these N tabs menu item
    • fill the popup window automatically (e.g. Saved Tabs window1, Saved Tabs window2 ... etc.)
  5. repeat from 2

Here https://gist.github.com/kshiteesh/b72e93d31d65008fcd11 is a nice applescript, (also similarly this which could be an good starting point for the development, but asking first here - maybe someone already has done this. :)

Any help, please?

Ps - motivation - The Apple's new policy, (blocking support for legacy Safari extensions), unfortunately caused that one of "must to have" Safari extension - Sessions - stops working in the Safari 12. So, looking for some home-grown functionality.

clt60
  • 8,459
  • Just out of curiosity, why would you want to bookmark so many pages? And why would you keep many windows open instead of one with several tabs? – Mateus Ribeiro Oct 18 '18 at 14:57
  • @MateusRibeiro - just want bookmark pages when I quitting the browser. One window? Lol - just for fun - now I have opened 9 windows in 4 desktops and each window have 1 up to 9 opened tabs... Simply working (browsing) simultaneously on more topices... For example have 1 window (2 tabs) for stackexchange (stackoverflow and this), 1 window youtube - just 1 tab, 1 window arduino (9 tabs), 2 windows about World of warcraft 3 & 6 tabs), 2 windows for company intranet (1x ledger), 1x for managing my employes, and 2 more windows for other unspecified things.. :) :) Im pretty busy.. :) – clt60 Oct 19 '18 at 23:00
  • Firefox has reopen all windows and tabs as an option on restart. Doesn't safari have the same thing? – historystamp Jan 04 '19 at 20:57
  • @historystamp Let define the session as a list of all tabs in all windows. Then opening the last session (as you said) is something very different as have a possibility to store and retrieve an history of sessions. Just click on the killed addon page to see about what I talking. Bookmarking isn't the optimal solution, but better as nothing... – clt60 Jan 05 '19 at 11:23
  • Thanks for mentioning sessions. I'm collecting too many tab windows these days. – historystamp Jan 05 '19 at 21:00
  • I'd be a little pessimistic in getting this function working in the latest Safari. "Sessions will not install on Safari 12 or later, as Apple has officially deprecated Safari Extensions" from the page you linked. If the sessions author cannot figure out how to do the function why should any one else be able to do it? Recently, I looked into writing some javascript for Safari. It seems that Apple removed the free javascript insertion app. I'd have to pay a $100 to get a developers license. I'm in Yosemite. – historystamp Jan 05 '19 at 21:08
  • You have to consider moving to another browser. Apple has become antagonist to DIY. I did find this four year old script that seems to do something along the lines you want. See if you can install and get it to work.https://apple.stackexchange.com/questions/212132/adding-bookmarks-for-multiple-tabs-in-safari-with-a-keyboard-shortcut If so, someone should be able to modify it to do exactly what you want. Post back your results. I could attempt to get it to do what you need, if you can get it working. Goes without saying, I would have to be able to inject javascript into safari for free. – historystamp Jan 05 '19 at 21:13
  • I'm using Waterfox these days. It's a clone of Firefox. Supports legacy Firefox extensions. It's seem to be a trend to suppress DIY efforts in web browsers. Chrome is another possibility, but it has the same restriction on the modern day Firefox. – historystamp Jan 05 '19 at 21:19
  • @historystamp Yeah, i was talked with the author of the "Sessions" too and offered some donation, but unfortunately he says that isn't possible to continue due Apple's killing policy. So, now me using some automator workflow (applescript & perl) to save the url's into files (by date). I do not posted it as answer, yet, because it is halfway written in custom installed perl and it uses some perl modules - e.g. it is far from usability of average mac user. – clt60 Jan 07 '19 at 15:30

1 Answers1

3

As I told in the above comment, now me using an self-developed automator workflow-app.

I'm an perl language developer, so in the workflow me using an custom-installed perl interpreter and many perl modules too - definitely NOT a solution for an average (common) mac user.

But posting it, mainly for reference and maybe someone could hack it to some better solution. How it works - how to use it:

  • unfortunately it must be run manually - i'm unable figure out how to run some automator workflow (service or app) when the Safari receives the quit event. :(

  • The automator (service) script has two actions:

    • run javascript (I don't know applescript nor Javascript, but the javascript is more understandable for me) - it simply gathers all URL's from all tabs and windows and create a JSON structure, which is passed to the next
    • run a shell script - which runs my perl script with custom installed perl interpreter.
  • when the app is executed, it saves an HTML file into the folder Sessions in my Desktop.
  • The name of the file is like s20190107-164908.html e.g. the current date-time string.
  • it is easy to use the Finder's quick-look feature (spacebar) to quicky check the content of the file.

It is far-far beyond of the original Sessions addon, but it is usable for me.

enter image description here

The full content of the above actions are:

1.) the Javascript

function run(input, parameters) {
    var tablist = [];
    var Safari = Application('Safari');
    Safari.includeStandardAdditions = true;
    Safari.activate();
    var windows = Safari.windows();
    for(let iw=0, wsize=windows.length; iw<wsize; iw++) {
        var wintabs = [];
        var tabs = windows[iw].tabs();
        if (!tabs) continue;
        for(let it=0, tsize=tabs.length; it<tsize; it++) {
            if( tabs[it].url() ) {
                wintabs.push( {"name": tabs[it].name(), "url": tabs[it].url()} );
            }
        }
        if( wintabs.length ) {
            tablist.push(wintabs);
        }
    }
    return JSON.stringify(tablist);
}

and the shell script

export ANYENV_ROOT=/opt/anyenv
export PATH="$ANYENV_ROOT/bin:$PATH"
eval "$(anyenv init -)"

perl -MJSON -MPath::Tiny -MTime::Piece -M5.014 -w -E '
    my $jsonstr = do { local $/; <STDIN> };
    my $j = decode_json($jsonstr);
    my @lines;
    lsay("<ol>");
    for my $w (@$j) {
        lsay("<li><ul>");
        for my $t (@$w) {
            my $astr = "<li><a href=\"$t->{url}\">$t->{name}</a></li>";
            lsay($astr);
        }
        lsay("</ul></li>");
    }
    lsay("</ol>");
    my $sessdir=path( $ENV{HOME}, "Desktop/Sessions");
    $sessdir->mkpath;
    my $sessfile=$sessdir->child(localtime->strftime("s%Y%m%d-%H%M%S.html"));
    $sessfile->spew_utf8(@lines);;
    sub lsay { push @lines, $_ for @_ }
'

Example what do you see in the quick-look:

enter image description here

As I already told - it is NOT a solution for an common user. It is usable only by an developers... :(

clt60
  • 8,459
  • Interesting app, not for you: https://www.fireeye.com/blog/threat-research/2017/03/introducing_monitor.html – historystamp Jan 11 '19 at 04:54
  • Maybe you could install a keylogger like Keyboard maestro and trap the quit events, run your scripts, then relaunch the quit. very fragile You could certainly trap command + q not sure about the mousing around. – historystamp Jan 11 '19 at 04:57
  • I've developed a javascript program for Waterfox. It intercepts the user typing command+s. You could write a javascript to intercept what the user is doing and invoke your app when the user quits. https://discussions.apple.com/docs/DOC-250000368 – historystamp Jan 17 '19 at 21:55
  • @historystamp The main problem is that the automator's javascript isn't executed inside of Safari. It is a Applescript environment -javascript, not a browser javascript = different environment, e.g. no page-load events and like. Your script in the pastebin captures the keypress in a javascript runing inside of a browser. Also as I said, my Javascript knowledge is terrible. Thanx anyway. – clt60 Jan 18 '19 at 02:53