Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
2

Script: insert folder name as text on open or saving document

Community Expert ,
Mar 06, 2024 Mar 06, 2024

I have a scenario where I need to insert the folder name on to the artboard/s within Illustrator 

 

For example

"Project Name>Illustrator>project name_version_01.ai"

on the artboard there is a placeholder that automatically updates
"Project Name"

 

then when a new document is required and is duplicated 

 

"New Project>Illustrator>New Project_version_01.ai"

the variable for "Project Name" updates to

"New Project"

 

Any ideas? 

For the record I use a script to save the file as a PDF

I'd be looking to add to this so when the file saves the text is updated automatically

this script already updates the date and time on the document automatically

 

as you see here i have text here called Folder Name

I'd like this text to dynamically update to the Folder Name

EugeneTyson_0-1709793324275.pngexpand image

 

So the text Folder Name updates to Folder Name 2

EugeneTyson_1-1709793419225.pngexpand image

 

 

Is this even possible? 

 

TOPICS
Scripting
356
Translate
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 Expert ,
Mar 06, 2024 Mar 06, 2024

Apologies there is another folder level

 

Folder name ..version 01...illustrator...filename.ai

Translate
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
Community Expert ,
Mar 07, 2024 Mar 07, 2024

you can get the active document full path as long as the file exists (it has been saved at least once)

var docRef = app.activeDocument.fullName // returns a file object

 

from here you can get the folder location 

var folder = docRef.parent; // returns a folder object

 

Let me know if you need additional help

Translate
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
Community Expert ,
Mar 08, 2024 Mar 08, 2024

Thanks @CarlosCanto 

Took me a while to get it right, but it is going back 3 folders now
<Folder>>Proof>Files>File.ai

And it returns <Folder> into the text marked on the page with Attributes through text marked with a Note

I've added this code to another script that saves the file and updates date/time and it seems to work quite well.

Code I came up with to fetch the folder name

// Main Code [Execution of script begins here]
try {
    if (app.documents.length > 0) {
        var sourceDoc = app.activeDocument;
        
        // Get the file path of the Illustrator document
        var filePath = sourceDoc.fullName.fsName;
        
        // Get the parent folder name
        var parentFolderName = getParentFolderName(filePath);
        
        // Log the retrieved parent folder name
        $.writeln("Retrieved Parent Folder Name: " + parentFolderName);
        
        // Update the text object with the folder name
        updateTextWithFolderName(parentFolderName);
        
        // Your existing code to save the document as PDF
        // ...
    } else {
        throw new Error('There are no documents open!');
    }
} catch(e) {
    alert(e.message, "Script Alert", true);
}

/** Retrieves the parent folder name from the file path.
    @param filePath The file path of the Illustrator document.
    @return The parent folder name.
*/
function getParentFolderName(filePath) {
    var pathComponents = filePath.split("/");
    var folderName = null;
    if (pathComponents.length > 3) { // Ensure there are enough components in the path
        // The parent folder is the fourth-to-last component in the path
        folderName = pathComponents[pathComponents.length - 4];
    }
    return folderName;
}

/** Updates the text object with the folder name.
    @param folderName The name of the folder.
*/
function updateTextWithFolderName(folderName) {
    var doc = app.activeDocument;
    var textObjectFound = false;
    for (var i = 0; i < doc.pageItems.length; i++) {
        var item = doc.pageItems[i];
        if (item.note === "Folder") {
            textObjectFound = true;
            item.contents = folderName;
            break; // Exit the loop once the folder name is updated
        }
    }
    if (!textObjectFound) {
        alert("Text object labeled 'Folder' not found in the document.");
    }
}

 

Translate
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
Community Expert ,
Mar 08, 2024 Mar 08, 2024
LATEST

awesome! thanks for sharing!

Translate
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