November 25, 201114 yr Here is a slight variation of the writeToFile module, so it also accepts filemaker paths. I know it is not 100% error free. OutputStreamWriter writer; File f; try { //The path is an OS path f = new File ( filePath ); create = f.createNewFile(); } catch ( Exception E ) { //The path is a FM path f = fmpro.fileForFMPath ( filePath ); create = f.createNewFile(); } writer = new OutputStreamWriter( new FileOutputStream( f ), "utf-8" ); writer.write( textToWrite ); writer.close(); return true;
November 29, 201114 yr Andries Here is a try at some error checking // WriteToFileAndries ( fm_filePath ; fm_textToWrite ; fm_overwrite ) // 11_11_29 JR // v1.1 // idea from Andries // Write to File with some error checking // OVER = fm_overwrite == '1'? true : false SYS = System.getProperty("os.name").toLowerCase().substring(0,3) isMac = SYS == 'mac'? true : false isWin = SYS == 'win'? true : false colon = fm_filePath.contains(/:/) def fileMake = { file -> if( !file.exists() || OVER ){ create = file.createNewFile() } else { throw new Exception() }// end if } def cleanPath = { path -> def cleaned = path.replace('file:', '').replace('filemac:', '').replace('filewin:', '') def firstChar = cleaned.indexOf(cleaned.replace('/', '')[0]) //removes first char if it is slash returnPath = cleaned[firstChar..-1] returnPath = isMac? '/Volumes/' + returnPath : returnPath.replace('/', '') } if( fm_filePath[0..3].toLowerCase() == 'file' && colon){ //this is a filemaker path try{ f = new File ( cleanPath(fm_filePath) ) fileMake(f) } catch (e) { return 'FILE exists already' }// end try } else { try{ //test for OS if(isMac && colon || isWin && !colon){ return 'WRONG path format for this OS' } f = new File ( fm_filePath ) fileMake(f) } catch (e) { return 'FILE exists already' }// end try }//end if if ( f.canWrite()){ writer = new OutputStreamWriter( new FileOutputStream( f ), 'utf-8') try{ writer.write(fm_textToWrite) writer.close() } catch (e) { f.delete() return e }// end true } else { return 'FILE not writeable' }// end if return true edited: Amended to remove dependancy on CF
December 3, 201114 yr Author Hi John, thanks for the code. The idea is however that the fmpro object provides a method that allows us to reference a file in Groovy by using a FMPath: fmpro.fileForFMPath( fmpath ). So there is no need to transform the path from FileMaker to OS. I adapted your code, and removed some parts which I think are no longer needed. If I removed too much, feel free to add them again or if you have any other comment, feel free as well. // WriteToFileAndries ( filePath ; textToWrite ; overwrite ) // 11_11_29 JR // v1.1 //Write to File with some error checking // 11_12_03 AH // v.1.2 // Adapted so it uses fmpro.fileForFMPath() method // // OVER = overwrite == '1'? true : false def fileMake = { file -> if( !file.exists() || OVER ){ create = file.createNewFile() } else { throw new Exception() }// end if } File f; //create the file object and try to create the file (if not existsing/overwrite) try { //Try first to create a file from OS path f = new File ( filePath ) fileMake(f) } catch (e) { //Try to create a file from FM path try { f = fmpro.fileForFMPath ( filePath ) fileMake(f) } catch () { return "file cannot be created" } } catch (e) { //SOME ERROR TRAPPING... } if ( f.canWrite()){ writer = new OutputStreamWriter( new FileOutputStream( f ), 'utf-8') try{ writer.write(textToWrite) writer.close() } catch (e) { f.delete() return e }// end true } else { return 'FILE not writeable' }// end if return true I am actually having doubts about the following part of the code. I don't know if it is a good idea to indent all those try... catch... logic. And most of all I am not sure if this is the correct way to do it. File f; //create the file object and try to create the file (if not existsing/overwrite) try { //Try first to create a file from OS path f = new File ( filePath ) fileMake(f); } catch (e) { //Try to create a file from FM path try { f = fmpro.fileForFMPath ( filePath ); fileMake(f); } catch ( er ) { return "file cannot be created"; } } catch (e) { //SOME ERROR TRAPPING... }
December 3, 201114 yr Andries.. I get that, but for me the fmpro.fileForFMPath only returns the file name not a path to file that can be used, but then seems to use the path anyway. Didn't quite get how it is supposed to work... Was just extending the conversation to expand my Groovy skills too..
February 16, 201213 yr Dear Andries, I use writetofile from scriptmaster and i 've got a problem with root path, if that's no c: I'm sorry but I don't know any idea about java programming. Please , can you write me how I have to put it in a filemaker script.... to register the new fixed function Thanks a lot.
Create an account or sign in to comment