Need script for replacing string in file

Post Reply
Zoranj
Member
Posts: 107
Joined: Tue Sep 20, 2016 7:37 pm
Location: Canada

Need script for replacing string in file

Post 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 11319 times
Thanks in advance.
User avatar
gabrielp
Advanced member
Posts: 645
Joined: Fri Aug 08, 2014 4:31 pm
Location: Boston
Contact:

Re: Need script for replacing string in file

Post 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.
Free Switch scripts: open-automation @ GitHub
Free Switch apps: open-automation @ Enfocus appstore

Want to hire me? I'm looking for my next gig. Contact me on LinkedIn or via email.
sander
Advanced member
Posts: 274
Joined: Wed Oct 01, 2014 8:58 am
Location: The Netherlands

Re: Need script for replacing string in file

Post 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 ;)
User avatar
gabrielp
Advanced member
Posts: 645
Joined: Fri Aug 08, 2014 4:31 pm
Location: Boston
Contact:

Re: Need script for replacing string in file

Post 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 ;)
Free Switch scripts: open-automation @ GitHub
Free Switch apps: open-automation @ Enfocus appstore

Want to hire me? I'm looking for my next gig. Contact me on LinkedIn or via email.
Zoranj
Member
Posts: 107
Joined: Tue Sep 20, 2016 7:37 pm
Location: Canada

Re: Need script for replacing string in file

Post by Zoranj »

Thanks guys, it is over my head but I will try to sit down and decipher what you are telling me :)
sander
Advanced member
Posts: 274
Joined: Wed Oct 01, 2014 8:58 am
Location: The Netherlands

Re: Need script for replacing string in file

Post 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.
Zoranj
Member
Posts: 107
Joined: Tue Sep 20, 2016 7:37 pm
Location: Canada

Re: Need script for replacing string in file

Post 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
Zoranj
Member
Posts: 107
Joined: Tue Sep 20, 2016 7:37 pm
Location: Canada

Re: Need script for replacing string in file

Post 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`;
User avatar
gabrielp
Advanced member
Posts: 645
Joined: Fri Aug 08, 2014 4:31 pm
Location: Boston
Contact:

Re: Need script for replacing string in file

Post 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
Free Switch scripts: open-automation @ GitHub
Free Switch apps: open-automation @ Enfocus appstore

Want to hire me? I'm looking for my next gig. Contact me on LinkedIn or via email.
Zoranj
Member
Posts: 107
Joined: Tue Sep 20, 2016 7:37 pm
Location: Canada

Re: Need script for replacing string in file

Post 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
Post Reply