beetlebug

ztk::win::fileassoc

Makes this program a handler of a file extension for the user, or takes the handler back.

Synopsis

ztk::win::fileassoc add EXTENSION ?-name TEXT? ?-args TEXT? ?-icon FILE? ?-iconindex N?
ztk::win::fileassoc remove EXTENSION

Arguments

Name Type Default Meaning
add EXTENSION subcommand Puts this program in the "Open with" list of the extension. The shell starts the program with the words of -args and then the path of the file. The default program that the user chose stays.
remove EXTENSION subcommand Removes the handler of this program.
EXTENSION text A dot and then letters, digits, +, -, or _, such as .note.
-name text EXT file The name of the file type. EXT is the extension with no dot.
-args text empty The arguments of the program before the path of the file.
-icon file this program The file with the icon of such a file.
-iconindex integer 0 The index of the icon in -icon.

The command writes HKEY_CURRENT_USER\Software\Classes, with no rights of an administrator. The keys name the path of the program, so call add again after the program moves. The values stay until remove.

Return value

An empty string.

Errors

Error When
ztk::win::fileassoc is only on Windows A backend other than Windows. The error code is ZTK WIN UNAVAILABLE.
the registry refused the file association A write to the registry failed. The error code is ZTK WIN FILEASSOC.
unknown subcommand "X": must be add or remove An unknown subcommand.
bad extension "X": must be a dot and then letters, digits, "+", "-", or "_" A bad extension.
bad option "X": must be -args, -icon, -iconindex, or -name An unknown option.
wrong # args: should be "ztk::win::fileassoc remove extension" remove with options.

Platforms

Platform Works
Windows Yes
macOS No: ZTK WIN UNAVAILABLE
Linux, Wayland No: ZTK WIN UNAVAILABLE
Linux, X11 No: ZTK WIN UNAVAILABLE
Web No: ZTK WIN UNAVAILABLE
iOS, Android No: ZTK WIN UNAVAILABLE
Headless No: ZTK WIN UNAVAILABLE

Example

if {[ztk::has win]} {
    ztk::win::fileassoc add .note -name "Note file" -args --open
    ztk::win::fileassoc remove .note
}

See also