Skip to main content
Participant
April 30, 2024
Question

scripting

  • April 30, 2024
  • 3 replies
  • 920 views

Hello community,
I would like to create a script for the attached graphic.
the goal is not to have to build everything manually but to be able to execute a script to determine the arrangement and the corresponding mask with the usa flag.
Later I just want to customise the city (e.g. Texas) and have the same design created.  (Keyword scalability)
I have tried to create a script via chat gpt4 (see attachment) but it doesn't seem to work.
Can anyone help?
Thanks a lot.

Cheers Eran

 

#target illustrator

// Function to create text
function createText(doc, textContent, posX, posY, fontSize) {
    var textFrame = doc.textFrames.add();
    textFrame.contents = textContent;
    textFrame.textRange.characterAttributes.size = fontSize;
    textFrame.left = posX;
    textFrame.top = posY;
    return textFrame;
}

// Function to create flag pattern (simplified version)
function createFlagPattern(doc) {
    var rect = doc.pathItems.rectangle(100, 100, 200, 100);
    rect.fillColor = doc.swatches["Red"].color;  // Assuming 'Red' is a predefined swatch
    // Add more details like stripes and stars as per requirement
    return rect;
}

// Function to apply clipping mask
function applyClippingMask(doc, textFrame, pattern) {
    var group = doc.groupItems.add();
    textFrame.moveToBeginning(group);
    pattern.moveToBeginning(group);
    group.clipped = true;
}

// Main function to execute
function main() {
    if (app.documents.length == 0) {
        alert("Please open a document and try again.");
        return;
    }

    var doc = app.activeDocument;

    // Create text
    var textFrame = createText(doc, "FLORIDA", 100, -100, 72);

    // Create flag pattern
    var pattern = createFlagPattern(doc);

    // Apply clipping mask
    applyClippingMask(doc, textFrame, pattern);
}

// Run the script
main();

 

 

This topic has been closed for replies.

3 replies

Kurt Gold
Community Expert
Community Expert
April 30, 2024

Some people would try to be a bit more adamant when it comes to tackle a task assisted by ChatGPT. Did you provide a detailed requirements specification to the machine (something that a human creator would need as well)?

 

In case you already tried that, what did ChatGPT told you about the reasons why the script provided so far does not work as desired? It's pretty obvious why it does not meet your needs as per your screenshot and the code you provided. But if you think that you may get something pleasant with the practised hand of ChatGPT, I'd assume that you have to talk to it in depth, providing all necessary details in order to get your goal.

 

Apart from that, I think that a nested graphic style may be an alternative appropriate approach to get what you want. It could be a bit complicated, but it may work.

Eran-CHAuthor
Participant
May 1, 2024

thanks for your comment. i am still not a coding expert.

so yes i gave it a try with chat gpt but thought it would be more efficient here.

so please no critism. thanks

Kurt Gold
Community Expert
Community Expert
May 1, 2024

No criticism intended. And you are probably right that it may still be more efficient to ask in this forum.

 

I just wanted to indicate that you will have to provide uber-precise instructions to so-called intelligent devices like ChatGPT in order to get some well-oiled scripting approaches.

pixxxelschubser
Community Expert
Community Expert
April 30, 2024

Here is a screenshot of the result (note: text is no longer live text):

 

pixxxelschubser
Community Expert
Community Expert
April 30, 2024

I am sorry. For the life of me, I can't imagine how this ChatGPT code is supposed to lead to the result shown.

But the following revised version at least shows a result.

 

 

// Function to create text
function createText(doc, textContent, posX, posY, fontSize) {
    var textFrame = doc.textFrames.add();
    textFrame.contents = textContent;
    textFrame.textRange.characterAttributes.size = fontSize;
    textFrame.left = posX;
    textFrame.top = posY;
    return textFrame;
}

// Function to create flag pattern (simplified version)
function createFlagPattern(doc) {
    var rect = doc.pathItems.rectangle(-88, 100, 263, 100);
    //rect.fillColor = doc.swatches["CMYK Rot"].color;  // German standard red color for my own test
    rect.fillColor = doc.swatches["Red"].color;  // Assuming 'Red' is a predefined swatch
    
    // Add more details like stripes and stars as per requirement
    return rect;
}

// Function to apply clipping mask
function applyClippingMask(doc, textFrame, pattern) {
    //var group = doc.groupItems.add();
    //pattern.moveToBeginning(group);
    textFrame.selected = true;
    textFrame.createOutline();
    app.executeMenuCommand("ungroup");
    app.executeMenuCommand("compoundPath");
    //textFrame.moveToBeginning(group);
    pattern.selected = true;
    app.executeMenuCommand("makeMask");
    //group.clipped = true;
}

// Main function to execute
function main() {
    if (app.documents.length == 0) {
        alert("Please open a document and try again.");
        return;
    }

    var doc = app.activeDocument;

    // Create flag pattern
    var pattern = createFlagPattern(doc);

    // Create text
    var textFrame = createText(doc, "FLORIDA", 100, -100, 72);

    // Apply clipping mask
    applyClippingMask(doc, textFrame, pattern);
}

// Run the script
main();

 

Eran-CHAuthor
Participant
May 1, 2024

thanks a lot buddy