Open chrome from osx terminal with specific named tab target - macos

I have implemented a file watcher that whenever a file is saved from a given watched folder it will open google chrome from the terminal like this /usr/bin/open -a "/Applications/Google Chrome.app" '${url}'
The problem is that If I save the file two times or more than one file, it will open many tabs in my chrome instance with the same url.
What I want to do is for it to refresh it instead. Similar as what would happen when you do this will open in somenamedtab
Is there any way to run /usr/bin/open -a "/Applications/Google Chrome.app" '${url}' but specifying which named tab?

Related

Katacoda Scenarios not recognizing open command in the terminal

I am learning to create my own scenario in katacoda. I want to open a file that I created in the katacoda editor, but it's not letting me use the open command in my background.sh file. This is what I have in it so far:
touch my-project/new.py
echo "print('Hello World')" >> my-project/new.py
open new.py
This creates the python file, but it does not open it. I tried running open new.py in the terminal, but it gives me an error
bash: open: command not found
I can click on it in the file tree to open it, but I want it to open automatically. What other command can I use to open it in the katacoda editor?
After messaging support, you are not able to put code in a file that is opened on start in Katacoda. You can embed links that will add code when the user clicks on it, but that's a different task. Katacoda does not support the "open" command, but you can use vim or nano.

Opening Chrome settings page in Terminal

I'd like to use Terminal on Mac's open command to open a link to a page in Chrome's settings. Currently, I'm doing something like this:
alias chrome_settings='open -a "Google Chrome" chrome://settings/'
But I get a message stating The file /Users/codes/chrome:/settings does not exist.
How is it being interpreted as a file, when I intend for it to be opened as a web page? I've also tried passing it in as an argument, using open -a "Google Chrome" --args chrome://settings/, but this doesn't seem to work as well.

Opening Finder from terminal with file selected

I want to open Finder from the terminal with a specific file selected. I know that by using open . I can open the current directory in Finder, but I also want to select some file in the Finder window.
The basic thing I want to do is run a script that randomly selects a file among many in a folder and for that I need to open a new Finder window with the file selected.
The . in your open . command just means path at current location (which would be a folder) so open decides that the correct application to use is Finder. If you were to do open myTextFile.txt which is at your current location in the terminal open will decide to use a text editor instead. You can however specify the application to open the file with by using the -a flag so your command would look like this: open -a Finder myTextFile.txt.
What Faisal suggested will also work, the -R flag is an equivalent to using ⌘↩ (Command Return) in Spotlight.
this and some other nice shell tricks with the open command are described in this post: Shell tricks: the OS X open command
For me, code below works fine.
open -R your-file-path
You can do it like that
osascript -e "tell application \"Finder\"" -e activate -e "reveal POSIX file \"<your file path>\"" -e end tell

OSX Terminal - Change Application Focus

Is there a way using command line to change the focus to a specific application? If so, do I need the process id or what?
My goal is to run a process that compiles files, and then focuses on the browser window once the files have finished compiling.
If I understand you correctly, this could be done quite easily with open -a. Opening an already-open application brings it to the forefront. The -a option allows you to specify the application you want, so pick your favorite:
open -a Google\ Chrome
open -a Safari
open -a Firefox
Another option is osascript -e 'activate application "Google Chrome"'.

bash open .html file not in browser but with $editor

I want to open all of my html/css/js etc. not in the Browser by typing open example.html, but with my default Editor. I did create the alias EDITOR already, but it does not work with every kind of file.
How to fix that?
Depending on the actual behavior you're looking for, you will probably want one of the following, from the open man page:
-e Causes the file to be opened with /Applications/TextEdit
-t Causes the file to be opened with the default text editor, as deter-
mined via LaunchServices
-f Reads input from standard input and opens the results in the default
text editor. End input by sending EOF character (type Control-D).
Also useful for piping output to open and having it open in the
default text editor.
-W Causes open to wait until the applications it opens (or that were
already open) have exited. Use with the -n flag to allow open to
function as an appropriate app for the $EDITOR environment variable.
Try reading the man page: man open
I see three options:
-a
-a application
Specifies the application to use for opening the file
open -a SomeEditor file.html
-e
Causes the file to be opened with /Applications/TextEdit
open -e file.html
-t
Causes the file to be opened with the default text editor, as determined via LaunchServices
open -t file.html
If you use TextMate or Sublime Text, you can use mate or subl. You can create aliases like this for other editors:
alias wrang="open -a TextWrangler"
open -t opens files in the default application for public.plain-text files. You can change it by adding a line like this to a duti configuration file:
com.macromates.TextMate.preview public.plain-text all

Resources