Native dialogs library

We are all eagerly awaiting the arrival of native extensions, but there is still some room for hacking using LuaJit FFI in the mean time, at least on desktop.

While working on a pet project I needed access to “Save/Open file”-dialogs, something currently not exposed in the Defold API. This prompted me to hack together a dumb little library called; defold-dialogs.

Important: It uses the FFI module in LuaJit, thus not available on all platforms. Currently only Windows (32bit) and OSX works.

Usage:

  1. Add the library as a dependency in your Defold project. Pick one of the release Zip file URLs from: https://github.com/andsve/defold-dialogs/releases

  2. Currently you need to copy the native_libs folder supplied in the library to the root of your project. (I’m trying to figure out ways to skip this…)

  3. In one of your scripts, add; local dialogs = require "dialogs/dialogs".

  4. When you want to show a dialog, call:

    • “Open file”-dialog:
    print(dialogs.open(".txt,.lua", "c:/PathToDefaultDirectory"))
    
    • “Save file”-dialog:
    print(dialogs.save(".txt,.lua", "c:/PathToDefaultDirectory"))
    
9 Likes

Genius

2 Likes

You can make a file dialog for windows x64

				pathWithFilename=io.popen("cd"):read'*all'
				comm = '"'..string.sub(pathWithFilename, 1,-2)..'\\test.bat"' 
                                --execute bat file calling file dialog
				os.execute(comm)
                               --bat save link file to "\tx.dt"
				f = (io.open(string.sub(pathWithFilename, 1,-2)..'\\tx.dt',"r"))
				file_path = (f:read())
				-- file_path contains a link to a file

the contents of the bat file:
@echo off
SetLocal EnableExtensions

for /f “tokens=2 delims=:” %%i in (‘chcp’) do set sPrevCP=%%i& chcp 1251 >nul
for /f “usebackq delims=” %%i in (mshta.exe "about:<input type=file id=F><script>F.click();new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).WriteLine(F.value);close();</script>" ^|more) do (
set file=%%i
)
chcp %sPrevCP% >nul
if defined file (
echo %file%>tx.dt
)

3 Likes