Skip to content

bug of hs.dialog.chooseFileOrFolder, it should return array-like table. #3862

@entropyMaker

Description

@entropyMaker

when this function is called and some files are chosen in the dialog, the file paths should be printed to the console, but it actually print nothing:

function printAllChosenFiles()
  local t = hs.dialog.chooseFileOrFolder("", "", true, false, true)
  for i = 1, #t do
    print(t[i])
  end
end

I checked the source code, it's because of this line:
https://github.com/Hammerspoon/hammerspoon/blob/master/extensions/dialog/libdialog.m#L426
lua_pushstring(L,[[url path] UTF8String]); lua_setfield(L, -2, [[NSString stringWithFormat:@"%i", count] UTF8String]);
here it set the file paths as values with string keys like "1", "2", "3" instead of integer keys 1, 2, 3.

as the document of this function says it returns "The selected files in a table or nil if cancel was pressed.", I think comparing to a table with string "1", "2", "3" keys, an array-like table is more close to the document, users are more likely to expect an array-like table, and an array-like table is more convenient for users (like iterating it).

obviously the fixing is easy, just replace lua_setfield(L, -2, [[NSString stringWithFormat:@"%i", count] UTF8String]); with lua_seti(L, -2, count);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions