123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221(*---------------------------------------------------------------------------
Copyright (c) 2018 The b0 programmers. All rights reserved.
Distributed under the ISC license, see terms at the end of the file.
---------------------------------------------------------------------------*)openB0_std(* macOS JavaScript automation *)letmacos_jxa?search()=Os.Cmd.find_tool?search(Fpath.v"osascript")letmacos_jxa_runjxascriptcli_args=letstdin=Os.Cmd.in_stringscriptinletcmd=Cmd.(pathjxa%"-l"%"JavaScript"%"-"%%cli_args)inOs.Cmd.(run_out~stdin~trim:truecmd)letmacos_jxa_default_browser_appidjxa=Result.map_error(fune->Fmt.str"default lookup: %s"e)@@macos_jxa_runjxa{|
ObjC.import('CoreServices');
var h = $.LSCopyDefaultHandlerForURLScheme($('http'));
$.CFStringGetCStringPtr (h, 0);
|}Cmd.empty(* Finding a browser *)moduleEnv=structletbrowser="BROWSER"endtypet=|CmdofCmd.t|Macos_chromeofCmd.tool(* this is osascript *)|Macos_safariofCmd.tool(* this is osascript *)|Macos_openofCmd.tool*stringoptionletbrowser_env_fallbackbrowser=matchbrowserwith|Some_asb->Okb|None->Os.Env.find'~empty_is_none:trueCmd.of_stringEnv.browserletfind_browser_cmd?searchcmd=Result.bind(Os.Cmd.find?searchcmd)@@function|None->OkNone|Somec->Ok(Some(Cmdc))letfind_macos_open?search~appid()=Result.bind(Os.Cmd.find_tool?search(Fpath.v"open"))@@function|None->OkNone|Sometool->Ok(Some(Macos_open(tool,appid)))letfind_with_macos_jxa?search~browserjxa=matchbrowserwith|Somecmdwhennot(Cmd.is_singletoncmd)->find_browser_cmd?searchcmd|Somecmd->beginmatchString.Ascii.lowercase@@List.hd(Cmd.to_listcmd)with|"chrome"->Ok(Some(Macos_chromejxa))|"firefox"->find_macos_open?search~appid:(Some"org.mozilla.firefox")()|"open"->find_macos_open?search~appid:None()|"safari"->Ok(Some(Macos_safarijxa))|_->find_browser_cmd?searchcmdend|None->Result.bind(macos_jxa_default_browser_appidjxa)@@funappid->matchString.Ascii.lowercaseappidwith|""->find_macos_open?search~appid:None()|"com.apple.safari"->Ok(Some(Macos_safarijxa))|"com.google.chrome"->Ok(Some(Macos_chromejxa))|appid->find_macos_open?search~appid:(Someappid)()letfind?search~browser()=Result.map_error(fune->Fmt.str"find browser: %s"e)@@Result.bind(browser_env_fallbackbrowser)@@funbrowser->Result.bind(macos_jxa?search())@@function|Somejxa->find_with_macos_jxa?search~browserjxa|None->matchbrowserwith|Somecmd->find_browser_cmdcmd|None->Result.bind(Os.Cmd.findCmd.(atom"xdg-open"))@@function|None->OkNone|Somexdg->Ok(Some(Cmdxdg))(* Show *)letshow_cmd~background~prefixcmduri=Os.Cmd.runCmd.(cmd%uri)letshow_macos_open~background~prefix:_open_tool~appiduri=letappid=matchappidwith|None->Cmd.empty|Someappid->Cmd.(atom"-b"%appid)inletcmd=Cmd.(pathopen_tool%%if'background(atom"-g")%%appid)inOs.Cmd.runCmd.(cmd%uri)letshow_macos_jxaname~background~prefixjxauriscript=letbool=string_of_boolinletargs=Cmd.(atom(boolbackground)%%atom(boolprefix)%uri)inmatchmacos_jxa_runjxascriptargswith|Ok_->Ok()|Errore->Fmt.error"%s jxa: %s"nameeletshow_macos_chrome~background~prefixjxauri=(* It seems we no longer mange to bring the window to front.
using win.index = 1 doesn't work. Maybe we should only consider
windows[0]. *)show_macos_jxa"chrome"~background~prefixjxauri{|
function is_equal (s0, s1) { return s0 === s1; }
function is_prefix (p, s) { return s && s.lastIndexOf (p, 0) === 0; }
function run(argv) {
var background = (argv[0] == 'true');
var pred = (argv[1] == 'true') ? is_prefix : is_equal;
var uri = argv[2];
var app = Application ('com.google.chrome');
if (!background) app.activate ();
for (var w = 0; w < app.windows.length; w++) {
var win = app.windows[w];
var tab = win.activeTab;
if (pred (uri, tab.url ())) { app.reload (tab); return; }
for (var t = 0; t < win.tabs.length; t++) {
tab = win.tabs[t];
if (pred (uri, tab.url ())) {
app.reload (tab); win.activeTabIndex = t + 1; return;
}
}
}
if (app.windows.length == 0) { app.Window().make(); }
if (app.windows[0].activeTab.url () === 'chrome://newtab/')
{ app.windows[0].activeTab.url = uri; }
else { app.windows[0].tabs.push(app.Tab ({ url : uri })); }
}|}letshow_macos_safari~background~prefixjxauri=(* win.index = 1 also (see chrome) doesn't work here and sadly opening
a directory file URI opens the finder. *)show_macos_jxa"safari"~background~prefixjxauri{|
function is_equal (s0, s1) { return s0 === s1; }
function is_prefix (p, s) { return s && s.lastIndexOf (p, 0) === 0; }
function run(argv) {
var background = (argv[0] == 'true');
var pred = (argv[1] == 'true') ? is_prefix : is_equal;
var uri = argv[2];
var app = Application ('com.apple.safari');
if (!background) app.activate ();
for (var w = 0; w < app.windows.length; w++) {
var win = app.windows[w];
var tab = win.currentTab;
if (pred (uri, tab.url ())) { tab.url = tab.url(); return; }
for (var t = 0; t < win.tabs.length; t++) {
tab = win.tabs[t];
if (pred (uri, tab.url ()))
{ tab.url = tab.url(); win.currentTab = tab; return; }
}
}
if (app.windows.length == 0) { app.Document().make(); }
if (app.windows[0].currentTab.url () === null)
{ app.windows[0].currentTab.url = uri; }
else { app.windows[0].tabs.push(app.Tab ({ url : uri }));
app.windows[0].currentTab =
app.windows[0].tabs[app.windows[0].tabs.length-1]; }
}|}letshow~background~prefixbrowseruri=Result.map_error(fune->Fmt.str"show uri %s: %s"urie)@@matchbrowserwith|None->Error"No browser found. Use the BROWSER env var to set one."|Someb->matchbwith|Cmdcmd->show_cmd~background~prefixcmduri|Macos_chromejxa->show_macos_chrome~background~prefixjxauri|Macos_safarijxa->show_macos_safari~background~prefixjxauri|Macos_open(open_tool,appid)->show_macos_open~background~prefixopen_tool~appiduri(* Cli interaction *)openCmdlinerletbrowser?docs?(opts=["browser"])()=letenv=Cmd.Env.infoEnv.browserinletdoc="The WWW browser command $(docv) to use. The value may be interpreted \
and massaged depending on the OS. On macOS: the names $(b,firefox), \
$(b,chrome) and $(b,safari) are interpreted specially; use $(b,open) \
if you would like to use $(b,open\\(2\\)); if absent the application that \
handles the $(b,http) URI scheme is used. On other platforms if
$(b,xdg-open\\(1\\)) is found in $(b,PATH) this is the program used by
default."inletabsent="OS dependent fallback"inletcmd=Arg.someB00_cli.cmdinArg.(value&optcmdNone&infoopts~absent~env~doc?docs~docv:"CMD")letprefix?docs?(opts=["prefix"])()=letdoc="Rather than the exact URI, reload if possible, the first browser tab \
which has the URI as a prefix. Platform and browser support for this \
feature is severly limited."inArg.(value&flag&infoopts~doc?docs)letbackground?docs?(opts=["background"])()=letdoc="Keep launched applications in the background. Platform support for \
this feature is limited."inArg.(value&flag&infoopts~doc?docs)(*---------------------------------------------------------------------------
Copyright (c) 2018 The b0 programmers
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
---------------------------------------------------------------------------*)