1

There is this solved topic about opening .html files via command line.

I use the solution and it works well for using

open ./myfile.html

However, it opens the file always in a new tab. I would like to open it always in the same tab (using the browser target). This is an easy thing to do in JavaScript, but I can't figure out a way to do it in combination with the above mentioned code.

My assumption for now is, that there must be a way to pass the target as a parameter to the open command. The man open reveals the following for the parameter --args:

All remaining arguments are passed to the opened application in the argv parameter to main(). These arguments are not opened or interpreted by the open tool.

So I tried the following:

open ./myfile.html --args target=myfile_target   # still opens in new tab
open ./myfile.html --args target="myfile_target" # still opens in new tab
open ./myfile.html --args target:myfile_target   # still opens in new tab

I am not sure if this even works but I think there must be a way to do this.

Edit: for now it is enough to make this work with chrome.

Jankapunkt
  • 8,128
  • 4
  • 30
  • 59
  • You could do it with AppleScript reasonably easily. Let me know if you want me to draft something up for this purpose. – CJK Jan 25 '18 at 00:03
  • Thank you @CJK Of course I would prefer a solution that uses only shell commands, so I can have the future option to port this script easily to other OS (BSD, Linux etc.). However, since that goal might not be realistic I would stick with your solution for now, if it is feasible. Note, that I have zero applescript experience yet. Thank you. – Jankapunkt Jan 25 '18 at 08:24
  • 1
    Sorry it's taken me a while to get back to you. The task was slightly more challenging than I had anticipated (because my Bash scripting isn't great), and I forgot to star this question, so spent ages tracking it down again. I've added a solution in the form of a bash script as we discussed. If you encounter any errors, let me know. – CJK Feb 07 '18 at 02:31
  • Thank you for the help. I will check it out as soon as I am back on the mac. – Jankapunkt Feb 07 '18 at 08:38

1 Answers1

3

This Bash script incorporates a bit of AppleScript in order to open a browser window with a reference that the script can keep track of and continue to target with ongoing URL requests.

You ought to be able to copy-n-paste it into a text editor and save it as whatever you wish to call this replacement open function. I saved it as url, in one of the directories listed in my $PATH variable. That way, I could simply type url dropbox.com from the command-line, and it would run.

You will have to make it executable before you can do that. So, after it's saved, run this command:

chmod +x /path/to/file

Then you should be good to go. Let me know if you encounter any errors, and I'll fix them.

    #!/bin/bash
    #
    # Usage: url %file% | %url%
    #
    # %file%: relative or absolute POSIX path to a local .html file
    # %url%: [http[s]://]domain[/path/etc...]

    IFS=''

    # Determine whether argument is file or web address
    [[ -f "$1" ]] && \
        _URL=file://$( cd "$( dirname "$1" )"; pwd )/$( basename "$1" ) || \
            { [[ $1 == http* ]] && _URL=$1 || _URL=http://$1; };

    # Read the value on the last line of this script
    _W=$( tail -n 1 "$0" )

    # Open a Safari window and store its AppleScript object id reference
    _W=$( osascript \
        -e "use S : app \"safari\"" \
        -e "try" \
        -e "    set S's window id $_W's document's url to \"$_URL\"" \
        -e "    return $_W" \
        -e "on error" \
        -e "    S's (make new document with properties {url:\"$_URL\"})" \
        -e "    return id of S's front window" \
        -e "end try" )

    _THIS=$( sed \$d "$0" ) # All but the last line of this script
    echo "$_THIS" > "$0"    # Overwrite this file
    echo -n "$_W" >> "$0"   # Appened the object id value as final line

    exit
    2934
CJK
  • 5,732
  • 1
  • 8
  • 26
  • Works like a breeze! (macOS 10.13, safari 11.1) And with a self-assignable code... May be more traditional/universal approach could be used but it is creative like that. – Ek1noX Sep 14 '18 at 07:54