Comment by vbezhenar

2 years ago

Few additions.

open -n file.pdf : opens new instance of Preview application which is useful if you want to open the same file twice (for example to look at different pages at once).

caffeinate -d : prevents display turning off, useful if you want to look at display without moving mouse.

Another useful caffeinate tip is the `-w` option.

You can use it to pass a pid to keep the computer awake until that process completes. I use it for longer-running scripts that I don't want interrupted

I use this all the time:

open -a <GUI Application> <File>

Handy for distinguishing between editing and consuming media.

open -a is nice, i use it with alias, ex:

  alias qt='open -a "quicktime player"'
  alias vlc='open -a "vlc"'

  • At least for me (I've installed vlc via homebrew), there is a vlc binary in the PATH, and I can just vlc <filename>

    • The point here is to open a document with an app not assigned as default for the given mime-type by file name extension.

`caffeinate -disu` is the best combination (that is, enable all options): your laptop won't go to sleep, won't dim the screem etc.

open -a "Finder" . - open Finder in the current directory.

Standard apps usually just need the name, like Finder and Safari but you can also specify the path "/Applications/DifferentFinder.app"

  • Finder is pretty good, and it's handy to be able to open it from the terminal. But I find it super annoying it litters everything with .DS_Store files and there is no way to turn that off, except for external and network drives. Aside from, obviously, using a different file manager. Very un-Apple.

    • Well those files are to keep the view/presentation settings.

      I guess you could do that centrally with some sort of database but that would open another can of worms; and most importantly you wouldn't be able to transfer a folder and keep its Finder presentation intact.

      Nowadays it's not as useful because of the App Store but when software was only released as .dmg images, it became expected to open a nice layout with graphics presenting the app and a shortcut to the App folder that you would drag'n'drop the app bundle to.

      This presentation relies of .DS_Store to work.

      There are some other use cases like that, it all comes down to a simple fact: Apple has always cared a lot more about how things look than Microsoft ever did, this is a perfect example.

      2 replies →

    • The .DS_Store files are not Finder specific; Apple treats everything as a file (including folders), and it exists to supply folders and the files within them with metadata.

      It is just the first time the .DS_Store file is needed is often when the folder is touched by Finder.

    • .DS_Store is also a bug. I’m not sure why it hasn’t been fixed. It’s history is quite interesting but I don’t recall where I read about it.

      1 reply →

  • Don't forget about "open -b <bundle_id> foo.txt" for example,

        open -b com.sublimetext.4 file.txt
    

    I prefer this vs using the App Name or Path, since those can vary depending on the system or app version. The bundle ID is more robust.

  • you can just

        open .
    

    unless you've reconfigured something else to open directories, which most people haven't.

That's one of macOS pitfalls: the inability to open 2 instances of the same app, simply using the default GUI is annoying sometimes.

I think Windows is right in that matter...

Does `open` give focus? It used to, but since a few releases ago the app opens in the background, which is pretty annoying.

My poor workaround is to use osascript: `tell application "System Events" to set frontmost of process "Finder" to true`

  • Isn’t open opening apps in the background a consequence of having “secure keyboard entry” enabled in Terminal.app?

Also, `cd .` to open the current directory in a Finder window.

Not mine (found online years ago), but here's the opposite. `cd` into the frontmost Finder window:

  cd "$(osascript -e 'tell app "Finder" to POSIX path of (insertion location as alias)')";