How to force a file to open via a bash script?

584 views Asked by At

Why can I not get this bash command to not work correctly?

As of now I am using this to open files in Sublime Text

open -a Sublime\ Text path/to/file_directory/file_name.php     (doesn't have to be php)

This works on most files but doesn't work on some files.

AKA:

  • .htaccess
  • some_file (without .txt, .php or any other ending to it)
  • some_file.txt
4

There are 4 answers

0
SaxDaddy On

From the OS X man page for open:

-t  Causes the file to be opened with the default text editor, as determined via LaunchServices

So all you need to do is add the following line to ~/.bashrc or ~/.profile

    EDITOR="/bin/local/subl -nw"

or if you have Sublime Text installed in the usual location,

    EDITOR="/Applications/Sublime\ Text.app/Contents/MacOS/Sublime\ Text -nw"

Then from a command line or script, you can run

    open -t file_name
1
Alex Cory On

Basically I am using the application Alfred to execute tasks to speed up my workflow.

I extended an alfred workflow that originally was supposed to show your recent downloads directory, but I changed this to show my developer notes directory. This is useful because then I can just open alfred with

⌘ space

and then it will type

dn

and it will pull in all the files from my Developer Notes directory. My problem was I wanted to just hit enter to open it correctly in Sublime Text. My stupid error was I didn't switch the script format from

/usr/bin/php    ==back to==>     /bin/bash

This is the reason why it wasn't working. Not any syntactical errors. Just a silly stupid overlooked error. I found it though! Just remember, if you're building alfred workflows, don't forget to switch the script filter language.

3
Michael Schlottke-Lakemper On

In OS X, each file type (identified by the file name extension, e.g. .txt or .php) has a default program associated with it, that is used to open files of this kind. It seems like you do not have any program associated with .txt or .htaccess files.

Here it is explained how to change file associations.

0
Saucier On

In your specific case just use

subl path/to/file

in order to open files with Sublime Text from the command line. Sublime Text provides a command line wrapper. Please have a look at the documentation for further details.