Welcome Dialog

Welcome to the Community!

We have a brand new look! Take a tour with us and explore the latest updates on Adobe Support Community.


Get root directory/drive

Contributor ,
Feb 05, 2013 Feb 05, 2013

Copy link to clipboard

Copied

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?

TOPICS
Actions and scripting

Views

3.5K

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Feb 05, 2013 Feb 05, 2013

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Feb 05, 2013 Feb 05, 2013

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Feb 05, 2013 Feb 05, 2013

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Feb 05, 2013 Feb 05, 2013

Copy link to clipboard

Copied

Hmm, sounds like it's a case of us both making it more complicated than it is... Basically, I'm looking for the JavaScript equivalent of Java's

 

File dir = new File("/").getCanonicalFile();

 

which is the same as the script asking "What drive am I on?".  You did nail the $.getenv("HOMEDRIVE"); on the head though.  That was what I had seen previously, but it obviously only works on windows and only returns the OS drive.  I must have mistaken my previous attempts to get the script's root drive with that method (before I knew better), so there probably isn't a simple solution for getting the script's root drive other than backtracking through the full path.  This leads back to the original question.  Using the script's current location; how can I get the script's current drive on both Windows and Mac?  I'm not a Mac user, but I do know their drives are labelled differently; instead being called something like "volumeX".

 

TLDR: How do I get a script to find the drive that it's currently on regardless of OS?

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Feb 05, 2013 Feb 05, 2013

Copy link to clipboard

Copied

var Mac = new File("/Users/USERNAME/Dir/tmpDir/file.psd").fsName;
var Win = new File("/C/Documents and Settings/USERNAME/Dir/desktop/file.psd").fsName;
alert(Mac.match(/^[\/\w]?[\/\w\s]*?[:\/]/).toString().replace(/[:\/]/g,''));
alert(Win.match(/^[\/\w]?[\/\w\s]*?[:\/]/).toString().replace(/[:\/]/g,''));

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Feb 05, 2013 Feb 05, 2013

Copy link to clipboard

Copied

Awesome; thanks Michael... One of these days I'll have to take some time to better learn regex.  There's still a lot of it that's confusing to me.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Feb 06, 2013 Feb 06, 2013

Copy link to clipboard

Copied

The regexp could be a little simpler if you used fullName instead of fsName. And it may be possible to find a better/shorter expression. It also looks old. From before I knew how ExtendScript handled paths. And it didn't really answer your question fully. So...

 

alert(decodeURI(new File($.fileName)).match(/^[\/\w]?[\/\w\s]*?[:\/]/).toString().replace(/[:\/]/g ,''));

 

But that will fail it the file is on the desktop. I don't know how to force ExtendScript to show the full, file system path for the desktop. Every way seems to use the '~' shortcut.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Feb 06, 2013 Feb 06, 2013

Copy link to clipboard

Copied

Might as well add another way ....

alert(File($.fileName).fsName.toString().split('\\')[0].replace(/:$/,''));

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Feb 06, 2013 Feb 06, 2013

Copy link to clipboard

Copied

Paul, does that work on Mac? It looks like you are splitting on backslash and I didn't think Mac used backslashes in paths.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Feb 06, 2013 Feb 06, 2013

Copy link to clipboard

Copied

Haven't tried on a Mac, only dust one off if I have to. So you could be right and for the Mac it would have to split on forward slash.

 

 

I wonder if this would work for both?

alert(File($.fileName).fsName.toString().split(/\\|\//)[0].replace(/:$/,''));

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Feb 06, 2013 Feb 06, 2013

Copy link to clipboard

Copied

JJMack, $.fileName will give you the path of a running script without the need to force an error. But it still doesn't help finding the root of that path.

 

Paul, not sure that would work Mac starts with a forward slash so the first element would be empty? And there still the script file on a desktop issue.

 

I think you would do something like if the first char is '~' use Folder.desktop.fsName else use File($.fileName).fsName and run the RE.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Feb 06, 2013 Feb 06, 2013

Copy link to clipboard

Copied

I wonder if there is an OS problem as it does return the correct drive letter from a script run from the desktop with Windows 7

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Feb 06, 2013 Feb 06, 2013

Copy link to clipboard

Copied

I am not clear on what is returning the full path from an script running on the desktop on Win 7. Is it File($.fileName).fsName. And it may be a version instead of OS issue. On Windows & and CS5 I still can't get File($.fileName) to return the full path for the desktop.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Feb 06, 2013 Feb 06, 2013

Copy link to clipboard

Copied

With Windows 7 alert(File($.fileName).fsName); returns the full path.

Edit:- I am using Photoshop CS6

Another edit: Photoshop CS5 is ok as well, so it look as if it an OS problem.

 

This works with Windows 7 and on a Mac ( I dusted the laptop and it stll works)

alert(File($.fileName).fsName.toString().split(/\\|\//,2).toString().replace(/(\/)|(,)|(:.+)/g,''));

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Feb 06, 2013 Feb 06, 2013

Copy link to clipboard

Copied

Wow, I didn't think this was going to strike up such an extensive, yet helpful string of posts... there's not enough "Was this helpful?" or "correct answers" to give out... very helpful posts from everyone though!  Thanks guys!

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Nov 14, 2021 Nov 14, 2021

Copy link to clipboard

Copied

LATEST

Yikes! I had no idea split method can use second argument for number of results!

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Feb 06, 2013 Feb 06, 2013

Copy link to clipboard

Copied

Just tried with XP Pro and alert(File($.fileName).fsName); returns the full path.

Tested with Photoshop CS3 and CS6.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Feb 06, 2013 Feb 06, 2013

Copy link to clipboard

Copied

Paul, guess its my turn to say I thought it wasn't working but it does. I don't know why but I would swear when I tested alert(File($.fileName).fsName); last night it didn't return the full path.

 

dgolberg, if you would please switch the corrent to Paul's post( #19 ) so others will not have to read a long thread to find it.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Feb 06, 2013 Feb 06, 2013

Copy link to clipboard

Copied

Strange things these computers! they will never catch on.

 

For completeness, Photoshop versions before CS3 would need something like...

scriptName = function() {try {var err=errorr;}catch(e){return File(e.fileName);}}
alert(scriptName().fsName.toString().split(/\\|\//,2).toString().replace(/(\/)|(,)|(:.+)/g,''));

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Feb 06, 2013 Feb 06, 2013

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Feb 06, 2013 Feb 06, 2013

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Feb 06, 2013 Feb 06, 2013

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Feb 06, 2013 Feb 06, 2013

Copy link to clipboard

Copied

Michael_L_Hale wrote:

 

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.

 

The code I found to do that looks like this:

// Find the location where this script resides

function findScript() {
	var where = "";
	try {
		FORCEERROR = FORCERRROR;
	}
	catch(err) {
		// alert(err.fileName);
		// alert(File(err.fileName).exists);
		where = File(err.fileName);
	}
	return where;
}
JJMack

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines