rejetto forum

Get path of currently selected file

Guest · 5 · 9596

0 Members and 1 Guest are viewing this topic.

e4g

  • Guest
Hello. I searched around the forum and couldn't find the answer to this question. I apologize ahead of time if it's already been answered (or is obvious).

Is it possible to get the path of the currently selected file in HFS so that it can be used in an external script? I've created a button that will only execute an external program if a single file is selected, but I want to reference that file in the code.

Example:

Code: [Select]
<form><input type='submit' name='view' value='View' onclick='
var a = selectedItems();
if (a.size() != 1)
this.value = "Select One File";
else
this.value = "Run"'/></form>
{.if|{.?view = Run.}|{:
{.exec|python script.py -e C:\pathto\selected\file.}
:}.}

Any help would be appreciated.


Offline raybob

  • Tireless poster
  • ****
    • Posts: 454
    • View Profile
    • FileSplat.com
HFS includes a function for getting file names...  Try using it in your onclick.  You'll find it in the standard lib.js.

Code: [Select]
function getItemName(el) {
    if (typeof el == 'undefined')
        return false;
    // we handle elements, not jquery sets 
    if (el.jquery)
        if (el.size())
            el = el[0];
        else
            return false;
    // take the url, and ignore any #anchor part
    var s = el.getAttribute('href') || el.getAttribute('value');
    s = s.split('#')[0];
    // remove protocol and hostname
    var i = s.indexOf('://');
    if (i > 0)
        s = s.slice(s.indexOf('/',i+3));
    // current folder is specified. Remove it.
    if (s.indexOf(HFS.folder) == 0)
        s = s.slice(HFS.folder.length);
    // folders have a trailing slash that's not truly part of the name
    if (s.slice(-1) == '/')
        s = s.slice(0,-1);
    // it is encoded
    s = (decodeURIComponent || unescape)(s);       
    return s;
} // getItemName


e4g

  • Guest
HFS includes a function for getting file names...  Try using it in your onclick.  You'll find it in the standard lib.js.

Code: [Select]
function getItemName(el) {
    if (typeof el == 'undefined')
        return false;
    // we handle elements, not jquery sets 
    if (el.jquery)
        if (el.size())
            el = el[0];
        else
            return false;
    // take the url, and ignore any #anchor part
    var s = el.getAttribute('href') || el.getAttribute('value');
    s = s.split('#')[0];
    // remove protocol and hostname
    var i = s.indexOf('://');
    if (i > 0)
        s = s.slice(s.indexOf('/',i+3));
    // current folder is specified. Remove it.
    if (s.indexOf(HFS.folder) == 0)
        s = s.slice(HFS.folder.length);
    // folders have a trailing slash that's not truly part of the name
    if (s.slice(-1) == '/')
        s = s.slice(0,-1);
    // it is encoded
    s = (decodeURIComponent || unescape)(s);       
    return s;
} // getItemName

Perhaps I'm missing something (I'm not very experienced with javascript) but how does that help me get the path to the selected file in a way that would allow it to be used in the macro? If I just needed the file name I could use "selectedFilesAsStr()" couldn't I? Can you perhaps give me an example of how this would work? Again, I appreciate the help.


e4g

  • Guest
EDIT: Nevermind, that doesn't actually work.

« Last Edit: July 29, 2011, 03:03:07 PM by e4g »


e4g

  • Guest
Thanks.  I figured out part of it (I think) although there may be a better way.

This:

Code: [Select]
<form><input type='submit' name='view' value='View' onclick='
var a = selectedItems();
if (a.size() != 1)
this.value = "Select One File";
else
this.value = selectedFilesAsStr()'/></form>
{.if not|{.?view = Select One File.}|{:
{.notify|{.vfs to disk|{.?view.}.}.}
:}.}

gives me a notify bubble with the full path to the file. However, this:

Code: [Select]
<form><input type='submit' name='view' value='View' onclick='
var a = selectedItems();
if (a.size() != 1)
this.value = "Select One File";
else
this.value = selectedFilesAsStr()'/></form>
{.if not|{.?view = Select One File.}|{:
{.exec|notepad {.vfs to disk|{.?view.}.}.}
:}.}

does not open the selected file with notepad. Does this make sense?



EDIT: Solved it:

Code: [Select]

<form><input type='submit' name='view' value='View' onclick='
var a = selectedItems();
if (a.size() != 1)
this.value = 0;
else
this.value = selectedFilesAsStr()'/></form>
{.if not|{.?view.}||{:
{.exec|notepad|{.vfs to disk|{.?view.}.}.}
:}.}
« Last Edit: July 29, 2011, 04:17:29 PM by e4g »