Launching an external mac program

Miscellaneous Forums/MacOS X Discussion/Launching an external mac program

Mac users,

Can you guys help with regards to launching an external program via BlitzMax on a Mac?

Using the test code below ...

a) Does a launched program run correctly?

Example: Try launching the BlitzMax IDE and monitoring the status bar of the laucher program.

b) Does the lauched program close immediately afterwards by itself?

c) can you terminate the launched program from the launchers GUI [Terminate application] button?

' Application Launcher

' Simons library
Import PUB.FreeProcess

AppTitle$="Max App Launcher"

' simple gui
Global win:TGadget=CreateWindow(AppTitle$,60,60,180,160,0,9)
Local but1:TGadget=CreateButton("Run applicaton",24,24,114,24,win,1)
Local but2:TGadget=CreateButton("Terminate application",24,56,114,24,win,1)

' variable to handle the launched application
Local myproc:TProcess

' timer for updating the statusbar
Local t:TTimer=CreateTimer(1)

' cross-platform executable extension filter
?Win32
Const ext$="Executable:exe"
?Linux
Const ext$=""
?Mac
Const ext$=""
?


' =====================================

' main loop

Repeat
	' DebugLog CurrentEvent.ToString()
	Select WaitEvent()
		Case EVENT_GADGETACTION
		Select EventSource()
			Case but1
			If AppRunning(myproc)
				Notify StripDir$(myproc.name$)+" still running!"
			Else
				f$=RequestFile$("Choose Executable",ext$)
				myproc=RunApp(f$)
			EndIf
			Case but2
			If AppRunning(myproc)
				myproc.Terminate
			Else
				Notify "No program to terminate"
			EndIf
		End Select
		Case EVENT_TIMERTICK
		UpdateStatusBar myproc
		Case EVENT_WINDOWCLOSE
		Exit
	End Select
Forever

End

' =====================================

' launch an application
Function RunApp:TProcess(app$)
	If app$="" Return Null
    Local proc:TProcess=TProcess.Create(app$,0)
    If Not Proc Notify "Failed to launch "+app$
	Return proc
End Function

' return true if app still running
Function AppRunning(p:TProcess)
	If p<>Null
		If p.Status() Return True
	EndIf
End Function


' update the status bar to reflect 'app running' status
Function UpdateStatusBar(p:TProcess)
	If p=Null
		SetStatusText win,"App not running"
		Return
	EndIf
	If p.Status()
		SetStatusText win,StripDir$(p.name$)+" running"
	Else
		SetStatusText win,"App terminated"
	EndIf
End Function


Doesn't seem to work properly on my 10.3.9 G4 (I get "b." as described above). Pressing "Run application" brings up the file requester, and when I select an app, your program's window says "<name>.app running" at the bottom. But the program doesn't appear, and after a second or two the window message changes to "App terminated." I tried it with Firefox, Calculator, and Chess, starting all three from the Applications folder, as well as trying to run a shortcut to MaxIDE on the desktop (using a standalone, compiled version of your launcher) - the same result for all.

Just to check, I tried your launcher in Win XP and Win 98, and it ran fine (could run/terminate apps through the window). In Ubuntu 5.10 Linux I could run apps, but not terminate them through the window (they terminated OK manually).

Thanks for the in-depth help WendelM.
Do all mac executables end with a '.app' extension?

Glad to help - thank you for the life-saving Framework Assistant. :)

OS X does the goofy extension-hiding that XP does by default. Going into Finder/Preferences/Advanced and turning on "show all extensions" is supposed to make them all show up, but it doesn't for me - only some show .app. But doing a Get Info shows that the ones I checked do have ".app" and doing a search for "app" shows that what seems like all apps have it. So, that's a qualified "yes, they all seem to."

WendellM,

Can you try this modification to the above test code and let me know if it correctly filters out everything except Mac applications? ..
' cross-platform executable extension filter
?Win32
Const ext$="Executable:exe"
?Linux
Const ext$=""
?Mac
Const ext$="Application:app"
?


Yes, it works - though the switch needs to be "?MacOS" rather than "?Mac". Only .app files can be selected (everything else shows up grayed-out), and all the apps seem to show up whether Finder shows the .app extension or not. They still immediately quit if run (as expected, since no code has changed in that regard).

In playing with this, I found out that Mac file requesters don't have the combobox that Windows does for switching among filetypes. So, the current RequestFile example in the help, which allows you to switch among Image Files, Text Files, and All Files in Windows, doesn't allow that in OS X. (That should present no problems for your launcher, since there's presumably no need to allow anything other than .app files to launch - except that shortcuts to applications don't work since they have no extensions).

However, on the Mac all possible extensions in the supplied filter string seem combined into a single list automatically. So, in the RequestFile example, the filter of "Image Files:png,jpg,bmp;Text Files:txt;All Files:*" always shows png, jpg, bmp, and txt files (whereas they're in separate groups in Windows). But, I don't see a way to allow all files to be selected in OS X when such a filter string is used. Files with extensions not in the filter list show up grayed-out in OS X, and they cannot be selected (while they only appear in Win if "All Files" is selected):

Windows XP:


Mac OS X:



I'm submitting this as a bug report.