Page 1 of 1

Need script for replacing string in file

Posted: Wed Feb 08, 2017 3:23 am
by Zoranj
I need help if someone can come up with a script that I can use in Switch to edit ink preset header.
We are sending PPF files (Cip3 ink preset) to Pecom for Rotoman.
For some reason Pecom can read only data inside the header of the PPF file and only from tag /Cip3AdmJobName

We use Printready workflow that writes only docket number into that tag, and writes sheet name into /Cip3AdmSheetName tag.

I would need to append this sheet name into Admjobname tag to get complete proper name of the signature that Pecom understands.

Attached is screenshot of how it looks and how it should look.
I googled everywhere and can't find solution.
ManRoland says that is the only way they can read tag, Heidelberg has setting for writing different tag, but it screws up names of the layout, etc..

I figure custom solution would be the best.
cip3 rename.png
cip3 rename.png (244.96 KiB) Viewed 11367 times
Thanks in advance.

Re: Need script for replacing string in file

Posted: Wed Feb 08, 2017 3:42 pm
by gabrielp
I can think of a few ways to accomplish this, but I think the most straightforward way would be to load the contents of the file as an array of lines lines like this:

Code: Select all

       // Construct File object
	var file = new File( job.getPath() );

	/// Open file as read only
	file.open( File.ReadOnly );

	// Create an array of the lines of the file
	var file_as_array = file.readLines();

	// Close the file
	file.close();
Then, loop through the lines until you find the line with the starting tag you're looking for. Then, append the needed values together and use File.write to write them into a file you send out of the script.

Re: Need script for replacing string in file

Posted: Wed Feb 08, 2017 4:02 pm
by sander
Can be done with scripting I guess, how is your experience with that?

I would read the /CIP3AdmJobName line and take the part after ( and before ), same for the /CIP3AdmSheetName.

Loop through the lines, and if /CIP3AdmJobName is on the line simply replace it.

I'll check some of my scripts where I do something similar..

Edit: Haha, I should not leave my posts idling for a couple of hours before hitting submit.. So, what Gabriel is saying ;)

Re: Need script for replacing string in file

Posted: Wed Feb 08, 2017 4:54 pm
by gabrielp
sander wrote:Edit: Haha, I should not leave my posts idling for a couple of hours before hitting submit.. So, what Gabriel is saying ;)
Great minds think alike ;)

Re: Need script for replacing string in file

Posted: Wed Feb 08, 2017 10:06 pm
by Zoranj
Thanks guys, it is over my head but I will try to sit down and decipher what you are telling me :)

Re: Need script for replacing string in file

Posted: Thu Feb 09, 2017 12:24 pm
by sander
Can you get me that ppf?

Maybe, don't pin me on it, I can play around with it if I have some spare time.

Re: Need script for replacing string in file

Posted: Thu Feb 09, 2017 5:35 pm
by Zoranj
Sure, here is link to Sig 1 front and sig 2 front, just in case so you can see the difference:
---
https://dl.dropboxusercontent.com/u/777 ... ig-1_F.ppf
https://dl.dropboxusercontent.com/u/777 ... ig-2_F.ppf

Re: Need script for replacing string in file

Posted: Tue Feb 14, 2017 7:49 pm
by Zoranj
I am very close, I feel it :) but need some more help.
I constructed a script using other snippets and some magic that works beautifully online on regex1o1.com site, however it is giving me parsing error in Switch scripter.
If you want to check 1o1 example that works: https://regex101.com/r/5SqNds/2

This is my code:

Code: Select all

var regex = /(\/CIP3AdmJobName *\()([^\)]+)(\)[^\n]+)\n([^\(]+\()([^\)]+)/g;
	var subst = `\$1\$2-\$5\$3\\n\$4\$5`;
	var extension = s.getPropertyValue("Extension");
   	var tempFile = job.createPathWithExtension(extension);
   	var myFile = new File(tempFile);
   	var InputPath = job.getPath();
   	var inputFileText = File.read(InputPath);
   	var outputFileText = inputFileText.replace(regex, subst);
	myFile.open( File.WriteOnly | File.Truncate );
   	myFile.writeLine(outputFileText);
   	myFile.close();
   	job.sendToSingle(tempFile);
   	job.sendToNull(InputPath);
It gives me parsing error in line 6, which is var subst = `\$1\$2-\$5\$3\\n\$4\$5`;

Re: Need script for replacing string in file

Posted: Tue Feb 14, 2017 7:53 pm
by gabrielp
Zoranj wrote:It gives me parsing error in line 6, which is var subst = `\$1\$2-\$5\$3\\n\$4\$5`;
It seems that you're using back ticks (`) when you should be quoting strings with (") or ('): http://eslint.org/docs/rules/quotes

Re: Need script for replacing string in file

Posted: Tue Feb 14, 2017 8:32 pm
by Zoranj
I tried but if I use single quote, this is the result (it removes my JobAdmSheetName completely and CIP3JobAdmName
---

Code: Select all

CIP3BeginSheet
/CIP3AdmJobCode (686708) def
$1$2-$5$3\n$4$5) def
%No TypeOfScreen
---
If I use regular quotes, this is the result:
----

Code: Select all

CIP3BeginSheet
/CIP3AdmJobCode (686708) def
$1$2-$5$3\n$4$5) def