B4J Tutorial Associating files with your app

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:
B4X:
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:
B4X:
[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.
 

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.

B4X:
[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                               ;<----------------------
 
Top