Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Open file using Open Dialog (inspirated by Lua File System)
Goto page 1, 2  Next
 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
Corroder
Grandmaster Cheater Supreme
Reputation: 75

Joined: 10 Apr 2015
Posts: 1667

PostPosted: Wed Jan 18, 2017 9:09 pm    Post subject: Open file using Open Dialog (inspirated by Lua File System) Reply with quote

Hi,

I try to make a file browser / opener using CE open dialog :

Code:
file_array = {
{fExt=".txt",fAccess="notepad.exe"},
{fExt=".jpg",fAccess="MSPaint.exe"},
{fExt=".png",fAccess="MSPaint.exe"},
{fExt=".doc",fAccess="winword.exe"},
{fExt=".xls",fAccess="excel.exe"}
}

function CEButton1Click(sender)
  dialog = createOpenDialog()
  dialog.Filename = 'file name'
  dialog.DefaultExt = ".ct"
  dialog.Filter = "file name (*.ct)|*.ct"
  dialog.FilterIndex = 1
  dialog.Options = '[ofEnableSizing]'
  if dialog.execute() then
    file=io.open(dialog.Filename, "r")
    file:close()
    file_ext =  -------------------- get file extension
    index = file_array[index]
     if index == nil or file_ext ~= index.fExt then  ------ look up from table array
       showMessage("Can't recognize file type")
    shellExecute(index.fAccess/Filename)
    openProcess(index.fAccess/Filename)
  end
end


How to improve script above to open file according file extension and process by correct windows application when open it ?

Need this because not want using "require" module such as LFS.

Thanks and regards
Back to top
View user's profile Send private message
Zanzer
I post too much
Reputation: 126

Joined: 09 Jun 2013
Posts: 3278

PostPosted: Thu Jan 19, 2017 7:41 am    Post subject: Reply with quote

Code:
shellExecute("cmd", [[/c start "" "E:/zanzer.txt"]])
Back to top
View user's profile Send private message
Corroder
Grandmaster Cheater Supreme
Reputation: 75

Joined: 10 Apr 2015
Posts: 1667

PostPosted: Thu Jan 19, 2017 9:29 pm    Post subject: Reply with quote

thanks Zanzer,

Code:
shellExecute("cmd", [[/c start "" "E:/zanzer.txt"]])


I can't find the way to get return filename when doing dialog.execute() to substitute this "E:/zanzer.txt".

So, maybe is better add textbox to give user input their path/filename and then get it as a variable to use with shellExecute.

Then the openFileDialog() function just use for user to preview or locating where is their file on the computer.

Regards
Back to top
View user's profile Send private message
Corroder
Grandmaster Cheater Supreme
Reputation: 75

Joined: 10 Apr 2015
Posts: 1667

PostPosted: Fri Jan 20, 2017 7:49 am    Post subject: Reply with quote

How to use lookup from table array ?

Code:
apps = {
{ext="txt", app="notepad.exe"},
{ext="xls", app="EXCEL.exe"},
{ext="doc", app="MSWORD.exe"}
}

index = "txt"
index = apps[index]
if index == nil then
print("Not in the table")
else
print(tostring(index.app))
end


--- result always return "Not in the table"

Thanks
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 198

Joined: 25 Jan 2006
Posts: 8516
Location: 127.0.0.1

PostPosted: Fri Jan 20, 2017 1:56 pm    Post subject: Reply with quote

You are not using a numeric index in your table, so you need to loop through the k/v pair and locate it that way.

Code:

apps = {
{ext="txt", app="notepad.exe"},
{ext="xls", app="EXCEL.exe"},
{ext="doc", app="MSWORD.exe"}
}

for k, v in pairs(apps) do
    if (v.ext == "doc") then
        print(v.app)
    end
end

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
mgr.inz.Player
I post too much
Reputation: 218

Joined: 07 Nov 2008
Posts: 4438
Location: W kraju nad Wisla. UTC+01:00

PostPosted: Fri Jan 20, 2017 3:59 pm    Post subject: Reply with quote

@Corroder, Actually, this table is indexed. You are using table with "index/value" pairs.

And this:
Code:
apps = {
{ext="txt", app="notepad.exe"},
{ext="xls", app="EXCEL.exe"},
{ext="doc", app="MSWORD.exe"}
}


is the same as this:
Code:
apps = {
[1]={ext="txt", app="notepad.exe"},
[2]={ext="xls", app="EXCEL.exe"},
[3]={ext="doc", app="MSWORD.exe"}
}


To search something in this table, you will need "for loop". And to keep the order when iterating, you should use ipair.




If you don't want to lookup through whole table, use table with "key/value" pairs.
Code:
apps = {
["txt"]="notepad.exe",
["xls"]="EXCEL.exe",
["doc"]="MSWORD.exe"
}

print( apps.doc )
>MSWORD.exe

print( apps["doc"] )
>MSWORD.exe




Read whole http://lua-users.org/wiki/TablesTutorial


 

_________________
Back to top
View user's profile Send private message MSN Messenger
Corroder
Grandmaster Cheater Supreme
Reputation: 75

Joined: 10 Apr 2015
Posts: 1667

PostPosted: Fri Jan 20, 2017 7:42 pm    Post subject: Reply with quote

Thanks atom0s,

Code:
for k, v in pairs(apps) do
    if (v.ext == "doc") then
        print(v.app)
    end
end


it work and i have put it to my script :

Code:
function GetFileExtension(path)
 local str = path
 local temp = ""
 local result = "."
 for i = str:len(), 1, -1 do
 if str:sub(i,i) ~= "." then
 temp = temp..str:sub(i,i)
 else
 break
 end
 end
 for j = temp:len(), 1, -1 do
 result = result..temp:sub(j,j)
 end
 return result
end

function file_check(file_name)
 local file_found=io.open(file_name, "r")
 if file_found==nil then
 file_found=showMessage(file_name .. " ... Error - File Not Found")
 else
 file_found=showMessage(file_name .. " ... File Found")
 end
 return file_found
end

apps = {
{ext=".txt", app="notepad.exe"},
{ext=".xlsx", app="EXCEL.exe"},
{ext=".doc", app="WINWORD.exe"}
}

function extLookup(ext)
 for k, v in pairs(apps) do
  if (v.ext == ext) then
  print(v.app)
  shellExecute(v.app, path);end
  end
end

function DoOpenFile()
 path = UDF1.CEEdit1.Text
 if path == "" then
  showMessage("Enter full path and file name you want to open. If you not sure, use explorer to check it.")
  return
 end
  file_check(path)
  print(path)
  ext = GetFileExtension(path)
  print(ext)
  extLookup(ext)
end


x = 0

function DoExplorer()  --- Make Button2 as ON/OFF
 if(x == 0)then
  shellExecute'explorer.exe'
  openProcess'explorer.exe'
  x = 1
 else
  shellExecute("taskkill", "/IM explorer.exe /F")
  x = 0
 end
end

function closeTrainer()
 closeCE()
 return caFree
 end

UDF1.CEButton1.onClick = DoOpenFile
UDF1.CEButton2.onClick = DoExplorer
UDF1.CEButton3.onClick = closeTrainer
form_onClose(UDF1, closeTrainer)
UDF1:show()


thanks also mgr.inz.Player
i have read : http://lua-users.org/wiki/TablesTutorial
and also search for some references from other site, but not found something simple like your explained. With your explain, it's more easier for me to understand.

I did worked with table array before, but i used it for look up a variable choose via a Combobox and I am not facing problem while to do it because it using a numeric index in the table, like atom0s said.

Got everything good and growing my knowledge about lua that can use in future, even I am learn everything by self-taught.

Thanks and regards
Back to top
View user's profile Send private message
panraven
Grandmaster Cheater
Reputation: 55

Joined: 01 Oct 2008
Posts: 941

PostPosted: Fri Jan 20, 2017 8:58 pm    Post subject: Reply with quote

Let's make mgr.inz.Player's [key=extension] idea more complicated (add template on formatting command and param to be executed by shellExecute)
Code:


-- sample template format: extension -> app cmd, app param
defaultExt2App = {
  txt = {'notepad', '"^F"',true}, -- 3rd parameter 'true' tell the directory has to be exist
  bat = {'cmd','/c "^F"'},
  cmd = {'cmd','/c "^F"'},
  [''] = {'explorer','/select,"^F"'}, -- handle if extension is empty string
}


function pathSplit(p) -- thanks from Zanzer's hint http://forum.cheatengine.org/viewtopic.php?p=5714297#5714297
  local dir,name,ext
  if p:find"%.[^:/\\]-$" then
    dir,name,ext = p:match"^(.-)([^:/\\]-)%.+([^%.:/\\]-)$"
  else
    dir,name,ext = p:match"^(.-)([^:/\\]*)$"
  end
  ext = ext or ''
  if ext~='' and name=='' then name,ext = '.'..ext,'' end
  return dir,name,ext
end

function dirExist(dir, testWritable)
  dir = dir:match"^(.-)[/\\]*$"..'/'
  if not testWritable then
    return nil~=getDirectoryList(dir)
  else
    local testNUL = io.open(dir..'nul','wb')
    return testNUL and testNUL:close() or false
  end
end

function file2RunApp(file, Ext2app)
  local ext2app = {}

  for k,v in pairs(Ext2app or {}) do if not ext2app[k] then ext2app[k]=v end end
  for k,v in pairs(defaultExt2App) do if not ext2app[k] then ext2app[k]=v end end

  local dir,name,ext = pathSplit(file)

  local appParam = ext2app[ext:lower()]
  if not appParam then return nil,'Application Not known: '..tostring(file)end

  local cmd,param,reqDirExist = appParam[1],appParam[2],appParam[3]==true

  if reqDirExist and not getDirectoryList(dir) then
    error("require directory not exist.",2)
  end

  local ctx = cmd and {D = dir, N = name, E = ext, F = file }

  cmd = cmd and cmd:gsub("%^([DNEF])",ctx)
  param = param and param:gsub("%^([DNEF])",ctx) or nil
  return cmd and pcall(shellExecute, cmd, param)
end

-- test

local target = "lsdir.cmd"
local testcmd = io.open(target,'wb')
if testcmd then
  testcmd:write[[
  @echo off
  dir *.* /w/s
  pause
  ]]
  testcmd:close()
end
if os.rename(target,target) then--file exist?
  print('try excuting :'..target)
  print('ok ?  '..tostring( file2RunApp(target) ))
end


usage:
Code:

-- support functions
    fileGetExt (f)  -- get the file extension
    fileGetName (f)  -- get the file base name excluding extension
    fileSplit (f, updir) -- splite the file into diretory, name, ext, and return itself concat with a possible parent directory when f is relative

-- main function
    file2RunApp(f, Ext2App)
      - f : the input file with diretory as string
      - Ext2App : a table to specify how to format command and parameter accoding to file extension, use defaultExt2App if omitted
      - return true if an corresponding shellExecute can be run

-- template to format command and parameters:
  In the template, use ^F = full file name, ^D = diretory part, ^N = base name, ^E = extension, to substitute. See above defaultExt2App.
  The extension as key has to make lower case.

_________________
- Retarded.


Last edited by panraven on Sun Jan 22, 2017 7:16 pm; edited 8 times in total
Back to top
View user's profile Send private message
Zanzer
I post too much
Reputation: 126

Joined: 09 Jun 2013
Posts: 3278

PostPosted: Fri Jan 20, 2017 9:41 pm    Post subject: Reply with quote

Code:
local dialog = createOpenDialog(nil)
dialog.options = "[ofAllowMultiSelect]"
local result = dialog.execute()
if result then
  local files = dialog.files
  for i = 0, files.count - 1 do
    shellExecute("cmd", [[/c start "" "]]..files[i]..[["]])
  end
end
dialog.destroy()
Back to top
View user's profile Send private message
panraven
Grandmaster Cheater
Reputation: 55

Joined: 01 Oct 2008
Posts: 941

PostPosted: Fri Jan 20, 2017 10:17 pm    Post subject: Reply with quote

Zanzer wrote:
Code:
local dialog = createOpenDialog(nil)
dialog.options = "[ofAllowMultiSelect]"
local result = dialog.execute()
if result then
  local files = dialog.files
  for i = 0, files.count - 1 do
    shellExecute("cmd", [[/c start "" "]]..files[i]..[["]])
  end
end
dialog.destroy()


Zanzer's 'start' solution is simple and elegant, but I got "Not enough storage is available to process this command" on running *.cmd or *.bat which prevent the console to close itself.
Not known if it only my machine setting.

bye~



cecmdstart.jpg
 Description:
 Filesize:  37.44 KB
 Viewed:  16286 Time(s)

cecmdstart.jpg



_________________
- Retarded.
Back to top
View user's profile Send private message
Corroder
Grandmaster Cheater Supreme
Reputation: 75

Joined: 10 Apr 2015
Posts: 1667

PostPosted: Sat Jan 21, 2017 4:53 am    Post subject: Reply with quote

Zanzer

Code:
local dialog = createOpenDialog(nil)
dialog.options = "[ofAllowMultiSelect]"
local result = dialog.execute()
if result then
  local files = dialog.files
  for i = 0, files.count - 1 do
    shellExecute("cmd", [[/c start "" "]]..files[i]..[["]])
  end
end
dialog.destroy()


Tested today Jan 21. 2017 :
Work good as same as using explorer [Windows 7 Ultimate - 32bit]

Code:
shellExecute("cmd", [[/c start "" "E:/filename.txt"]])
--- also tested with doc, xlsx, jpg, etc

path = UDF1.CEEdit1.Text  --- eq. "E:/filename.txt
shellExecute("cmd", [[/c start "" ", path]])



Tested on Jan 19. 2017 :
File not open and got something windows message about powerShell.
Don't know what is the problem.
But when testing again today Jan 21, with shellExecute("cmd", [[/c start "" "E:/filename.txt"]]), it work with no issue.

Panraven,
I still try to learn and try to understand about your script logic and trying to implement that script.

Thanks to all of you
Back to top
View user's profile Send private message
panraven
Grandmaster Cheater
Reputation: 55

Joined: 01 Oct 2008
Posts: 941

PostPosted: Sat Jan 21, 2017 1:45 pm    Post subject: Reply with quote

No problem, at least you said you have tried.
I'll try stick to what I learnt from the past too.
bye~

_________________
- Retarded.
Back to top
View user's profile Send private message
Corroder
Grandmaster Cheater Supreme
Reputation: 75

Joined: 10 Apr 2015
Posts: 1667

PostPosted: Sat Jan 21, 2017 10:43 pm    Post subject: Reply with quote

Panraven bro,

Test your functions above :

Code:
file = "E:test.txt"

print(fileGetExt (file))  -- get the file extension
print(fileGetName (file))  -- get the file base name excluding extension
print(fileSplit (file, updir))
file2RunApp(file,Ext2app)

-- result [[
txt E:test
 E:test
E:test  txt E:test.txt

-- Notepad open correctly while use : file2RunApp(file,Ext2app)
]]

-- again :

file = "E:\test.txt"   -- add \ at directory name

print(fileGetExt (file))    ----> txt E:   est
print(fileGetName (file))  ---->  E:   est
print(fileSplit (file, updir)) ----> E:   est  txt E:   est.txt
file2RunApp(file,Ext2app)  ---> filename, dir name, volume label incorrect / notepad open without open the file given

-- again :

file = "E:\corroder\test.txt"
give error "script Error:[string "-- sample template format: extension -> app c..."] ....  invalid escape sequence near '"E:\c' "

Back to top
View user's profile Send private message
Zanzer
I post too much
Reputation: 126

Joined: 09 Jun 2013
Posts: 3278

PostPosted: Sat Jan 21, 2017 11:00 pm    Post subject: This post has 1 review(s) Reply with quote

Code:
local path = [[c:\temp\test\myfile.txt]]
local dir, name, ext = string.match(path, "(.-)([^\\]-([^%.]+))$")
print(dir)
print(name)
print(ext)
Back to top
View user's profile Send private message
Corroder
Grandmaster Cheater Supreme
Reputation: 75

Joined: 10 Apr 2015
Posts: 1667

PostPosted: Sat Jan 21, 2017 11:43 pm    Post subject: Reply with quote

Work good, thank Zanzer,

Code:
local path = [[c:\temp\test\myfile.txt]]
local dir, name, ext = string.match(path, "(.-)([^\\]-([^%.]+))$")
print(dir)  --> c:\temp\test\
print(name) --> myfile.txt
print(ext)  --> txt

--- Put on a function

file = [[c:\temp\test\myfile.txt]]
function SplitFilename(strFilename)
 return string.match(strFilename, "(.-)([^\\]-([^\\%.]+))$")
end

path,file,extension = SplitFilename(file)
print(path,file,extension) --> c:\temp\test\ myfile.txt txt
print(path) --> c:\temp\test\
print(file) --> myfile.txt
print(extension) --> txt

Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites