Created
March 21, 2018 18:52
-
-
Save vuski/2763fd4be8046b18fdb30b2f91576641 to your computer and use it in GitHub Desktop.
attached03
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//대화상자를 띄워 폴더를 선택한다 | |
var sourceFolder = Folder.selectDialog( 'Select the folder with svg files you want to convert to PDF', '~' ); | |
var destFolder = Folder.selectDialog( 'Select the folder where you want to save the converted PDF files.', '~' ); | |
var files = new Array(); | |
var fileType = "*.svg"; | |
//폴더의 모든 파일 목록을 읽는다 | |
var files = sourceFolder.getFiles( fileType ); | |
alert(files.length); | |
//파일 하나씩 실행시킨다 | |
for ( k = 0; k < files.length;k++ ) { | |
var docRef = app.open(files[k]); //파일열기 | |
var layer1=docRef.layers.getByName("Layer 1"); //Layer 1이 최상위 레이어라는 것을 미리 알고 있음 | |
//클리핑한다. | |
var clippingPathGroup= layer1.groupItems[1].groupItems; //1번 그룹에 지도 외곽선이 있다는 사실을 미리 알고 있음 | |
var clippingPathItem; | |
loop1 : for (var i=0 ; i<clippingPathGroup.length ; i++) { //루프를돌면서 path를찾는다 어떤형식으로 구성되어 있는지 미리 알고 있음 | |
var subItem = clippingPathGroup[i].pageItems; | |
for (var j=0 ; j<subItem.length ; j++) { | |
if (subItem[j].typename=="PathItem") { | |
clippingPathItem = subItem[j]; | |
break loop1; | |
} | |
} | |
} | |
for (var i=2 ; i<layer1.groupItems.length; i++) { | |
clippingPathItem.duplicate(layer1.groupItems[i], ElementPlacement.PLACEATBEGINNING); //위에서 저장한 레이어를 각 레이어의 맨 위에 둔다 | |
layer1.groupItems[i].clipped = true; //클리핑한다 | |
} | |
//비어있는 레이어를 지운다. 어떤 레이어가 비어있는지 미리 알고 있음 | |
layer1.groupItems[15].remove(); | |
layer1.groupItems[14].remove(); | |
layer1.groupItems[2].remove(); | |
//레이어 정리.레이어를 하나씩 만들면서 그룹을 그 레이어 밑에다 두고 이름을 복사한다 | |
var groupCount = layer1.groupItems.length; | |
for (var i=groupCount-1 ; i>=0 ; i--) { | |
var newLayer = docRef.layers.add(); | |
newLayer.name = layer1.groupItems[i].name; | |
layer1.groupItems[i].move(newLayer, ElementPlacement.PLACEATEND); | |
} | |
layer1.remove(); | |
var options = this.getOptions(); | |
var targetFile = this.getTargetFile(docRef.name, '.pdf',destFolder); | |
docRef.saveAs(targetFile, options); | |
docRef.close(); | |
} | |
//여기부터는 함수. 일러스트레이터에서 기본으로 제공하는 스크립트에서 참조했다 | |
/** Returns the options to be used for the generated files. | |
@return PDFSaveOptions object | |
*/ | |
function getOptions() | |
{ | |
// Create the required options object | |
var options = new PDFSaveOptions(); | |
// See PDFSaveOptions in the JavaScript Reference for available options | |
// Set the options you want below: | |
// For example, uncomment to set the compatibility of the generated pdf to Acrobat 7 (PDF 1.6) | |
// options.compatibility = PDFCompatibility.ACROBAT7; | |
// For example, uncomment to view the pdfs in Acrobat after conversion | |
// options.viewAfterSaving = true; | |
return options; | |
} | |
/** Returns the file to save or export the document into. | |
@param docName the name of the document | |
@param ext the extension the file extension to be applied | |
@param destFolder the output folder | |
@return File object | |
*/ | |
function getTargetFile(docName, ext, destFolder) { | |
var newName = ""; | |
// if name has no dot (and hence no extension), | |
// just append the extension | |
if (docName.indexOf('.') < 0) { | |
newName = docName + ext; | |
} else { | |
var dot = docName.lastIndexOf('.'); | |
newName += docName.substring(0, dot); | |
newName += ext; | |
} | |
// Create the file object to save to | |
var myFile = new File( destFolder + '/' + newName ); | |
// Preflight access rights | |
if (myFile.open("w")) { | |
myFile.close(); | |
} | |
else { | |
throw new Error('Access is denied'); | |
} | |
return myFile; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment