How to detect whether X11 is installed?

I’ve written a script that runs a unix command (pcl6, which I compiled from source) which requires X11 to run. If I run the script on a system without OS X installed, OS X pops up an error message and asks me to install X11.

Is it possible for AppleScript to determine whether X11 is installed so that I post an error message at the start?

Thanks for any help with this.

(By the way, if I knew how to compile pcl6 so that it doesn’t require X11, I would, but I haven’t figured out how to do it.)

Try this:

try
	tell application "X11" to activate
on error
	display alert "X11 is not installed."
end try

NOTE: This will only tell you if X11 is installed, only if it isn’t installed.

That certainly works, but I was hoping to find a “silent” way of doing it - one that doesn’t open a window and make the user close it. But I think this is a starting point and I’ll continue to work on it.

Deved’s example won’t work because you will be prompted with ‘Where is application X11’ and no error will be trown. The same applies for

get version of application "Address Book"

even if this is silent (without starting up the application) it won’t work because it will still ask the user where x11 is. So to make it all work well you’ll need


try
   set appID to "com.apple.x11"
   tell application id appID to set APPVersion to version
on error errmsg
   set APPVersion to null
end try

if APPversion is null then
return --stop application
end

EDIT I wrote this post before seeing the previous answer. Am about to try it…! Thanks!

Actually, I see that I was asking the question the wrong way. The unix software used by my script doesn’t require the “X1.app” application - it requires an “X1 server” - a software layer that does not necessarily start up an X1.app, but runs invisibly. I think the answer may be that I simply need to test for a specific error message saying that X1 isn’t present. If this turns out to be the case, I’ll post a solution here. But if anyone has a better solution, I’ll be grateful to hear about it.

This works perfectly if Apple’s X11 is installed, but it won’t work if XQuartz is used:

http://xquartz.macosforge.org/trac/wiki

Since I think this and Apple’s X11 are the only two X11 servers likely to be used under OS X, I should be able to test for one, then the other. When I’ve got a working script, I’ll post it here. Thank you for pointing me in what is clearly the right direction!

If I add this second try block to DJ Bazzie Wazzie’s script, then I can test for both Apple’s X11 and the XQuartz one:

try
	set appID to "org.macosforge.xquartz.X11"
	tell application id appID to set APPVersion to version
on error errmsg
	set APPVersion to null
end try

That seems to be enough to sort this out. Thank you for providing exactly the code I needed to solve this one.

I now find that DJ Bazzie Wazzie’s method does not in fact report the presence of X11 under Lion. This tells me that no X11 is installed, but I can install it:

try
	set appID to "com.apple.x11"
	tell application id appID to set APPVersion to version
	display dialog "Apple X11:" & APPVersion
on error errmsg
	try
		set appID to "org.macosforge.xquartz.X11"
		tell application id appID to set APPVersion to version
		display dialog "XQuartz :" & APPVersion
	on error errmsg
		set APPVersion to null
	end try
end try

if APPVersion is null then
	display dialog "No X11 found."
else
	display dialog "We've got some X11."
end if

Am I missing something obvious?

Is the actual bundle name on Lion com.apple.x11? I posted it as an example becaue I don’t have x11 installed. The second thing is that you tell the current application to display a dialog, is the current application allowed for user interaction?

There’s a com.apple.x11.plist in my ~/Library folder, but it turns out that the name of X11 in the plist of the app itself is org.x.X11, and that produced the desired results.

The only reason I displayed a dialog is for test purposes. In my actual application I’ll do something more useful.

Thanks again. This now solves the problem completely, I think.

Under 10.7.4 the script in my previous posting no longer works. However, this variation on it does work to detect X11:

try
	set appID to "org.x.x11"
	tell application id appID to set APPVersion to version
	display dialog "Apple X11:" & APPVersion
on error errmsg
	try
		set appID to "org.macosforge.xquartz.X11"
		tell application id appID to set APPVersion to version
		display dialog "XQuartz :" & APPVersion
	on error errmsg
		set APPVersion to null
	end try
end try

if APPVersion is null then
	display dialog "No X11 found."
else
	display dialog "We've got some X11."
end if

I hope someone else finds this useful!