How do I edit a non-text file without corrupting it?

0 favourites
  • 11 posts
From the Asset Store
In this template the music plays without looping in your game
  • I've made a useful little app using Construct 2 and NW.js to help me with managing lots of files on my windows desktop. It does stuff like moving files and renaming them automatically, saving me loads of repetitive work. However I really wish I could insert small bits of information in these files as well. The files are .eps, a vector format used in graphic design and editable with Adobe Illustrator and such.

    I got my script to make changes in the files, using the NWjs object and the Write File command but the files it generates are no longer readable by Illustrator. I can edit these eps files with a binary text editor just to add some bits of metadata that Illustrator can't add but I was hoping I could do the same, automatically, with my Construct app. Does anyone know if it is possible at all? Maybe with javascript or some plugin?

    Thanks

  • It’s a bad idea to add data to a file like that. It would

    Break it. The write file feature focuses on text files but that’s not the issue. Basically you need to know or have a JavaScript library know how the data is stored in the files. And even then many formats don’t allow you to add unrelated data in it.

    Here’s a different idea. You could create your own file in the folder and store the extra data there.

  • It’s a bad idea to add data to a file like that. It would

    Break it. The write file feature focuses on text files but that’s not the issue. Basically you need to know or have a JavaScript library know how the data is stored in the files. And even then many formats don’t allow you to add unrelated data in it.

    Here’s a different idea. You could create your own file in the folder and store the extra data there.

    I process over 100 eps files every day and for each one I have to add a title and a headline in the metadata, in both of which I insert the name of the respective file. I do this using Illustrator but it would have been so much easier to do by script if only I could get my app to write the eps files correctly. I mean I can even edit eps files with MS Word without corrupting them so I was hoping it wouldn't be too difficult to do by script.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • I don't use nwjs, but I tried loading a binary file with ajax and saving it with the browser object. The file doesn't load. It looks like saving and loading as text will not work. Nwjs saves and loads as text as well.

    To save and load as binary will require an entirely new plugin to do so or to use javascript to do it in the browser execjs action. There's a fair amount of stuff to look up to do that.

    But then if you get past that, there's the matter of modifying the file in a way that doesn't break it. It depends on how the file format is laid out.

  • I would expect that Illustrator would use the basic svg format which is essentially xml.

  • I don't use nwjs, but I tried loading a binary file with ajax and saving it with the browser object. The file doesn't load. It looks like saving and loading as text will not work. Nwjs saves and loads as text as well.

    To save and load as binary will require an entirely new plugin to do so or to use javascript to do it in the browser execjs action. There's a fair amount of stuff to look up to do that.

    But then if you get past that, there's the matter of modifying the file in a way that doesn't break it. It depends on how the file format is laid out.

    Right, so it's not exactly simple to do this. Thank you for putting things in perspective. I'm pretty sure what I need is doable with MS Word and macros however so I may give it a try.

  • I would expect that Illustrator would use the basic svg format which is essentially xml.

    That's one of the formats it can open and save, yes, but the files I work with need to be eps. The metadata part of the eps is also xml I suppose.

    EDIT: I may be able to convert the eps files into svg easily, edit the svgs with my app without corrupting them since it's really just text and then reconvert them back to eps. I'm going to give it a try, thanks!

  • Not all EPS files have binary data, some are entirely plain-text.

    You can modify your script to look for the "%%BeginBinary" tag and if it doesn't exist - process this file. If this tag is present - skip the file and edit it manually.

  • Here's a rough reference on loading and saving binary files. Untested but it has the relavant sources. In the examples i skipped over some more robust error checking and cross browser compatability.

    Ajax way

    https://developer.mozilla.org/en-US/doc ... ng_Started

    https://developer.mozilla.org/en-US/doc ... rrayBuffer

    https://stackoverflow.com/questions/234 ... -a-browser

    xhttp = new XMLHttpRequest();
    xhttp.responseType = 'arraybuffer';  //default is text
    xhttp.open('GET', 'old.eps');
    xhttp.onreadystatechange = function()
    {
    	if (xhttp.readyState === XMLHttpRequest.DONE && xhttp.status === 200) 
    	{
    		var byteview = new Int8Array(xhttp.response);
    		// do stuff. view is an array of bytes.
    		
    		saveByteArray([byteview], 'new.eps');  // from the third link above
    	}
    }
    xhttp.send();
    [/code:1w600qbz]
    
    nwjs way
    [url=https://nodejs.org/api/fs.html]https://nodejs.org/api/fs.html[/url]
    [url=https://nodejs.org/api/buffer.html#buffer_class_buffer]https://nodejs.org/api/buffer.html#buffer_class_buffer[/url]
    
    [code:1w600qbz]
    fs = require('fs');
    
    fs.readFile('test.eps', (err, data) => {
    	if (err)
    		throw err;
    	// do something with data.  data is an array of bytes
    	fs.writeFile('test_modified.eps', data, (err) => {
    		if (err) 
    			throw err;
    	});
    });
    [/code:1w600qbz]
    
    As for the eps file format itself i found this:
    [url=http://www.fileformat.info/format/eps/egff.htm#EPSF-DMYID.2]http://www.fileformat.info/format/eps/e ... SF-DMYID.2[/url]
    an eps file can either be all text or start with a binary header showing where things are in the file: the text ps portion, and a wmf and tiff portion which is a simple thumbnail of what the image looks like.
    
    I'm guessing the metadata you want to add would be lines that start with "%".  So you could look at the header, get that portion of the file (which is all text) and add additional lines in.  Looks like they would have to be after the initial % line as that has special meaning, also the text would have to jeust be ascii per the spec it look like.  Then before saving the file you'd have to redo the header with the new sizes and offsets of everything.  So you'd probably have to create an entirely new buffer to save to.  Seems kind of low level but the links about buffers should be an acceptable reference.
    
    Anyways perhaps some of that is useful.  I'll be sure to use it as a reference later regardless.
    
    Edit: i suppose i did skip the part on how this can be used.  Typically it would be done in a plugin or with some clever usage of the browser execjs action.  Debugging with either has a learning curve but it can be done.
  • Too bad Construct Classic is stuck in xp mode.

    As I recall it had a plug for binary, as well as one for passing command line parameters to a program like Ghostscript.

  • Thank you for your help, everyone. What I've found is that most binary data in eps files saved by Illustrator stores editability functions. Without it the files are a mess to work with for anyone who opens them. Illustrator does the same with svg files but the editability data in them doesn't seem to be binary. I could have done everything I needed through svg files but even though they can store any metadata, Illustrator refuses to load the metadata when it opens an svg so when it converts it back to an eps, the eps is void of metadata.

    This is obviously a problem I'll need to dedicate more time and effort to. Thanks for the leads.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)