Jump to content

Written File Encoding (line endings)


This topic is 3852 days old. Please don't post here. Open a new topic instead.

Recommended Posts

Currently have a process working where i export global field contents to the desktop but of course this file is encoded as:

 

Unicode ( UTF-16 LittleEndian) with Classic Mac (CR)

 

But the 3rd Party wants the file to be:

 

WINDOWS ASCII WITH {CR}{LF} ROW DELIMITERS

 

Can ScriptMaster Groovy fix the file format once it's written?

 

 

Link to comment
Share on other sites

Managed to Find This Code:

 

However everytime I open it in BBEdit it is UTF-8 ( haven't tested on Windows yet ) but not sure how to get it to automatically be Western (ASCII)

/* 
Purpose: Writes the provided text to a file with the requested encoding and EOL.
Returns: Text

Name: SaveTextToFile ( filePath ; theText ; theEncoding ; eolFormat )

Parameters: 	( filePath ) filepath
            	( theText ) text
			( theEncoding ) enum = (UTF-8, US-ASCII, LATIN-1...LATIN-9)
			( theFormat ) enum = (MAC (< Mac OS X), WIN, UNX (includes Mac OS X)

Dependencies: NONE

2011-08-31 JPS, Created.

NOTES:
Supported Encodings
 8859_1 (ISO-8859-1/LATIN-1)
 8859_2 (ISO-8859-2/LATIN-2)
 8859_3 (ISO-8859-3/LATIN-3)
 8859_4 (ISO-8859-4/LATIN-4)
 8859_5 (ISO-8859-5/LATIN-5)
 8859_6 (ISO-8859-6/LATIN-6)
 8859_7 (ISO-8859-7/LATIN-7)
 8859_8 (ISO-8859-8/LATIN-8)
 8859_9 (ISO-8859-9/LATIN-9)
 ASCII (7-bit ASCII)
 UTF8 (UCS Transformation Format-8)

Supported EOL:
Mac, Windows, Linux/UNIX
*/

theEncoding = theEncoding.toUpperCase();

//Determine if the provided encoding is valid, otherwise return an error.
switch ( theEncoding ) {
    case ["UTF8", "UTF-8"]:
        theEncoding = "UTF-8";
        break;
 
    case ["US-ASCII", "ASCII"]:
        theEncoding = "US-ASCII";
        break;
 
    case ["ISO-8859-1/LATIN-1", "ISO-8859-1", "8859-1", "8859_1", "LATIN 1", "LATIN-1"]:
        theEncoding = "8859_1";
        break;

    case ["ISO-8859-2/LATIN-2", "ISO-8859-2","8859-2", "8859_2", "LATIN 2", "LATIN-2"]:
        theEncoding = "8859_2";
        break;
 
    case ["ISO-8859-3/LATIN-3", "ISO-8859-3","8859-3", "8859_3", "LATIN 3", "LATIN-3"]:
        theEncoding = "8859_3";
        break;
        
    case ["ISO-8859-4/LATIN-4", "ISO-8859-4","8859-4", "8859_4", "LATIN 4", "LATIN-4"]:
        theEncoding = "8859_4";
        break;
    
    case ["ISO-8859-5/LATIN-5", "ISO-8859-5","8859-5", "8859_5", "LATIN 5", "LATIN-5"]:
        theEncoding = "8859_5";
        break;
 
    case ["ISO-8859-6/LATIN-6", "ISO-8859-6","8859-6", "8859_6", "LATIN 6", "LATIN-6"]:
        theEncoding = "8859_6";
        break;
        
    case ["ISO-8859-7/LATIN-7", "ISO-8859-7","8859-7", "8859_7", "LATIN 7", "LATIN-7"]:
        theEncoding = "8859_7";
        break;

    case ["ISO-8859-5/LATIN-8", "ISO-8859-8","8859-8", "8859_8", "LATIN 8", "LATIN-8"]:
        theEncoding = "8859_8";
        break;

    case ["ISO-8859-5/LATIN-9", "ISO-8859-9","8859-9", "8859_9", "LATIN 9", "LATIN-9"]:
        theEncoding = "8859_9";
        break;

    default:
        //Invalid Type provided
        return false;
} 

theFormat = theFormat.toUpperCase();

//Determine if the provided eolForm is valid, otherwise return false.
switch ( theFormat ) {
    case ["WIN", "WINDOWS", "CR+LF", "CRLF", "rn"]:
        theFormat = "rn";
        break;
 
    case ["MAC", "CR", "r"]:
        theFormat = "r";
        break;
 
    case ["UNIX", "UNX", "LINUX", "LNX", "MAC OS X", "n"]:
        theFormat = "n";
        break;

    default:
        //Invalid Type provided
        return false;
}

//The BufferedReader reads lines without the EOL, just append the selected EOL
BufferedReader br = new BufferedReader( new StringReader ( theText ));
StringBuilder sb = new StringBuilder();
for (;;) {
    String line = br.readLine();
    if (line == null)
        break;
    sb.append(line + theFormat);
}
theText = sb.toString();

//This more advanced version allows you to specify the character encoding
OutputStreamWriter writer = new OutputStreamWriter( new FileOutputStream( filePath ), theEncoding );
writer.write( theText );
writer.close();
return true;
Link to comment
Share on other sites

  • 2 months later...

Hi Stephen

I am not sure about your script above, but I have edited the simple "Write to file" function to include a parameter for the encoding format.

This way, I specify in the plugin call, which format I want to write.

The same modification is applied to the "Read file content" function.

In my case, I had a FMGO solution, save content of a text field to a file and email that file.

Perfectly simple. However, FMGO saves the file as utf-16 and when I imported the text in another FM file, it expected utf-8.

So, by having the encoding parameter, I am able to both write and read files with different encodings. This, however, requires me to know the encoding format on beforehand...

 

Java supports a LOT of encodings and you can find description here: http://docs.oracle.com/javase/1.5.0/docs/guide/intl/encoding.doc.html

 

Input Variables:

filePath

textToWrite

encoding

 

The groovy script:

 

//This simple version uses utf-8

//new File( filePath ).write( textToWrite );

 

//This more advanced version allows you to specify the character encoding

OutputStreamWriter writer = new OutputStreamWriter( new FileOutputStream( filePath ), encoding );

writer.write( textToWrite );

writer.close();

return true;

Link to comment
Share on other sites

This topic is 3852 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.