count tabs gather data from tabbed delimited text .JSX Adobe InDesign
I need to know how to read data from tabbed delimited text file in order to export merged documents to correct location.
-- will always be 4th tab 2nd row (group number) directory will be named by group number
-- have directories already created by privious .vbs script
data will have to be placed:
app.open(File(dirPath + "Card~" + (group number) + ".indd"));
and also:
fileName = (group number) + " " + myPageName + " " + date + ".pdf";
myFilePath = dirPath + docType + "/" + fileName;
myFile = new File(myFilePath);
myDocument.exportFile(ExportFormat.pdfType, myFile, false);
within previous .vbs script to work with this data file was written as so:
Call TwoDimensionArrayTest
Sub TwoDimensionArrayTest
Dim fso
Dim oFile
Dim arrline
Dim arrItem
Dim i
Dim arrMain()
Dim sFileLocation, strResults
Dim filesys, folder, path
Const forReading = 1
strFolder = "P:\RxCut\In Design Implementation\build\Co-Brand\"
mkdir = "P:\RxCut\In Design Implementation\"
Set objFSO = CreateObject("Scripting.FileSystemObject")
For Each objFile In objFSO.GetFolder(strFolder).Files
If Right(LCase(objFile.Name), 4) = LCase(".txt") Then
' The file contains on each line:
' Text1 (tab) Text2 (tab) Text3 (tab) Text4
' Text5 (tab) Text6 (tab) Text7 (tab) Text8
'etc etc
Set fso = CreateObject("Scripting.FileSystemObject")
sFileLocation = objFile.Name
Set oFile = fso.OpenTextFile(objFile.Name, forReading, False)
Do While oFile.AtEndOfStream <> True
strResults = oFile.ReadAll
Loop
' Close the file
oFile.Close
' Release the object from memory
Set oFile = Nothing
' Return the contents of the file if not Empty
If Trim(strResults) <> "" Then
' Create an Array of the Text File
arrline = Split(strResults, vbNewLine)
End If
For i = 0 To UBound(arrline)
If arrline(i) = "" Then
' checks for a blank line at the end of stream
Exit For
End If
ReDim Preserve arrMain(i)
arrMain(i) = Split(arrline(i), vbTab)
Next
path = arrMain(1)(3)
dir = mkdir & path
set filesys=CreateObject("Scripting.Fil开发者_C百科eSystemObject")
If Not filesys.FolderExists(dir) Then
Set folder = filesys.CreateFolder(dir)
End If
dir = mkdir & path
Set folder = filesys.CreateFolder(dir & "\Web")
Set folder = filesys.CreateFolder(dir & "\Web\Web Cards")
Set folder = filesys.CreateFolder(dir & "\Web\Web Headers")
fso.MoveFile sFileLocation, arrMain(1)(3) & ".txt"
End If
Next
End Sub ' TwoDimensionArrayTest
How can I achieve something similar in .jsx?
Figured it out this will read 1,3 and create a variable for group number 1,3
var aFile = File("P:/RxCut/In Design Implementation/build/automate/automate.txt");
var fileData = readTabDelimitedFile ( aFile ) ;
//$.writeln(fileData[1][3]); //should contain first tab on second line
tstdata = fileData[1][3];
var group = tsdata
function readTabDelimitedFile ( fPath ) {
var returnArray = new Array ( ) ;
//-- Verify that the file exists
var fileObject = File ( fPath ) ;
if ( ! fileObject.exists ) {
return returnArray ; // an empty array because the file doesn't exist.
}
//-- Create a regular expression for a tab.
var tabExpression = new RegExp ( '\\t' ) ;
//-- Read the file.
try {
//-- The file has to be open.
fileObject.open ('r') ; //-- Open for reading.
while ( ! fileObject.eof ) {
var currentLine = fileObject.readln () ;
if ( tabExpression.test( currentLine ) ) {
returnArray.push(currentLine.split ('\t')) ;
}
}
fileObject.close() ;
}
catch (errMain) {
try {
fileObject.close() ;
}
//-- if the close generates an error skip it.
catch (errInner ) { /* nothing here */ }
}
return returnArray ;
var group = returnArray
}
精彩评论