What's the difference between 'open -a TextMate.app' and 'mate'? - textmate

I've thought that mate is virtually the same as 'open -a TextMate.app', but I guess I'm wrong in this.
As when I run the following command, when there's no hello2.txt, I get this error.
open -a TextMate.app hello2.txt
The file /Users/smcho/hello2.txt does not exist.
But, it's OK to run mate.
mate hello.txt --> opens the text mate.
What's the difference between the two?
I even tried
open -a TextMate.app --args hello2.txt
But this time, TextMate run with the file name 'Untitled', not 'hello2.txt'.
And this code opens the 'hello3.txt' without any problem.
[NSTask launchedTaskWithLaunchPath:#"/Applications/TextMate.app/Contents/MacOS/TextMate" arguments:[NSArray arrayWithObjects:#"hello3.txt", nil]];

open will open the given file with the default or a specific application.
open -a TextMate.app hello2.txt
means "Open the file hello2.txt using the application TextMate.app".
If there is no hello2.txt, there's nothing open could open, with or without TextMate.app, hence the error.
open -a TextMate.app --args hello2.txt
means "open nothing specific in the application TextMate.app (i.e. only open TextMate.app) and pass 'hello2.txt' as additional argument". This is a different kind of argument than the first example. TextMate.app can decide what to do with that additional argument. Apparently it chooses to ignore it.
mate is a utility optionally installed by TextMate.
mate hello.txt
means "I'd like to edit a file called hello.txt in TextMate", which is exactly what TextMate will let you do. It's a different utility with different behavior and different purpose, and it seems to better suite what you want it to do.

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.

Change directory in terminal using hyperlinks

Opening a gnome-terminal in the specified directory is straightforward:
gnome-terminal --working-directory ~/dotfiles
Creating hyperlinks is not a problem:
echo -e '\e]8;;file:///home/pmn/dotfiles\aThis is a link\e]8;;\a'
this produces a link that opens the file explorer in the correct folder when Ctrl+Clicked.
What I want to do is combine the two things, so that when I Ctrl+Click the link, a terminal pops up, already in the requested folder. It would not be a problem if the current terminal I'm in changed directory, but in that case I'd like to still see the previous terminal output and just do the equivalent of a regular cd (Edit: Note that the ~/dotfiles folder is just an example, I have several links that are generated by a script, and I'd like to be able to click on them to quickly open a terminal where needed).
I tried fiddling with registering custom applications:
In ~/.local/share/applications/mimeapps.list add:
(Note that the use of this file is deprecated, ~/.config/mimeapps.list should be used, if you have tips on doing that properly I'm open to them)
[Default Applications]
x-scheme-handler/mygnometerm=mygnometerm.desktop
In ~/.local/share/applications/mygnometerm.desktop add:
[Desktop Entry]
Type=Application
Terminal=false
Name=My Gnome Terminal
Exec=/bin/gnome-terminal --working-directory=%f
MimeType=x-scheme-handler/mygnometerm
Create the folder for the mime database:
mkdir -p ~/.local/share/mime/packages
Update the mime database:
update-mime-database ~/.local/share/mime
This does work: if I right-click on a folder in the file explorer and select My Gnome Terminal as the app to use, a terminal is opened already in the right folder.
If I print
echo -e '\e]8;;mygnometerm:///home/pmn/dotfiles\aThis is a link\e]8;;\a'
and click on the link, a terminal does pop up, but in the root folder, and the same happens when running
gio open mygnometerm:///home/pmn/dotfiles
I just need to combine everything, but I tried a lot of combination of what to echo in the link and could not find the right one.
I'm also open to other ways to achieve this behaviour, but at this point I'm quite curious in how to to it this way.
The use case, for the curious, is that I made a simple script to check the status of my repos, and I want to click on the ones with things to do without having to copy and paste the path. I reckon I'll save at least 5 seconds!
Cheers!

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

How to startup a Mac OS X application from command line?

"open -a" is not the answer wanted, because I want to debug the Mac OS X application automatically. This means it's better if someone can give the command line like [program] [args] format. So ltrace mechanism can make [program] as target for debugging and take [args] as input.
I have tried command line like "/Applications/Microsoft Office 2011/Microsoft PowerPoint.app/Contents/MacOS/Microsoft PowerPoint" /Users/poc.pptx, only Microsoft Point process started but the poc.pptx not opened.
After grepping the Microsoft Point with pptx file opened, it's something like: /Applications/Microsoft Office 2011/Microsoft PowerPoint.app/Contents/MacOS/Microsoft PowerPoint -psn_0_307275, there is no argument "poc.pptx".
I even manually use "gdb /Applications/Microsoft Office 2011/Microsoft PowerPoint.app/Contents/MacOS/Microsoft PowerPoint" and "set args /Users/poc.pptx", and then "r", the target application can not run with the certain file opened.
I am confused about this, so, is there someone can help me to solve this problem?
Thank you!
open -b com.microsoft.PowerPoint <filename> seems to work for me to open presentations from the command line.
Go to file directory and then type
open -a "Microsoft PowerPoint" <filename.ppt>
Here "Microsoft PowerPoint" is the name of power point application, please check name of power point if it is different in your application directory.
This is working perfectly fine on my MAC (OSX 10.8).
We can also give complete path instead of just file name.
open -a "Microsoft PowerPoint" <ppt file path>
This is also working fine.
I know this is a old question, but here is my 2ct anyway.
I add the applications I want to open through command line in /usr/local/bin as a symlink.
I never run into any problems, but as Ken stated it depends how a application handles arguments.
Example with Visual Studio:
First I check what makes the application start bij executing the file inside the App contents like:
$ /Applications/Visual\ Studio\ Code.app/Contents/MacOS/Electron
If that works, then I create the symlink as follows (ln -s <path-to-app> <path-to-symlink>):
$ ln -s /Applications/Visual\ Studio\ Code.app/Contents/MacOS/Electron /usr/local/bin/vs
After that I can start up Visual Studio with the current folder loaded as:
~/Development/SomeProject $ vs .
If PowerPoint is not opening a document passed as a command-line argument, then that's a reflection on how PowerPoint was coded. There's nothing anybody but Microsoft can do about that.
The OS does not normally use that technique to tell applications to open documents. Instead, it passes Apple Events to the application. Cocoa will, by default, accept command-line arguments and treat them similarly to such Apple Events, but apparently PowerPoint is overriding that default behavior.
If you want to debug or trace PowerPoint, I recommend that you do it in two steps. First, launch it without arguments under the debugger or trace program. Then, tell it to open a document. You can do that in the normal way, using the Finder and/or Dock, or you can use open -a .... Such a request to open a document will not launch a second instance of PowerPoint, it will deliver an event to the already-running PowerPoint which you are debugging/tracing. So, the result should be similar to what you seem to want.
Not sure if this will help you (depends on how you want to do your debugging), but you can use AppleScript from the command line, like this:
%osascript <<<EOD
tell application "Excel" to open "Users:xxx:Documents:sheet.xls"
EOD
When entered this way, your script can contain several lines, it does not have to be limited to a single one.

How to launch an app on OS X with command line - The best way

I want to launch an app on OSX from a script. I need to pass some command line arguments. Unfortunately, open doesn't accept command line args.
The only option I can think of is to use nohup myApp > /dev/null & to launch my app so it can exist independently of the script that launches it.
Any better suggestions?
As was mentioned in the question here, the open command in 10.6 now has an args flag, so you can call:
open -n ./AppName.app --args -AppCommandLineArg
In OS X 10.6, the open command was enhanced to allow passing of arguments to the application:
open ./AppName.app --args -AppCommandLineArg
But for older versions of Mac OS X, and because app bundles aren't designed to be passed command line arguments, the conventional mechanism is to use Apple Events for files like here for Cocoa apps or here for Carbon apps. You could also probably do something kludgey by passing parameters in using environment variables.
An application bundle (.app file) is actually a directory. Instead of using open and the .app filename, you can move into the app's directory and start the actual machine code program located inside. For instance:
$ cd /Applications/LittleSnapper.app/
$ ls
Contents
$ cd Contents/MacOS/
$ ./LittleSnapper
That is the actual binary executable that might accept arguments (or not, in LittleSnapper's case).
In case your app needs to work on files (what you would normally expect to pass as: ./myApp *.jpg), you would do it like this:
open *.jpg -a myApp
You can launch apps using open:
open -a APP_YOU_WANT
This should open the application that you want.
open also has an -a flag, that you can use to open up an app from within the Applications folder by it's name (or by bundle identifier with -b flag). You can combine this with the --args option to achieve the result you want:
open -a APP_NAME --args ARGS
To open up a video in VLC player that should scale with a factor 2x and loop you would for example exectute:
open -a VLC --args -L --fullscreen
Note that I could not get the output of the commands to the terminal. (although I didn't try anything to resolve that)
I would recommend the technique that MathieuK offers. In my case, I needed to try it with Chromium:
> Chromium.app/Contents/MacOS/Chromium --enable-remote-fonts
I realize this doesn't solve the OP's problem, but hopefully it saves someone else's time. :)
Lots of complex answers when you can simply access Applications folder and type:
open -a [APP NAME]
This is it!
I wanted to have two separate instances of Chrome running, each using its own profile. I wanted to be able to start them from Spotlight, as is my habit for starting Mac apps. In other words, I needed two regular Mac applications, regChrome for normal browsing and altChrome to use the special profile, to be easily started by keying ⌘-space to bring up Spotlight, then 'reg' or 'alt', then Enter.
I suppose the brute-force way to accomplish the above goal would be to make two copies of the Google Chrome application bundle under the respective names. But that's ugly and complicates updating.
What I ended up with was two AppleScript applications containing two commands each. Here is the one for altChrome:
do shell script "cd /Applications/Google\\ Chrome.app/Contents/Resources/; rm app.icns; ln /Users/garbuck/local/chromeLaunchers/Chrome-swirl.icns app.icns"
do shell script "/Applications/Google\\ Chrome.app/Contents/MacOS/Google\\ Chrome --user-data-dir=/Users/garbuck/altChrome >/dev/null 2>&1 &"
The second line starts Chrome with the alternate profile (the --user-data-dir parameter).
The first line is an unsuccessful attempt to give the two applications distinct icons. Initially, it appears to work fine. However, sooner or later, Chrome rereads its icon file and gets the one corresponding to whichever of the two apps was started last, resulting in two running applications with the same icon. But I haven't bothered to try to fix it — I keep the two browsers on separate desktops, and navigating between them hasn't been a problem.
Beginning with OS X Yosemite, we can now use AppleScript and Automator to automate complex tasks. JavaScript for automation can now be used as the scripting language.
This page gives a good example example script that can be written at the command line using bash and osascript interactive mode. It opens a Safari tab and navigates to example.com.
http://developer.telerik.com/featured/javascript-os-x-automation-example/
osascript -l JavaScript -i
Safari = Application("Safari");
window = Safari.windows[0];
window.name();
tab = Safari.Tab({url:"http://www.example.com"});
window.tabs.push(tab);
window.currentTab = tab;
Simple, here replace the "APP" by name of the app you want to launch.
export APP_HOME=/Applications/APP.app/Contents/MacOS
export PATH=$PATH:$APP_HOME
Thanks me later.
With applescript:
tell application "Firefox" to activate
Why not just set add path to to the bin of the app. For MacVim, I did the following.
export PATH=/Applications/MacVim.app/Contents/bin:$PATH
An alias, is another option I tried.
alias mvim='/Applications/MacVim.app/Contents/bin/mvim'
alias gvim=mvim
With the export PATH I can call all of the commands in the app. Arguments passed well for my test with MacVim. Whereas the alias, I had to alias each command in the bin.
mvim README.txt
gvim Anotherfile.txt
Enjoy the power of alias and PATH. However, you do need to monitor changes when the OS is upgraded.
To Create a New Text File OR open an existing one, in any folder, using a Text/Code Editor like the Free TextMate app on MACOSX, use this command on Terminal:
open -n /Applications/TextMate.app --args "$PWD/some file.txt"
Instead of a Text File, you can use any file type, based on your app's requirements and its support for this syntax.
This command also simulates the New Text Document Here Command on Windows and has been tested on MacBook Pro 2021 and Monterey 12.2.1 successfully.

Resources