Application:get: Difference between revisions

From GiderosMobile
No edit summary
 
(25 intermediate revisions by 2 users not shown)
Line 5: Line 5:
=== Description ===
=== Description ===
Returns the state of the desktop setting provided as string parameter.
Returns the state of the desktop setting provided as string parameter.
<source lang="lua">
<syntaxhighlight lang="lua">
varies = application:get(setting)
varies = application:get(setting)
</source>
</syntaxhighlight>


=== Parameters ===
=== Parameters ===
Line 15: Line 15:
'''Returns''' (varies) the value(s) of the setting
'''Returns''' (varies) the value(s) of the setting


=== '''Android''' ===
=== '''Compatibility table''' ===
Added in Gideros 2020.4:
{| class="wikitable" style="text-align: center;"
<source lang="lua">
!!!QT!!Win32!!UWP!!Android!!Apple MacOS!!Apple iOS!!Apple tvOS!!Linux!!HTML
application:get("batteryLevel")
|-
</source>
|batteryLevel||||||||X||||X||||||
|-
|clipboard||X||X||||||||||||X||
|-
|commandLine||X||X||||||||||||X||
|-
|currentUrl||||||||||||||||||X
|-
|cursorPosition||X||X||||||||||||X||
|-
|directory||X||X||||||||||||X||
|-
|documentDirectory||X||X||||||||||||X||
|-
|openDirectoryDialog||X||X||X||||X||||||||
|-
|openFileDialog||X||X||X||||X||||||||
|-
|pathfileexists||X||X||||||||||||X||
|-
|saveFileDialog||X||X||X||||X||||||||
|-
|screenSize||X||X||X||||||||||X||X
|-
|temporaryDirectory||X||X||||||||||||X||
|-
|windowPosition||X||X||||||||||||X||
|-
|windowSize||X||X||||||||||||X||
|-
|windowTitle||X||X||||||||||||||X
|}


=== Examples ===
=== Examples ===
'''Various examples'''
<source lang="lua">
print(application:get("windowTitle"))
print(application:get("documentDirectory"))
print(application:get("temporaryDirectory"))
print("\n")
-- get the user download folder path
print(application:get("directory", "download")) -- prints C:/Users/xxx/Downloads
</source>
'''Prints a list of all available settings'''
'''Prints a list of all available settings'''
<source lang="lua">
<syntaxhighlight lang="lua">
print(application:get("help"))
print(application:get("help"))
--[[
--[[
Line 70: Line 90:
0
0
]]
]]
</source>
</syntaxhighlight>
 
'''Retrieves the whole command line'''
<syntaxhighlight lang="lua">
-- myapp.exe -param1 -param2
application:get("commandLine")
</syntaxhighlight>
 
'''Various examples'''
<syntaxhighlight lang="lua">
print(application:get("windowTitle"))
print(application:get("documentDirectory"))
print(application:get("temporaryDirectory"))
 
-- get screen size
screenwidth, screenheight = application:get("screenSize") -- the actual user screen size!
myappleft, myapptop, myappright, myappbot = application:getLogicalBounds()
myappwidth, myappheight = myappright - myappleft, myappbot - myapptop
application:set("windowPosition", (screenwidth - myappwidth)/2, (screenheight - myappheight)/2) -- center the app
application:set("windowTitle", "MY TITLE")
 
-- get the user download folder path
print(application:get("directory", "download")) -- prints c:/Users/xxx/Downloads
 
-- save a RenderTarget
local sep = "\\" -- for Win32
if Qt then sep = "/" end -- for Qt
rt:save(application:get("directory", "pictures")..sep.."myimage.png")
</syntaxhighlight>
 
'''File dialog open directory'''
<syntaxhighlight lang="lua">
local path = application:get("openDirectoryDialog", "Title", "c:/tmp/") -- "title", "path"
local path = application:get("openDirectoryDialog", "Title", "c:\\tmp\\") -- "title", "path" Win32
print(path) -- on cancel, returns nil for Win32 and "" for other systems
</syntaxhighlight>


'''File dialog to open a file of type .txt or .png'''
'''File dialog open file'''
<source lang="lua">
<syntaxhighlight lang="lua">
local path = application:get(
local path = application:get("openFileDialog", "My Title", "c:\\", "Images (*.png *.jp*g);; 3D (*.obj *.fbx)")
-- "openFileDialog", "Title|C:/tmp/|Text (*.txt);; Image (*.png)") -- "title|path|extensions" WILL BE DEPRECATED IN 2022.10
print(path) -- on cancel, returns nil for Win32 and "" for other systems
"openFileDialog", "Title", "C:/tmp/", "Text (*.txt);; Image (*.png)") -- "title", "path", "extensions" NEW WAY
print(path)
</source>


'''File dialog to save a file as .txt or .jpg'''
-- file dialogs on Win32 should use a delay Timer.delayedCall or Core.asyncCall
<source lang="lua">
self.tiled_ui.btnBrowse:addEventListener("clicked", function()
Core.asyncCall(self.browse, self)
end)
</syntaxhighlight>
 
'''File dialog save file'''
<syntaxhighlight lang="lua">
-- first we choose the destination
-- first we choose the destination
local path = application:get(
local path = application:get("saveFileDialog", "Title", "c:/tmp/", "Text (*.txt);; Image (*.jpg)") -- "title", "path", "extensions"
-- "saveFileDialog", "Title|C:/tmp/|Text (*.txt);; Image (*.jpg)") -- "title|path|extensions" WILL BE DEPRECATED IN 2022.10
-- and we save some data to it
"saveFileDialog", "Title", "C:/tmp/", "Text (*.txt);; Image (*.jpg)") -- "title", "path", "extensions" NEW WAY
if path ~= "" then -- on cancel, returns nil for Win32 and "" for other systems
if path ~= 0 then -- and we save some data to it
local srcf = io.open("mytext.txt", "rb")
local srcf = io.open("mytext.txt", "rb")
local dstf = io.open(path, "wb")
local dstf = io.open(path, "wb")
local size = 2^13 -- good buffer size (8K)
local size = 2^13 -- good buffer size (8K)
while true do local block = srcf:read(size) if not block then break end dstf:write(block) end
while true do
local block = srcf:read(size)
if not block then break end
dstf:write(block)
end
srcf:close()
srcf:close()
dstf:close()
dstf:close()
end
end
</source>
</syntaxhighlight>
 
'''Path File Exists'''
<syntaxhighlight lang="lua">
-- Path File Exists for Windows Qt
-- tests folder path (nil=doesn't exist, 1=exists)
if application:get("pathfileexists", application:get("directory", "pictures").."/Folder01") == nil then
-- application:set("mkDir", application:get("directory", "pictures"), "Folder01") -- mkDir doesn't work for win Qt :-(
end
 
-- Path File Exists for Win32
-- tests folder path (nil=doesn't exist, 1=exists)
if application:get("pathfileexists", application:get("directory", "pictures").."\\Folder01") == nil then
-- create it
application:set("mkDir", application:get("directory", "pictures").."\\Folder01")
end
</syntaxhighlight>


'''File dialog to open a directory'''
'''Bonus'''
<source lang="lua">
<syntaxhighlight lang="lua">
--local path = application:get("openDirectoryDialog", "Title|C:/tmp/") -- "title|path" WILL BE DEPRECATED IN 2022.10
-- to open a directory
local path = application:get("openDirectoryDialog", "Title", "C:/tmp/") -- "title", "path", "extensions" NEW WAY
os.execute("start "..application:get("directory", "pictures"))
print(path)
-- to open a sub directory
</source>
local sep = "\\" -- for Win32
--local sep = "/" -- for Qt
os.execute("start "..application:get("directory", "pictures")..sep.."myfolder")
</syntaxhighlight>


{{Application}}
{{Application}}

Latest revision as of 08:35, 27 September 2026

Available since: Gideros 2015.7
Class: Application

Description

Returns the state of the desktop setting provided as string parameter.

varies = application:get(setting)

Parameters

setting (string) the name of the desktop setting

Return values

Returns (varies) the value(s) of the setting

Compatibility table

QT Win32 UWP Android Apple MacOS Apple iOS Apple tvOS Linux HTML
batteryLevel X X
clipboard X X X
commandLine X X X
currentUrl X
cursorPosition X X X
directory X X X
documentDirectory X X X
openDirectoryDialog X X X X
openFileDialog X X X X
pathfileexists X X X
saveFileDialog X X X X
screenSize X X X X X
temporaryDirectory X X X
windowPosition X X X
windowSize X X X
windowTitle X X X

Examples

Prints a list of all available settings

print(application:get("help"))
--[[
	Accepted value for Desktop's application:get()
	- [x,y] windowPosition
	- [w,h] windowSize
	- [w,h] screenSize
	- [x,y] cursorPosition
	- [text] clipboard
	- [text] windowTitle
	- [path] directory(where//help)
	- [path] openDirectoryDialog(title|path//help)
	- [path] openFileDialog(title|path|extensions//help)
	- [path] saveFileDialog(title|path|extensions//help)
	- [path] documentDirectory
	- [path] temporaryDirectory
	0
]]
print("\n")

print(application:get("directory", "help"))
--[[
	Accepted value for directory :
	- executable
	- document
	- desktop
	- temporary
	- data
	- music
	- movies
	- pictures
	- cache
	- download
	- home
	0
]]

Retrieves the whole command line

-- myapp.exe -param1 -param2
application:get("commandLine")

Various examples

print(application:get("windowTitle"))
print(application:get("documentDirectory"))
print(application:get("temporaryDirectory"))

-- get screen size
screenwidth, screenheight = application:get("screenSize") -- the actual user screen size!
myappleft, myapptop, myappright, myappbot = application:getLogicalBounds()
myappwidth, myappheight = myappright - myappleft, myappbot - myapptop
application:set("windowPosition", (screenwidth - myappwidth)/2, (screenheight - myappheight)/2) -- center the app
application:set("windowTitle", "MY TITLE")

-- get the user download folder path
print(application:get("directory", "download")) -- prints c:/Users/xxx/Downloads

-- save a RenderTarget
local sep = "\\" -- for Win32
if Qt then sep = "/" end -- for Qt
rt:save(application:get("directory", "pictures")..sep.."myimage.png")

File dialog open directory

local path = application:get("openDirectoryDialog", "Title", "c:/tmp/") -- "title", "path"
local path = application:get("openDirectoryDialog", "Title", "c:\\tmp\\") -- "title", "path" Win32
print(path) -- on cancel, returns nil for Win32 and "" for other systems

File dialog open file

local path = application:get("openFileDialog", "My Title", "c:\\", "Images (*.png *.jp*g);; 3D (*.obj *.fbx)")
print(path) -- on cancel, returns nil for Win32 and "" for other systems

-- file dialogs on Win32 should use a delay Timer.delayedCall or Core.asyncCall
self.tiled_ui.btnBrowse:addEventListener("clicked", function()
	Core.asyncCall(self.browse, self)
end)

File dialog save file

-- first we choose the destination
local path = application:get("saveFileDialog", "Title", "c:/tmp/", "Text (*.txt);; Image (*.jpg)") -- "title", "path", "extensions"
-- and we save some data to it
if path ~= "" then -- on cancel, returns nil for Win32 and "" for other systems
	local srcf = io.open("mytext.txt", "rb")
	local dstf = io.open(path, "wb")
	local size = 2^13 -- good buffer size (8K)
	while true do
		local block = srcf:read(size)
		if not block then break end
		dstf:write(block)
	end
	srcf:close()
	dstf:close()
end

Path File Exists

-- Path File Exists for Windows Qt
-- tests folder path (nil=doesn't exist, 1=exists)
if application:get("pathfileexists", application:get("directory", "pictures").."/Folder01") == nil then
--	application:set("mkDir", application:get("directory", "pictures"), "Folder01") -- mkDir doesn't work for win Qt :-(
end

-- Path File Exists for Win32
-- tests folder path (nil=doesn't exist, 1=exists)
if application:get("pathfileexists", application:get("directory", "pictures").."\\Folder01") == nil then
	-- create it
	application:set("mkDir", application:get("directory", "pictures").."\\Folder01")
end

Bonus

-- to open a directory
os.execute("start "..application:get("directory", "pictures"))
-- to open a sub directory
local sep = "\\" -- for Win32
--local sep = "/" -- for Qt
os.execute("start "..application:get("directory", "pictures")..sep.."myfolder")