Cole Tierney

Q: command line utility to find standard directories

I'm looking for a command line utility that can print standard directories like:

/Library/Application Support

In objective-c I would use:

NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSLocalDomainMask, YES)

Posted on Dec 13, 2012 12:48 PM

Close

Q: command line utility to find standard directories

  • All replies
  • Helpful answers

Page 1 Next
  • by Linc Davis,

    Linc Davis Linc Davis Dec 13, 2012 1:41 PM in response to Cole Tierney
    Level 10 (207,926 points)
    Applications
    Dec 13, 2012 1:41 PM in response to Cole Tierney

    find . -type d

  • by Cole Tierney,

    Cole Tierney Cole Tierney Dec 13, 2012 4:15 PM in response to Linc Davis
    Level 4 (1,375 points)
    Dec 13, 2012 4:15 PM in response to Linc Davis

    Thanks, but I mean common directory locations like the user's application support folder, the global application support folder, user's documents folder, etc. The NSSearchPathForDirectoriesInDomains method lets you ask the system for these locations. I understand these paths are easy to predict; just wondering if there's an oficial way to do this via the command line.

  • by Linc Davis,Helpful

    Linc Davis Linc Davis Dec 13, 2012 4:45 PM in response to Cole Tierney
    Level 10 (207,926 points)
    Applications
    Dec 13, 2012 4:45 PM in response to Cole Tierney

    Not that I know of, but if the solution doesn't need to be portable, you could install F-Script, which is a POSIX interface to the Cocoa API.

  • by Cole Tierney,Helpful

    Cole Tierney Cole Tierney Dec 13, 2012 4:56 PM in response to Linc Davis
    Level 4 (1,375 points)
    Dec 13, 2012 4:56 PM in response to Linc Davis

    It does need to be portable, but thanks for the suggestion.

    Edit: F-Script looks really interesting though.

     

    Message was edited by: Cole Tierney

  • by etresoft,Solvedanswer

    etresoft etresoft Dec 13, 2012 5:02 PM in response to Cole Tierney
    Level 7 (29,056 points)
    Dec 13, 2012 5:02 PM in response to Cole Tierney

    I am unaware of any variability on anything other than HOME and TMP and those have environment variables. Usually a command-line tool wouldn't be poking around in Library or Documents. It seems like it would be easy enough to write such a tool if you needed it.

  • by Cole Tierney,

    Cole Tierney Cole Tierney Dec 13, 2012 6:15 PM in response to etresoft
    Level 4 (1,375 points)
    Dec 13, 2012 6:15 PM in response to etresoft

    That's a good idea. I could make a command line app with the objective-c source I've got. Too bad there's no low hanging fruit, though. :)

  • by etresoft,

    etresoft etresoft Dec 13, 2012 7:09 PM in response to Cole Tierney
    Level 7 (29,056 points)
    Dec 13, 2012 7:09 PM in response to Cole Tierney

    In what context would you want to use it? The only place anyone needs those things are in installer packages and those paths are always hard-coded.

  • by Frank Caggiano,

    Frank Caggiano Frank Caggiano Dec 13, 2012 7:14 PM in response to Cole Tierney
    Level 7 (25,782 points)
    Dec 13, 2012 7:14 PM in response to Cole Tierney

    How does this need to be portable? Those paths only have special meaning to OS X. Most won't even exist on any other system.

  • by Cole Tierney,

    Cole Tierney Cole Tierney Dec 14, 2012 5:47 AM in response to etresoft
    Level 4 (1,375 points)
    Dec 14, 2012 5:47 AM in response to etresoft

    I have a cocoa app that unzips files in /Library/Application\ Support/OurCompanyName. A non-cocoa app then uses these files. The cocoa app gets the global app support folder using:

    NSString *appsDataDir = [
         NSSearchPathForDirectoriesInDomains(
              NSApplicationSupportDirectory,
              NSLocalDomainMask,
              YES
         ) lastObject
    ];

    The non-cocoa app uses a hard-coded version of this string. But it does have access to bash via a third party plugin. So I thought it would be nice if both apps used the same method to find the path incase it ever changes. There is an sdk for these plugins, but it's written in C++ and I don't know how to use Objective-C in a C++ framework.

  • by Cole Tierney,

    Cole Tierney Cole Tierney Dec 14, 2012 6:20 AM in response to Frank Caggiano
    Level 4 (1,375 points)
    Dec 14, 2012 6:20 AM in response to Frank Caggiano

    What I mean by portable is that it needs to run on macs that I don't have any control over. That said I do have a Windows version of the app that uses a similar strategy to find common paths by asking the OS. Occasionally school systems will have their computers configured to save data in non standard locations like a central server which is why I'm trying to avoid hardwiring paths in general.

  • by twtwtw,

    twtwtw twtwtw Dec 14, 2012 7:58 AM in response to Cole Tierney
    Level 5 (4,935 points)
    Dec 14, 2012 7:58 AM in response to Cole Tierney

    if that's all you need, just use this:

     

    osascript -e "posix path of (path to application support folder from user domain)"

     

    note that the result you want will be in the last line, but oascript may throw in some warnings first if you have third party osax installed.  you'll probably want to use grep or extract the last line.

  • by Cole Tierney,

    Cole Tierney Cole Tierney Dec 14, 2012 8:06 AM in response to twtwtw
    Level 4 (1,375 points)
    Dec 14, 2012 8:06 AM in response to twtwtw

    Thanks! Using applescript didn't occur to me.

  • by etresoft,

    etresoft etresoft Dec 14, 2012 8:34 AM in response to Cole Tierney
    Level 7 (29,056 points)
    Dec 14, 2012 8:34 AM in response to Cole Tierney

    Cole Tierney wrote:

     

    I thought it would be nice if both apps used the same method to find the path incase it ever changes.

     

     

    I think the only significant part of that is the domain. That is variable from user to System to root ($HOME, /System, or /, respectively). The relative paths from there can't change. Installers consist of archives with these paths already defined. A properly designed installer can pick a user or root domain, but then it just thows in a pre-made directory tree without overwriting any directories already there.

     

    There is an sdk for these plugins, but it's written in C++ and I don't know how to use Objective-C in a C++ framework.

     

    Just write it as a function in a .mm file and call it from C++. You could define it as a C function with "extern C" as well.

  • by twtwtw,

    twtwtw twtwtw Dec 14, 2012 8:40 AM in response to etresoft
    Level 5 (4,935 points)
    Dec 14, 2012 8:40 AM in response to etresoft

    etresoft wrote:

     

    I think the only significant part of that is the domain. ...

     

    That's generally true, but not 100%.  Every once in a while (usually at major OS releases) Apple moves a standard directory.  If I remember correctly they've done that with the Scripts folder in the 10.7 to 10.8 shift.  The path to command will always reflect the proper path; hard coded paths (even within a domain) might not.

Page 1 Next