2

I recently discovered that I can open application from Terminal like this:

$ open -a TextEdit

Is there any command that would just show the application folder instead of opening it? Such that I could do this:

$ alias vmrun=\"`some-command 'VMware Fusion'`/Contents/Library/vmrun\"

I need that some-command to print the folder of VMware Fusion application.

UPDATE: I don't know where the application is installed, otherwise I could just hardcode it in the script

  • What's the bigger picture of your script? As far as I understand it vmrun entered in Terminal should control an arbitrary VM and VMware Fusion or even several different versions are installed in arbitrary folders (which usually isn't the case because the default installation folder is /Applications/). – klanomath Sep 1 '15 at 7:52
  • @tair Do you want to hardcode it? i.e.. Are you more concerned with finding <some-command> or identifying the true path of /VMware\ Fusion.app/? – voices Sep 1 '15 at 8:28
  • @tjt263 I want it to be script-friendly and maintainable – Tair Sep 1 '15 at 8:54
  • @tair that doesn't really answer my question.. do you just want to find the directory where the *.app resides so you can hardcode it? or would you prefer something dynamic like a command / variable? – voices Sep 1 '15 at 12:39
  • Ok, I see your point. The script should be portable from mac to mac, IOW should not depend on a path of application on a particular mac – Tair Sep 1 '15 at 12:59
2

I believe this is what you're looking for:

alias vmrun=\"`osascript -e 'tell application "Finder" to get POSIX path of (application file id "com.vmware.fusion" as alias)'`/Contents/Library/vmrun\"

Now when I type vmrun, without any arguments, in a Terminal and press Enter it outputs the internal help file, as it should.

This works on my system, however it returns the first occurrence of the "VMware Fusion.app" application bundle's path. I have four different visions installed, so this would not be ideal in my situation, although for those that only have one version installed it works.

  • Regarding 'first occurence' -- at least consistent with `open -a my.app'. – Tair Sep 1 '15 at 7:09
  • Nice. So is that AppleScript? I've never actually used it before.. or seen anything quite like it.. +1 – voices Sep 3 '15 at 3:38
  • @tjt263, Yes, part of that is, from the CLI or in a Shell Script, not necessarily the normal Apple Script that is done in the Script Editor (previously named AppleScript Editor). In other words running AppleScript from the CLI or in a Shell Script it uses osascript to process the code. From the Editor things are a bit different and there is plenty of AppleScript on the Internet if you what to see what it looks like when run in the Editor or even AppleScript added to Automator. – user3439894 Sep 3 '15 at 4:28
  • @user3439894 yeah cool so running osascript is sort of like running python then, for instance? – voices Sep 3 '15 at 5:39
  • @tjt263 exactly. But I personally don't like it too much, because everytime I try to do something with AppleScript I struggle with lack of documentation – Tair Sep 14 '15 at 5:02
3

I have just created and uploaded a GitHub Gist GitHub Gist which may help you out.

The main functionality is:

function get_apps_folder () { 
    mdfind -0 -onlyin / \
        'kMDItemKind=="Application" && kMDItemDisplayName="'"${1:-TextEdit}"'"' \
    | xargs -0 -I{} dirname {} 
}

function open_apps_folder () {
    open $(get_apps_folder "$1")
}

You could take that and add the two functions to your ~/.bash_profile and they would be available for any interactive shell sessions you are using. (And yes, getting around mixed and embedded single and double quotes can be messy.) :-)

But, to give a better explanation, the main part is:

mdfind -0 -onlyin / 'kMDItemKind=="Application" && kMDItemDisplayName="VMware Fusion"' | xargs -0 -I{} open {}

The -onlyin /path limits the search to just the root volume, otherwise any “Spotlight-searchable” volume attached will most likely be searched as well, including Time Machine, which can get unwieldy and/or verbose, hence using -onlyin /.

In the Gist, each line output by mdfind is fed to xargs which calls open on what is passed to it. Note that there is no particularly robust checking of what is passed to xargs (ie. it is assuming it is getting a directory).

mdfind and mdls can be pretty handy for things like this, although they can take a little getting used to. I usually use mdls /path/to/file to get an idea of what metadata that type of file has. I can then take those keys and values to search using mdfind. You can find a lot of pages, examples and documentation out there, BTW.

  • While shell functions are nice, and I originally though he'd have to use a function over an alias (conversion since been deleted), nonetheless I believe my solution is in the format that tair expressed he wanted. Also, he is not wanting to open the VMware Fusion application via a function, he's wants to set an alias to the vmrun executable which is not the same as the VMware Fusion executable. – user3439894 Aug 31 '15 at 23:22
  • La di da and too-rye-ay. – user76598 Sep 1 '15 at 0:03
  • @jps3 Interesting. I think I like this answer the best, even though I'm not entirely sure that I fully understand it. – voices Sep 2 '15 at 19:38
2

Just type:

mdfind <target_file_name.ext>

If there are any matches, you will be presented with the relevant associated path(s). If there's more than one, choose whichever is appropriate and hardcode it.


For example:

me@MacBook-Pro:~$mdfind "VMware Fusion.app"
/opt/homebrew-cask/Caskroom/vmware-fusion/7.1.2-2779224/VMware Fusion.app

Or more likely, one of these:

you@MacBook-Pro:~$mdfind "VMware Fusion.app"
/Applications/VMware Fusion.app
/Users/you/Applications/VMware Fusion.app

1

Two quick and easy options:

  1. You can use Spotlight (mdfind) via command-line for high level items:

    appLocation=$(mdfind Fusion.app)
    echo $appLocation
    /Applications/VMware Fusion.app
    
  2. Or if you use the locate binary (pre-requisite command to enable it will be noticed first time you use it).

    appLocation=$(locate vmrun)
    echo "Found at " $appLocation
    Found at /Applications/VMware Fusion.app/Contents/Library/vmrun
    
  • If that first example works for you; try: 'open $"(mdfind Fusion.app)"' – voices Sep 14 '15 at 8:38
  • Or: '$"(mdfind Fusion.app)"/Contents/Library/vmrun' – voices Sep 14 '15 at 8:43
0

You have to add:

alias vmrun='/Applications/VMware\ Fusion.app/Contents/Library/vmrun'

to your ~/.bash_profile, save the file and source ~/.bash_profile. In the future you just have to type vmrun to launch the tool.

If you need quick access to any more commands from this directory you can add:

:/Applications/VMware\ Fusion.app/Contents/Library/

to your path.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .