does lua support a run command?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
jjmafiae
Party member
Posts: 1331
Joined: Tue Jul 24, 2012 8:22 am

does lua support a run command?

Post by jjmafiae »

does lua support some kind of run command :huh:?

to start programs.
User avatar
slime
Solid Snayke
Posts: 3170
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: does lua support a run command?

Post by slime »

User avatar
Jasoco
Inner party member
Posts: 3727
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: does lua support a run command?

Post by Jasoco »

Be careful when using os.* commands though. Löve is cross-platform and should stay that way, but not all OS' have all the same commands. Does os.execute even work on OS X or Linux?

It would be nice if the Löve API could offer a wrapper for us to use when we need to launch things like URL's or open folders. (Within permission reasons of course) For instance, a link in an About/Credits screen that opens up the developer's website/FaceBook/Twitter/MySpace* page. Or one that can open specific folder directories, like how MineCraft has a button to open the Texture Pack folder. At least by providing API's for this stuff we can avoid risking alienating some users when a command ends up being Windows only or something. (Like how the Texture Pack button in Minecraft didn't even work on OS X for years before they fixed it.)

*What's MySpace? It's a website where bands go to talk to eachother.
User avatar
master both
Party member
Posts: 262
Joined: Tue Nov 08, 2011 12:39 am
Location: Chile

Re: does lua support a run command?

Post by master both »

os.execute works on my ubuntu, also in Mari0, on the "select map" option, you can press "m" and the maps packs folder pop up, but i dont know how did he do it
User avatar
Jasoco
Inner party member
Posts: 3727
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: does lua support a run command?

Post by Jasoco »

master both wrote:os.execute works on my ubuntu, also in Mari0, on the "select map" option, you can press "m" and the maps packs folder pop up, but i dont know how did he do it
Well, the Maps Pack folder is inside the "Working Directory" for the game so it'd be simple enough.

It would be harder to execute applications, though I don't think there's any reason we should need to.

But URL's would be nice. Can os.execute execute a URL? It would be a trivial thing to do as every OS has a browser and it would just send the command to open it in the default browser.

I'd still like a proper Löve wrapper API for it though. Part of love.filesystem of course like love.filesystem.openfolder() or love.filesystem.openurl().

Edit: LOL, looking through Mari0's code to find the function for opening the folder and I notice his comment about not using a GUI Library because he's lazy. Personally I programmed my own menu GUI library because I simply didn't want to use anyone elses.

Anyway, here's the code Maurice uses for opening the "save folder":

Code: Select all

function openSaveFolder(subfolder) --By Slime
	local path = love.filesystem.getSaveDirectory()
	path = subfolder and path.."/"..subfolder or path
	
	local cmdstr
	local successval = 0
	
	if os.getenv("WINDIR") then -- lolwindows
		--cmdstr = "Explorer /root,%s"
		if path:match("LOVE") then --hardcoded to fix ISO characters in usernames and made sure release mode doesn't mess anything up -saso
			cmdstr = "Explorer %%appdata%%\\LOVE\\mari0"
		else
			cmdstr = "Explorer %%appdata%%\\mari0"
		end
		path = path:gsub("/", "\\")
		successval = 1
	elseif os.getenv("HOME") then
		if path:match("/Library/Application Support") then -- OSX
			cmdstr = "open \"%s\""
		else -- linux?
			cmdstr = "xdg-open \"%s\""
		end
	end
	
	-- returns true if successfully opened folder
	return cmdstr and os.execute(cmdstr:format(path)) == successval
end
Note how it needs to check the OS using a really roundabout way. So having proper API's for this would be brilliant.

I used a different method that I haven't tested much so I don't even know if it properly works, but it's simpler in its OS check:

Code: Select all

local add = love.filesystem.getAppdataDirectory()
if string.sub(add, -7) == "Support" then
	ACCOUNT_FOR_WIDGETS = 44 --OS X - Menubar (22) + Titlebar (22) (Not including Dock)
	USERS_OS = "osx"
else
	ACCOUNT_FOR_WIDGETS = 70 --Windows 7 Taskbar (40) + Titlebar (30)
	USERS_OS = "win7"
end
Note: The ACCOUNT_FOR_WIDGETS variable is for debugging my game where instead of using the proper "fullscreen" functionality os Löve which has a tendancy to freeze up and not let me get back to my OS like Java and MineCraft do occasionally (Which prevents me from using Fullscreen in MC as well all the time) so instead the window is just resized to take up the entire screen size minus default widget sizes. On OS X this is 44 pixels which is the menubar and titlebar height combined, on Windows it's 70 which includes Windows 7's default small Taskbar height and its overly tall default Titlebar height combined. I don't check for Linux as I don't have it and am hoping by the time my game comes out there will be a proper Löve API for detecting this properly proper.)
User avatar
slime
Solid Snayke
Posts: 3170
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: does lua support a run command?

Post by slime »

You can get the current operating system with the love._os string. It will return "Windows", "OS X", or "Linux". It got added to 0.8.0 at the last minute, just slightly after Mari0 was released IIRC, so that code doesn't have it.
User avatar
Jasoco
Inner party member
Posts: 3727
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: does lua support a run command?

Post by Jasoco »

slime wrote:You can get the current operating system with the love._os string. It will return "Windows", "OS X", or "Linux". It got added to 0.8.0 at the last minute, just slightly after Mari0 was released IIRC, so that code doesn't have it.
Oh, awesome! That's cool. I would have expected something simpler. Is that in the Wiki? Are there any other hidden things like that?

We still should have an API for opening folders and URL's without a bunch of code though. Just for simplicity's sake.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: does lua support a run command?

Post by Robin »

Jasoco wrote:But URL's would be nice. Can os.execute execute a URL? It would be a trivial thing to do as every OS has a browser and it would just send the command to open it in the default browser.
No, it can't. os.execute is basically entering a line in your terminal window. I think in Windows you can do "open http://love2d.org/", and on Linux "xdg-open http://love2d.org/", but I don't know how it is for OS X.

It might be a good idea to add a love.openinbrowser() function or something. It would be disabled by default in SELÖVE, of course.
Help us help you: attach a .love.
User avatar
substitute541
Party member
Posts: 484
Joined: Fri Aug 24, 2012 9:04 am
Location: Southern Leyte, Visayas, Philippines
Contact:

Re: does lua support a run command?

Post by substitute541 »

Tried the open command Robin said in the command prompt, gives me an error saying that "open" is an invalid command.
Currently designing themes for WordPress.

Sometimes lurks around the forum.
User avatar
Jasoco
Inner party member
Posts: 3727
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: does lua support a run command?

Post by Jasoco »

Robin wrote:
Jasoco wrote:But URL's would be nice. Can os.execute execute a URL? It would be a trivial thing to do as every OS has a browser and it would just send the command to open it in the default browser.
No, it can't. os.execute is basically entering a line in your terminal window. I think in Windows you can do "open http://love2d.org/", and on Linux "xdg-open http://love2d.org/", but I don't know how it is for OS X.

It might be a good idea to add a love.openinbrowser() function or something. It would be disabled by default in SELÖVE, of course.
On OS X it's apparently just "open http://www.love2d.org/".

Apparently on Windows (At least 7) it's "start http://www.love2d.org/" instead of "open".

Edit: I can't get this code to work...
...
I get an error saying execute is a nil value. Am I using it wrong?

Edit: Fixed and correct on next page...
Last edited by Jasoco on Fri Apr 05, 2013 3:39 am, edited 1 time in total.
Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests