Skip to main content
JCmayer
Inspiring
February 11, 2021
Answered

FM Variable Folder Name

  • February 11, 2021
  • 2 replies
  • 744 views

Hi. I have a little problem with a variable.
i would like to recuperate the folder name, of my document, as value, to put it, on a master page.
I tought it was a system variable <$dir>.... but it don't exist.
Is it possible to create a user variable to get the content of the folder name?
Thank You.

JC

    This topic has been closed for replies.
    Correct answer frameexpert

    This isn't a complete script, but will it show you what is possible. And I like to have fun playing with code.

    Insert the Filename (Long) variable in a paragraph and put your text cursor in the paragraph and run this code:

    #target framemaker
    
    var doc, pgf, text, regex;
    
    // Get the active document.
    doc = app.ActiveDoc;
    // Get the paragraph that contains the Filename (Long) variable.
    pgf = doc.TextSelection.beg.obj;
    // Get the text of the paragraph containing the text cursor.
    text = getText (pgf, doc);
    // Drop the filename.
    text = text.replace (/\\[^\\]+$/, "");
    // Drop the path except the last folder.
    text = text.replace (/^.+\\/, "");
    // Show me the text that will get assigned to the user variable.
    alert (text);
    
    function getText (textObj, doc) {
        
        // Gets the text from the text object or text range.
    
        var text = "", textItems, i;
        
        // Get a list of the strings in the text object or text range.
        if (textObj.constructor.name !== "TextRange") {
            textItems = textObj.GetText(Constants.FTI_String);
        } else {
             textItems = doc.GetTextForRange(textObj, Constants.FTI_String);
        }
        // Concatenate the strings.
        for (i = 0; i < textItems.len; i += 1) {
            text += (textItems[i].sdata);
        }
        return text; // Return the text
    }
    

     

    2 replies

    LinSims
    Community Expert
    Community Expert
    February 11, 2021

    I think the variable you want is <$fullfilename>.

     

    All system variables are listed in the Variable panel. 

     

    [Ninja'd: this is what happens when you get distracted by work]

    JCmayer
    JCmayerAuthor
    Inspiring
    February 11, 2021

    Sorry LinSims, but I don't want all the path...

    ONLY the folder name of my document.

    Thanks

    frameexpert
    Community Expert
    frameexpertCommunity ExpertCorrect answer
    Community Expert
    February 11, 2021

    This isn't a complete script, but will it show you what is possible. And I like to have fun playing with code.

    Insert the Filename (Long) variable in a paragraph and put your text cursor in the paragraph and run this code:

    #target framemaker
    
    var doc, pgf, text, regex;
    
    // Get the active document.
    doc = app.ActiveDoc;
    // Get the paragraph that contains the Filename (Long) variable.
    pgf = doc.TextSelection.beg.obj;
    // Get the text of the paragraph containing the text cursor.
    text = getText (pgf, doc);
    // Drop the filename.
    text = text.replace (/\\[^\\]+$/, "");
    // Drop the path except the last folder.
    text = text.replace (/^.+\\/, "");
    // Show me the text that will get assigned to the user variable.
    alert (text);
    
    function getText (textObj, doc) {
        
        // Gets the text from the text object or text range.
    
        var text = "", textItems, i;
        
        // Get a list of the strings in the text object or text range.
        if (textObj.constructor.name !== "TextRange") {
            textItems = textObj.GetText(Constants.FTI_String);
        } else {
             textItems = doc.GetTextForRange(textObj, Constants.FTI_String);
        }
        // Concatenate the strings.
        for (i = 0; i < textItems.len; i += 1) {
            text += (textItems[i].sdata);
        }
        return text; // Return the text
    }
    

     

    Community Expert
    February 11, 2021

    Hi,

     

    I think that there is only the system variable Filename (long) which you can use.

    It contains the building block <$fullfilename>.

    That's the path plus file name.

    Does this help?

    Otherwise you would need to write an ExtendScript script.

    This should not be too difficult.

     

    Best regards

     

    Winfried

    frameexpert
    Community Expert
    Community Expert
    February 11, 2021

    Here is the process I would use to do it with a script.

    1) Find the document's Right master page. Every document has one.

    2) Create a temporary text frame on the master page.

    3) Insert the Filename (Long) variable in the text frame's first paragraph.

    4) Retrieve the full filename value from the text frame.

    5) Parse the value to remove the filename and leave the folder.

    6) Make or update the user variable that should contain this value.

    I am busy right now and can't do this for free, but if you want a quote for a script like this, please contact me offlist. A script like this could be triggered automatically so once it is written, you wouldn't have to think about it.

    JCmayer
    JCmayerAuthor
    Inspiring
    February 11, 2021

    Thank you frameexpert

     

    I will see your solution

     

    Best Regards