-- Open_OpenOffice.org.applescript
-- Open_OpenOffice.org

--  Created by Ozk on Thu Jul 22 2004.
--  Copyright (c) 2003 Ozk. All rights reserved.

--on idle
(* Add any idle time processing here. *)
--end idle

on open dropfile
	
	do shell script "/bin/tcsh -c 'which soffice'"
	set thePath to result
	do shell script "/bin/tcsh -c 'echo $DISPLAY'"
	set theDISPLAY to result
	do shell script "grep '^export XMODIFIERS' ~/.xinitrc"
	set tempXMODIFIERS to result
	set tmp to AppleScript's text item delimiters
	set AppleScript's text item delimiters to "="
	set theXMODIFIERS to last text item of tempXMODIFIERS
	set AppleScript's text item delimiters to tmp
	
	tell application "X11" to activate
	
	set theOOo to "false"
	set tempOOo to "false"
	
	try
		do shell script "ps -cx | grep soffice"
		set tempOOo to result
		set tmp to AppleScript's text item delimiters
		set AppleScript's text item delimiters to space
		set theOOo to last text item of tempOOo
		set AppleScript's text item delimiters to tmp
	end try
	
	if theOOo is "soffice.bin" then
		
		set dropfile to dropfile as string
		set filepath to POSIX path of file dropfile
		
		do shell script "export DISPLAY=" & theDISPLAY & space & "&&" & space & thePath & space & filepath & space & "&"
		
	end if
	
	if theOOo is not "soffice.bin" then
		
		set theResult to "false"
		repeat until theResult is theXMODIFIERS
			tell application "Finder"
				try
					set theResult to name of (application process theXMODIFIERS)
				end try
			end tell
		end repeat
		
		do shell script "/usr/bin/open-x11" & space & thePath
		
	end if
	
	set theOOo to "false"
	set tempOOo to "false"
	
	repeat until theOOo is "soffice.bin"
		try
			do shell script "ps -cx | grep soffice"
			set tempOOo to result
			set tmp to AppleScript's text item delimiters
			set AppleScript's text item delimiters to space
			set theOOo to last text item of tempOOo
			set AppleScript's text item delimiters to tmp
		end try
	end repeat
	
	if theOOo is "soffice.bin" then
		
		set dropfile to dropfile as string
		set filepath to POSIX path of file dropfile
		
		do shell script "export DISPLAY=" & theDISPLAY & space & "&&" & space & thePath & space & filepath & space & "&"
	end if
	
	display dialog result
	--set result to "OK"
	
end open

on launched theObject
	
	do shell script "/bin/tcsh -c 'which soffice'"
	set thePath to result
	do shell script "/bin/tcsh -c 'echo $DISPLAY'"
	set theDISPLAY to result
	do shell script "grep '^export XMODIFIERS' ~/.xinitrc"
	set tempXMODIFIERS to result
	set tmp to AppleScript's text item delimiters
	set AppleScript's text item delimiters to "="
	set theXMODIFIERS to last text item of tempXMODIFIERS
	set AppleScript's text item delimiters to tmp
	
	tell application "X11" to activate
	
	set theOOo to "false"
	set tempOOo to "false"
	
	try
		do shell script "ps -cx | grep soffice"
		set tempOOo to result
		set tmp to AppleScript's text item delimiters
		set AppleScript's text item delimiters to space
		set theOOo to last text item of tempOOo
		set AppleScript's text item delimiters to tmp
	end try
	
	if theOOo is "false" then
		
		set theResult to "false"
		repeat until theResult is theXMODIFIERS
			tell application "Finder"
				try
					set theResult to name of (application process theXMODIFIERS)
				end try
			end tell
		end repeat
		
	end if
	
	do shell script "/usr/bin/open-x11" & space & thePath
	
end launched

