Page 1 of 2

rtf files

Posted: Mon Jul 07, 2014 5:38 pm
by spacebuddy
Is there any way to save an rtf file using the EditorGadGet?

I can't find anything in the help file. :oops:

Re: rtf files

Posted: Wed Jul 09, 2014 6:21 am
by Danilo
Try this to get the RTF as string:

Code: Select all

Procedure LoadRTF(gadget, filename.s)
    CocoaMessage(0,GadgetID(gadget),"readRTFDFromFile:$",@filename)
EndProcedure

Procedure.s GetEditorRTF(gadget)
    Protected range.NSRange, result.s, length, theData
    range\location = 0
    range\length   = Len(GetGadgetText(gadget))
    CocoaMessage(@nsData,GadgetID(gadget),"RTFFromRange:@",@range)
    If nsData
        length  = CocoaMessage(0,nsData,"length")
        theData = CocoaMessage(0,nsData,"bytes")
        result = PeekS(theData,length,#PB_UTF8) ; #PB_Ascii or #PB_UTF8 ?
    EndIf
    ProcedureReturn result
EndProcedure

Procedure Btn1_Clicked()
    Protected filename.s = OpenFileRequester("Open RTF","","*.rtf",0)
    If filename
        LoadRTF(0,filename)
    EndIf
EndProcedure

Procedure Btn2_Clicked()
    Protected rtf.s = GetEditorRTF(0)
    Debug rtf
    Debug "-[RTF ends]---------------------------------------------------"
EndProcedure


If OpenWindow(0, 0, 0, 640, 480, "EditorGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    EditorGadget(0, 0, 0, 640, 455)
    
    CocoaMessage(0,GadgetID(0),"setRichText:",#YES)                      ; enable RichText
    CocoaMessage(0,GadgetID(0),"setAutomaticLinkDetectionEnabled:",#YES) ; Link detection
    CocoaMessage(0,GadgetID(0),"setImportsGraphics:",#YES)               ; import images
    ;CocoaMessage(0,GadgetID(0),"setAllowsImageEditing:",#YES)            ; allow image editing
    
    ;CocoaMessage(0,GadgetID(0),"setRulerVisible:",#YES) ; enable Ruler
    ;CocoaMessage(0,GadgetID(0),"setUsesRuler:",#YES)    ; enable Ruler
    ;CocoaMessage(0,GadgetID(0),"updateRuler")           ; update Ruler
    
    ButtonGadget(1,440,455,100,25,"Load RTF")
    BindGadgetEvent(1,@Btn1_Clicked())
    
    ButtonGadget(2,540,455,100,25,"Get RTF")
    BindGadgetEvent(2,@Btn2_Clicked())
    
    For a = 0 To 5
        AddGadgetItem(0, a, "Line "+Str(a))
    Next
    
    SetActiveGadget(0)
    Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf

Re: rtf files

Posted: Wed Jul 09, 2014 5:35 pm
by spacebuddy
Thanks Danilo, that works perfect. But how do I save the rtf file to disk? When I save, it saves in regular text. :shock:

Re: rtf files

Posted: Wed Jul 09, 2014 5:54 pm
by Danilo
Just save the string?

Code: Select all

Procedure Btn2_Clicked()
    Protected rtf.s = GetEditorRTF(0)
    Debug rtf
    Debug "-[RTF ends]---------------------------------------------------"
    If CreateFile(0,GetHomeDirectory()+"output_test.rtf")
        WriteString(0,rtf,#PB_UTF8)
        CloseFile(0)
    EndIf
EndProcedure
Another one, writes RTF with attachments to directory:

Code: Select all

Procedure SaveRTF(gadget, directoryName.s)
    CocoaMessage(0,GadgetID(gadget),"writeRTFDToFile:$",@directoryName,"atomically:",#NO)
EndProcedure

SaveRTF(0,GetHomeDirectory()+"output_test_2")

Re: rtf files

Posted: Wed Jul 09, 2014 6:53 pm
by spacebuddy
Dear Danilo,

Yes, it is working very goods now.

Thank you :D :D

Re: rtf files

Posted: Wed Jul 09, 2014 7:03 pm
by wilbert
It's also possible to use the textStorage object from the EditorGadget.
It makes it possible to use one procedure to allow for writing multiple file types.

Code: Select all

Procedure SaveFormattedText(EditorGadget, FileName.s, Type.s = "NSRTF")
  
  ; Type can be "NSPlainText", "NSRTF", "NSHTML", "NSDocFormat", "NSWordML", "NSOfficeOpenXML", "NSOpenDocument"
  
  Protected.i range.NSRange, attributes, dataObj, textStorage = CocoaMessage(0, GadgetID(EditorGadget), "textStorage")
  CocoaMessage(@range\length, textStorage, "length")
  CocoaMessage(@attributes, 0, "NSDictionary dictionaryWithObject:$", @Type, "forKey:$", @"DocumentType")
  CocoaMessage(@dataObj, textStorage, "dataFromRange:@", @range, "documentAttributes:", attributes, "error:", #Null)
  ProcedureReturn CocoaMessage(0, dataObj, "writeToFile:$", @FileName, "atomically:", #NO)
  
EndProcedure
Example :

Code: Select all

SaveFormattedText(0, "MyFile.rtf", "NSRTF"); save as rtf file

Re: rtf files

Posted: Wed Jul 09, 2014 7:04 pm
by Danilo
Write the RTF buffer directly, without PeekS()/UTF8 conversion:

Code: Select all

Procedure SaveRTFBuffer(gadget, filename.s)
    Protected range.NSRange, result, length, theData, file
    range\location = 0
    range\length   = Len(GetGadgetText(gadget))
    CocoaMessage(@nsData,GadgetID(gadget),"RTFFromRange:@",@range)
    If nsData
        length  = CocoaMessage(0,nsData,"length")
        theData = CocoaMessage(0,nsData,"bytes")
        If theData
            file = CreateFile(#PB_Any,filename)
            If file
                WriteData(file,theData,length)
                CloseFile(file)
                result = #True
            EndIf
        EndIf
    EndIf
    ProcedureReturn result    
EndProcedure
EDIT: Thanks wilbert, textStorage.length was what I was looking for to replace the ugly "length = Len(GetGadgetText(gadget))". :)

Re: rtf files

Posted: Wed Jul 09, 2014 7:30 pm
by spacebuddy
I notice one thing with EditorGadget, it has no undo feature. If I delete text then use Command Z to undo it not work.

Is there a special flag to turn this on?

Re: rtf files

Posted: Wed Jul 09, 2014 7:37 pm
by Danilo

Code: Select all

    CocoaMessage(0,GadgetID(0),"setAllowsUndo:",#YES) ; allow UNDO
:)

Re: rtf files

Posted: Wed Jul 09, 2014 11:04 pm
by spacebuddy
yes, that work :D

My editor is nearly ready to use

One thing I did notice, if I right click and select "Import" image then try to save this, the image not included? :shock:

Re: rtf files

Posted: Fri Jul 25, 2014 7:39 pm
by spacebuddy
I have been playing with the EditorGadget saving to .rtf format. Now I am trying to detect if there is an attachment (picture) in the EditorGadGet. I looked at the NSText Class reference
and could not find anything. Maybe I am missing something? :shock:

Re: rtf files

Posted: Fri Jul 25, 2014 8:19 pm
by wilbert
You need the textStorage property of the EditorGadget.
On the textStorage object, you can use the methods found in the NSAttributedString AppKit Additions Reference.
https://developer.apple.com/library/mac ... rence.html

Re: rtf files

Posted: Fri Jul 25, 2014 10:49 pm
by spacebuddy
Thanks Wilbert :D

Re: rtf files

Posted: Sun Sep 22, 2019 3:35 am
by BarryG
[Deleted, answer given in another post]

Re: rtf files

Posted: Sun Sep 22, 2019 10:52 am
by C87
As an aside on RTF files ; they cannot contain a virus load. :D