B4J Tutorial Associating files with your app

Discussion in 'B4J Tutorials' started by Erel, Jun 23, 2019.

  1. Erel

    Erel Administrator Staff Member Licensed User

    Follow these steps to associate files, based on their extension, with your app. The user will be able to double click on a file and your app will be opened with the selected file.

    Step 1: Make sure to use Java 11 with B4JPackager11 v1.10+.
    Step 2: Add the code to handle the selected file.
    Example:
    Code:
    Sub AppStart (Form1 As Form, Args() As String)
       MainForm = Form1
       MainForm.RootPane.LoadLayout(
    "1")
       MainForm.Show
       
    If Args.Length > 0 Then
           
    Try
               
    If File.Exists(Args(0), ""Then 'don't assume that it is a valid path
                   ListView1.Items.AddAll(File.ReadList(Args(0), ""))
               
    End If
           
    Catch
               
    Log(LastException)
           
    End Try
       
    End If
    End Sub
    Step 3: You need to modify InstallerScript-Template.iss file that is created when you run B4JPackager11.
    The following code should be added:
    Code:
    [Registry]
    Root: HKCR; SubKey: .lst; ValueType: 
    string; ValueData: My List Files; Flags: uninsdeletekey noerror; Tasks: ; Languages:
    Root: HKCR; SubKey: My 
    List Files; ValueType: string; ValueData: My List Files; Flags: uninsdeletekey noerror
    Root: HKCR; SubKey: My 
    List Files\Shell\Open\Command; ValueType: string; ValueData: """{app}\{#MyAppExeName}"" ""%1"""; Flags: uninsdeletevalue noerror
    Root: HKCR; Subkey: My 
    List Files\DefaultIcon; ValueType: string; ValueData: {app}\{#MyAppExeName},0; Flags: uninsdeletevalue noerror
    It will associate files with .lst extension with your app. The file description will be My List Files.

    The template file will be recreated every time that you run B4JPackager11. Make sure to copy it outside of the temp folder.
     
    jmon, Chris2, AnandGupta and 6 others like this.
  2. Erel

    Erel Administrator Staff Member Licensed User

  3. Peter Simpson

    Peter Simpson Expert Licensed User

    Absolutely brilliant @Erel, this is just what the doctor ordered and just in the nick of time too :)

    Thank you...
     
    Erel and DonManfred like this.
  4. Chris2

    Chris2 Member Licensed User

    I've found this useful...
    Adding ChangesAssociations=yes to the [Setup] area of the Inno Setup .iss script forces Explorer to refresh its file extensions so that the icon you specify will be shown on the associated files without a logoff/restart being necessary.

    Code:
    [Setup]
    ; NOTE: The value of AppId uniquely identifies this 
    application.
    Do not use the same AppId value in installers for other applications.
    ; (
    To generate a new GUID, click Tools | Generate GUID inside the IDE.)
    AppId={
    #MyAppID}
    AppName={
    #MyAppName}
    AppVersion={
    #MyAppVersion}
    ;AppVerName={
    #MyAppName} {#MyAppVersion}
    AppPublisher={
    #MyAppPublisher}
    DefaultDirName={pf
    }\{#MyAppName}
    DefaultGroupName={#MyAppName}
    OutputBaseFilename=Setup
    OutputDir=.
    Compression=lzma
    SolidCompression=yes
    SetupIconFile=icon.ico
    ArchitecturesAllowed=x64
    ArchitecturesInstallIn64BitMode=x64
    ChangesAssociations=yes                               ;<----------------------
     
    Erel likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice