Launcher Code

From NeoWiki

Jump to: navigation, search

launcher xcode files: http://neo-downloads.sixthcrusade.com/launcher-xcode.zip


-- launcher.applescript
-- launcher

--  Created by Jacob  Haddon on 9/18/06.
--  Copyright (c) 2003 __MyCompanyName__. All rights reserved.



(*************************************************************************
 *
 *  OpenOffice.org - a multi-platform office productivity suite
 *
 *  $RCSfile: main.applescript,v $
 *
 *  $Revision: 1.2 $
 *
 *  last change: $Author: obo $ $Date: 2005/10/13 09:46:35 $
 *
 *  The Contents of this file are made available subject to
 *  the terms of GNU Lesser General Public License Version 2.1.
 *
 *
 *    GNU Lesser General Public License Version 2.1
 *    =============================================
 *    Copyright 2005 by Sun Microsystems, Inc.
 *    901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License version 2.1, as published by the Free Software Foundation.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public
 *    License along with this library; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *    MA  02111-1307  USA
 *
 *************************************************************************)



on clicked theObject
	set j to (name of theObject as string)
	--display dialog j
	if j is equal to "open" then
		display open panel
		set the pathNames to (path names of open panel)
		openFiles(pathNames)
	else if j is equal to "quit" then
		--display dialog "Are You Sure You Want to Quit OpenOffice.org?"
		quit {}
	else
		openSoffice(j, "-")
	end if
end clicked


--
-- the default handlers: run, open, idle, quit
--



on open theObject
	openFiles(theObject)
end open

on idle
	-- close icon only if ooo has terminated
	if isOOoRunning() = 0 then
		tell me to quit
	end if
	-- check all x seconds if ok to quit
	return 3
end idle

on quit
	if isOOoRunning() = 0 then
		continue quit
	else
		display dialog "OpenOffice is still running, Quit Launcher?"
		continue quit
	end if
end quit

-------------------------------------------------------------

on isOOoRunning()
	set soffice to getProgramPath() & "/soffice"
	set isRunning to do shell script "_FOUND_=`ps -wx -o command | grep \"" & soffice & "\" | grep -v grep`; echo $_FOUND_"
	if isRunning ≠ "" then
		return 1
	else
		return 0
	end if
end isOOoRunning

on openSoffice(progToOpen, aFile)
	set oooProgramPath to getProgramPath()
	--display dialog "getProgramPath"
	set theDisplay to startXServer()
	--display dialog "start x"
	set theEnv to "DISPLAY=" & theDisplay & " ; export DISPLAY; "
	set theCmd to "sh \"" & oooProgramPath & "/" & progToOpen & "\" "
	--display dialog "soffice"
	do shell script theEnv & theCmd & aFile & " &>/dev/null & echo $!"
end openSoffice

on startXServer()
	-- First try standard X11 location, then try standard XDarwin location
	set Xserverloc to "/Applications/Utilities/X11.app"
	set whichserver to do shell script "if [ -d " & Xserverloc & "  ]; then echo \"X11\"; else echo \"NOXSERVER\"; fi"
	if whichserver = "NOXSERVER" then
		set Xserverloc to "/Applications/XDarwin.app"
		set whichserver to do shell script "if [ -d " & Xserverloc & " ]; then echo \"XDarwin\"; else echo \"NOXSERVER\"; fi"
	end if
	
	-- if nothing found yet try using locate, first with X11.app and then with XDarwin.app
	if whichserver = "NOXSERVER" then
		set Xserverloc to do shell script "locate X11.app/Contents/MacOS/X11 | sed -e 's-/Contents/MacOS/X11--g'"
		if Xserverloc ≠ "" then
			set whichserver to "X11"
		end if
	end if
	if whichserver = "NOXSERVER" then
		set Xserverloc to do shell script "locate XDarwin.app/Contents/MacOS/XDarwin | sed -e 's-/Contents/MacOS/XDarwin--g'"
		if Xserverloc ≠ "" then
			set whichserver to "XDarwin"
		end if
	end if
	
	set now_running to ""
	set now_running to do shell script "INX=`ps -wcx | grep  \"" & whichserver & "\"`; echo $INX"
	if whichserver = "NOXSERVER" then
		-- display dialog "No XServer Found"
		set now_running to "Skip"
	end if
	
	-- display dialog "now running is " & now_running
	if now_running = "" then
		if whichserver = "X11" then
			-- enable keyboard extension for X11 for proper Alt key handling 
			set x11cmd to Xserverloc & "/Contents/MacOS/X11 +kb &>/dev/null & echo $!"
			do shell script x11cmd
			-- save process id
			set x11pid to the result
			-- wait until the window manager is started which is the second child process of x11
			set numchildrencmd to "ps -x -o ppid | grep " & x11pid & " | wc -l"
			set numchildren to 0
			set d to current date
			set t1 to time of d
			repeat while numchildren ≠ 2
				set d to current date
				set t2 to time of d
				-- give up after 30 seconds
				if t2 - t1 > 30 then
					display dialog "Command timed out"
					exit repeat
				end if
				set result to do shell script numchildrencmd
				set numchildren to result as integer
			end repeat
		else -- startup XDarwin
			do shell script "open " & "\"" & Xserverloc & "\"" & " >> /dev/null 2>&1"
			do shell script "sleep 4"
		end if
	end if
	
	
	if whichserver = "X11" then
		-- the DISPLAY variable is different for every user currently logged in
		-- X11 passes the DISPLAY as the last command line parameter to its child process
		-- we can use ps to read the command line and parse the trailing :0, :1, or whatever
		set xdisplay to do shell script "ps -wx -o command | grep X11.app | grep \":.$\" | sed \"s/^.*:/:/g\""
		-- display dialog xdisplay
		return xdisplay
	else
		-- TODO: find out how XDarwin does it
		return ":0"
	end if
end startXServer

on checkPath(aPath)
	set pathFound to do shell script "if [ -d \"" & aPath & "\" ]; then echo \"true\"; else echo \"false\"; fi"
	--display dialog quoted form of aPath
	if (pathFound = "true") then
		return 1
	else
		return 0
	end if
end checkPath

-- getInstallPath
-- finds the path to the oo.o installation
-- checks the path to make sure there is something there
-- returns true if so, or error message if not

on getInstallPath()
	set myPath to (path to me)
	--set oooInstallPath to "/Applications/OpenOffice.org 2.0.app/Contents/openoffice.org"
	tell application "Finder" to set j to POSIX path of (application file id "org.openoffice.script" as string)
	set oooInstallPath to j & "/Contents/openoffice.org"
	if checkPath(oooInstallPath) = 0 then
		display dialog "No valid installation found at: " & oooInstallPath
		return ""
	else
		return oooInstallPath
	end if
end getInstallPath

on getProgramPath()
	return getInstallPath() & "/program"
end getProgramPath


on openFiles(fileList)
	-- open dialog sends posix 
	if (count of fileList) > 0 then
		repeat with i from 1 to the count of fileList
			set theDocument to (quoted form of item i of fileList)
			--display dialog theDocument
			openSoffice("soffice", theDocument)
		end repeat
	end if
end openFiles

Personal tools