Share My Creation B4J PlugIn: What If?

Hi there

WeCouldPlugIn.gif


The fact that on the Files tab, one is able to open a file with an external program got me thinking this morning. What if I can write a B4J app, that can get passed the file name I selected, then process something inside the file and save it back.

So I wrote a small script for something that I do almost often. Add a new page to my existing B4J app. To test my theory I created a form with a text area and created a UI app called NewPage...

B4X:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private txtData As TextArea
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.Initialize("frm", 800, 800)
    MainForm.RootPane.LoadLayout("vMain")
    MainForm.Show
    
    If Args.Length = 0 Then
        Log("File Name is missing.")
        Return
        'ExitApplication
    End If
    'get the file name
    Dim fName As String = Args(0)
    jMash.InformUser("File Name",fName)
    'read the contents
    Dim contents As String = File.ReadString("",fName)
    txtData.Text = contents
    
    'New Page Content...
    Dim npc As StringBuilder
    npc.initialize
    npc.append("Sub Process_Globals").Append(CRLF)
    npc.Append("Private fx As JFX").Append(CRLF)
    npc.Append("Private frm As Form").Append(CRLF)
    npc.Append("End Sub").append(CRLF).Append(CRLF)
    npc.Append("Sub Close").Append(CRLF)
    npc.Append("frm.close").Append(CRLF)
    npc.Append("End Sub").Append(CRLF)
    npc.Append("Public Sub Show(ParentForm As Form)").Append(CRLF)
    npc.append($"frm.Initialize("frm",400, 550)"$).Append(CRLF)
    npc.append("frm.Resizable = False").Append(CRLF)
    npc.Append($"frm.RootPane.LoadLayout("<Change>")"$).Append(CRLF)
    npc.Append("frm.Title = <Change>").Append(CRLF)
    npc.Append("frm.SetOwner(ParentForm)").Append(CRLF)
    npc.Append("frm.ShowAndWait").append(CRLF)
    npc.Append("End Sub").Append(CRLF)
    txtData.Text = txtData.Text & CRLF & npc.tostring
End Sub

'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

What the code above does is to open the selected file, read its contents, display the contents in a text area in a form after appending the code to Show and Close a form.

After compiling this app, I needed to configure the external viewers..

ConfigureExternal.png


Then I right clicked on the file, selected Open with NewPage and yippiee!! See the gif.

Now, what if the functionality to Open With could be available on the Modules tab? Me thinks the possibilities are endless..
 

Attachments

  • NewPageCode.png
    NewPageCode.png
    38.8 KB · Views: 2,652
Top