Skip to main content
Inspiring
January 4, 2023
Question

Help with a script

  • January 4, 2023
  • 1 reply
  • 414 views

Hey 🙂


i'm trying to acheive a script that will scan a folder for pdfs

Here is the example folder:

 

then, it'll create one ai file that will include all pdfs with the same prefix as links, while sorting them as shown here, into 4 different rows:

1. right sign, fron side

2. left sign, front side

3. right sign, back side

4. left sign, back side.

every sign has a back side, but not every right sign has a left parallel sign.

"1"prefix will cause sign to be placed at the top

"2" will be right after the top

etc...

it'll look like this:

 


someone helped me with this. his done the script,  but it works only with 2 rows (not 4 as i need) - if the pdfs contain right+ left and front + back side then it's all a mess:
-

 

Can someone understand what i'm trying to acheive or does my language barrier make it difficult?

 

here is the script:

 

// doc = app.documents.add()
var doc= app.activeDocument


//Get pdfs
pdf_folder = Folder.selectDialog()
pdfs = pdf_folder.getFiles('*.pdf')

front_side = []
back_side = []
for (var f in pdfs){
    if(pdfs[f].name.toLowerCase().search('back')!=-1){
        back_side.push(pdfs[f])
    }
    else{
        front_side.push(pdfs[f])
    }

}


//layer
itemLayer = doc.layers[0]


function newRect(x, y, width, height) {
    var l = 0;
    var t = 1;
    var r = 2;
    var b = 3;

    var rect = [];

    rect[l] = x;
    rect[t] = -y;
    rect[r] = width + x;
    rect[b] = -(height - rect[t]);

    return rect;
};



gap_ver_global = 10
gap_hor_global = 100



for (var f = 0; f < front_side.length; f ++){
    my_f_name = front_side[f].name.replace('.pdf','')
    placePdfOnNewAB(front_side[f],gap_ver_global,f,my_f_name)
}



for (f in back_side){
    my_f_name = back_side[f].name.replace('.pdf','')
    placePdfOnNewAB_back(back_side[f],gap_ver_global,gap_hor_global,f,my_f_name)
}


space_top = 10

space_right = 100
space_bottom = 100


//front side
x1 = doc.artboards[0].artboardRect[0]
y1 = doc.artboards[0].artboardRect[1] - space_top


width = doc.artboards[front_side.length-1].artboardRect[2]-doc.artboards[front_side.length-1].artboardRect[0] + space_right
height = -doc.artboards[front_side.length-1].artboardRect[3] + space_bottom

front_AB = doc.artboards.add(doc.artboards[0].artboardRect)
new_artboard_rect = newRect(x1,y1,width,height)
front_AB.artboardRect = new_artboard_rect




//back side
from = front_side.length
to = front_side.length+back_side.length-1
x1 = doc.artboards[from].artboardRect[0]-space_right
y1 = doc.artboards[from].artboardRect[1] - space_top


width = doc.artboards[from].artboardRect[2]-doc.artboards[to].artboardRect[0] + space_right
height = -doc.artboards[to].artboardRect[3] + space_bottom

front_AB = doc.artboards.add(doc.artboards[from].artboardRect)
new_artboard_rect = newRect(x1,y1,width,height)
front_AB.artboardRect = new_artboard_rect


function placePdfOnNewAB(nFile,gap_ver,count,fname){

    artboards = doc.artboards
    var artboard;

    placed = itemLayer.placedItems.add();  
    placed.file = nFile;

    h = placed.height
    w = placed.width
    
    if(count==0){
        artboard = doc.artboards[0]
        artboard.artboardRect = newRect(0,0,w,h)
        artboard.name = fname

        


    }

    else{
        artboard = artboards.add(artboards[artboards.length-1].artboardRect);
        x1 = artboards[artboards.length-1].artboardRect[0]
        y1 = - artboards[artboards.length-1].artboardRect[3] + gap_ver

        artboard.artboardRect = newRect(x1,y1,w,h)
        artboard.name = fname
    }

    placed.position = [artboard.artboardRect[0],artboard.artboardRect[1]]

    


}


function placePdfOnNewAB_back(nFile,gap_ver,gap_hor,count,fname){

    artboards = doc.artboards
    var artboard;

    placed = itemLayer.placedItems.add();  
    placed.file = nFile;

    h = placed.height
    w = placed.width


    if(count==0 ){
        artboard = artboards.add(artboards[0].artboardRect)
        x1 = artboards[0].artboardRect[0] - w - gap_hor
        y1 = artboards[0].artboardRect[1]
        artboard.artboardRect = newRect(x1,y1,w,h)
        artboard.name = fname


    }
    else{
        artboard = artboards.add(artboards[artboards.length-1].artboardRect);
        x1 = artboards[artboards.length-1].artboardRect[0]
        y1 = - artboards[artboards.length-1].artboardRect[3] + gap_ver

        artboard.artboardRect = newRect(x1,y1,w,h)
        artboard.name = fname
    }

    placed.position = [artboard.artboardRect[0],artboard.artboardRect[1]]

}


//place pdf 1st
//resize artboard if 1st artboard -> move pdf
//add new artboard -> resize -> place it underneath the previous artboard -> move pdf








// alert(placed)

 

This topic has been closed for replies.

1 reply

danezeqAuthor
Inspiring
January 17, 2023

anyone?

danezeqAuthor
Inspiring
January 27, 2023

Help!