Skip to main content
Inspiring
February 5, 2013
Question

Get root directory/drive

  • February 5, 2013
  • 2 replies
  • 6088 views

I could have swore I saw this somewhere before, but I can't seem to find it now. 

Anyway, how does one go about getting the root drive/directory that the script itself is on regardless of which sub-folder it's in on said drive?  Obviously one could simply get the script's current path then deconstruct it, but I would think there is a more efficient method than this.  Any ideas?

This topic has been closed for replies.

2 replies

JJMack
Community Expert
Community Expert
February 6, 2013

I'm a hacker so I do a lot of cut and paste here is what I have seen and used. 

 

From Photoshop Javescripting guide for the user desktop:

"var nfile =  new File('~/Desktop/new.psd');" ie "~/Desktop/"

 

For temp file I saw this for OSx in forums on Photoshop scripting comunity web site and I used it in Windows XP and Win 7 and it seem to work:

"var URL = new File(Folder.temp + "/PhotoCollageToolkit.html");"

So Folder.temp seems to work on OS X and Windows I do not know why?

 

I would cut and paste  the above regular expression there beyond my comprehension

JJMack
Inspiring
February 6, 2013

Thats a static property of the Folder object… I think they were brought in with CS4… I use these all the time now… This gives me a string root to desktop without the tilde…

$.writeln( Folder.desktop.fsName );
// gives me /Users/marklarsen/Desktop
Inspiring
February 6, 2013

JJMack, Thanks, but I knew that '~' was a shortcut to the desktop that works on Mac or PC. And I knew some of the Folder class properties like temp, current, etc. I didn't know about Folder.desktop. But neither will let you find the root of a running script file.

Mark, thanks for the info. Also, it seems there was a reason I used fsName. The first RE I posted with file or folder paths using fsName. The second RE will fail using fullName on a folder( at least on Windows ). You should only need the decodeURI if there can be spaces in the root drive. If spaces are not allowed in Mac root drive names then you should be able to remove the \s in the RE and not need decodeURI.

Inspiring
February 5, 2013

Without getting the path of the script( if there is one ) I don't see a way to find the root folder. And it's not that hard to extract the root from the path.

dgolbergAuthor
Inspiring
February 5, 2013

Hmm, if I remember correctly, the one I saw before was something like:

var rootDir = $ENV('ROOT_DRIVE');

Obviously that's not correct or I wouldn't be here, but it was something along those lines if I'm remembering correctly.

 

Edit: Whoops, did a straight copy paste from what I was messing with in ESTK previously.  Fixed the example.

Inspiring
February 5, 2013

Maybe I misunderstood what you are trying to do. That looks like you are trying to access an environment variable. Something like $.getenv("HOMEDRIVE"));

 

That would be the system boot drive( at least on Windows, Mac may need a different string ).

 

But I wouldn't think the script path would be in the list. And the boot drive may not be the drive the script file is on. Again, it is possible that there isn't a filepath. An unsaved script in ESKT or a script run with BridgeTalk, $.evalFile(), or other eval methods will not have a path.