Search code examples
macosshellgoogle-chromebrowser-tab

Set target when open .html file via terminal on MacOS


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.


Solution

  • 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