cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Check out the JMP® Marketplace featured Capability Explorer add-in
Choose Language Hide Translation Bar
statcon
Level II

Scripting: Getting the (absolute) path of the script itself

Hi JMP-Users,

i'm writing a script that uses several files that should be located in the same directory, like the script file. At the moment i am accessing these files using the "get current directory" command. Sadly this does only work if the user opened an instance of JMP by double-clicking on the script in his explorer.

If JMP is already running and the user opens the script via File -> Open -> ... . The current directory is the path of the JMP installation not the path of the scriptfile.

Is there a way to get the path of the scriptfile, to solve this problem?

Thanks,

Sebastian

1 ACCEPTED SOLUTION

Accepted Solutions
pmroz
Super User

Re: Scripting: Getting the (absolute) path of the script itself

This might do what you want:

get default directory()

I usually strip off the first character because it's a forward slash, something like "/M:/myapplication/bigdata/"

app_path = Substr(Get Default Directory(), 2, 999);

View solution in original post

8 REPLIES 8
pmroz
Super User

Re: Scripting: Getting the (absolute) path of the script itself

This might do what you want:

get default directory()

I usually strip off the first character because it's a forward slash, something like "/M:/myapplication/bigdata/"

app_path = Substr(Get Default Directory(), 2, 999);

statcon
Level II

Re: Scripting: Getting the (absolute) path of the script itself

Thats it!

Thanks You!

Re: Scripting: Getting the (absolute) path of the script itself


@pmroz wrote:

This might do what you want:

 

get default directory()

 

I usually strip off the first character because it's a forward slash, something like "/M:/myapplication/bigdata/"

 

app_path = Substr(Get Default Directory(), 2, 999);



This solution fails in the following case: if your script or application is launched via hyperlink from an HTML document, Get Default Directory() may return a temporary folder created by the web browser.

djhanson
Level V

Re: Scripting: Getting the (absolute) path of the script itself

Get Default Directory () works great....except when users map a network drive (e.g. X:\mynetworkpath\test.jsl).  Then you get the truncated path and not the full path (e.g. \\yournetwork.jmp.com\blahblah\mynetworkpath\test.jsl). 

If anyone knows a way around this, that is a way to actually read in the full path even if mapped, let me know!  thx.. dj

Re: Scripting: Getting the (absolute) path of the script itself

In regular SAS I have just parsed the output of the Windows (DOS) command "net use" to find the volume that is mapped to a particular drive letter.

djhanson
Level V

Re: Scripting: Getting the (absolute) path of the script itself

Thanks Tom.  That's a great idea!

Now I only wish that "net use" was a Windows Environmental Variable then I would simply use something like: Get Environment Variable("NETUSE") (which doesn't work of course).  dj

Craige_Hales
Super User

Re: Scripting: Getting the (absolute) path of the script itself

With a poorly thought out regex...maybe it is good enough...

txt = runprogram(executable("net"), options({"use"}), readfunction("text"));
show(regex(txt,"U:[ \t]*([^ \t]*)","\1")); // find the "U:" drive

the txt value looks like this, before the regex

"New connections will be remembered.
Status       Local     Remote                    Network
-------------------------------------------------------------------------------
             U:        \\network\root\u\chales   Microsoft Windows Network
The command completed successfully.
"

the regex captures this

Regex(txt, "U:[ \t]*([^ \t]*)", "\1") = "\\network\root\u\chales";

The 2nd argument to the regex is the pattern, which means

find U:, followed by zero or more blanks or tabs, then zero or more characters that are not blanks or tabs. That last bit is in parens, which form capture group \1, which is also the third argument that specifies what the result of the regex will be.

Craige
BioData41
Level II

Re: Scripting: Getting the (absolute) path of the script itself

Not sure how long this function has been around, as I am fairly new to JMP, and I just found this function myself while trying to solve a similar problem, but it seems that the function "Include File List()" returns a list of open files that are in the context of the executing script.  (By the way, the description of this function is only sort of informative, as is the case for most functions/methods/messages in the Scripting Index or online resources.)  

 

It seems that if this function is run at the top of the script before anything else is opened by the script that the script file path and script file name can be extracted from that as this is the only path returned at that point.  

When I tested it with other files open in JMP, and ran the script by double clicking it from Windows Explorer (with the script set to run on open without opening a script editor window by including "//!" at the top of the script), I get only the path of that script returned.  None of the other opened files are returned.