update dev300-m58
[ooovba.git] / instsetoo_native / macosx / application / main.applescript
blobb85021ed98f1925e093905220fad7be6256fdee4
1 (*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: main.applescript,v $
10 * $Revision: 1.5 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 *************************************************************************)
31 (*==== (global variables as get-functions) ====*)
33 on getOOInstallPath()
34 return (((path to me) as string) & "Contents:")
35 end getOOInstallPath
37 on getOOResourcesPath()
38 return (((path to me) as string) & "Contents:Resources:")
39 end getOOResourcesPath
41 on getOOProgramPath()
42 return (getOOInstallPath() & "MacOS:")
43 end getOOProgramPath
45 on getScriptPath()
46 -- set this to absolute path when debugging
47 return (((path to me) as string) & "Contents:Resources:Scripts:")
48 end getScriptPath
50 on getOOUserSettingsPath()
51 return (((path to home folder) as string) & "Library:Application Support:OpenOffice.org:%USERDIRPRODUCTVERSION:")
52 end getOOUserSettingsPath
54 on getOOUserFontPath()
55 return (getOOUserSettingsPath() & "user:fonts:")
56 end getOOUserFontPath
58 on getUserLogsPath()
59 return (((path to home folder) as string) & "Library:Logs:")
60 end getUserLogsPath
62 on shellTerminator()
63 return (" &>/dev/null & echo $!") as string
64 end shellTerminator
67 -- the default handlers: run, open, idle, quit
70 on run
71 if (preRun()) then
72 logEvent("(Scripts/main) Running OpenOffice.org")
73 openSoffice("-")
74 end if
75 end run
77 on open (theFiles)
78 if (preRun()) then
79 openFiles(theFiles)
80 end if
81 end open
83 on idle
84 -- close icon only if ooo has terminated
85 if (hasOOoQuit()) then
86 tell me to quit
87 end if
88 -- check all x seconds if ok to quit
89 return 3
90 end idle
92 on quit
93 if (hasOOoQuit()) then
94 continue quit
95 end if
96 end quit
98 -------------------------------------------------------------
100 on preRun()
103 -- Check for the required version of Mac OS X
104 if (not atLeastOSXVersion(10, 3, 0)) then
105 display dialog getMessage("ERROR_NEED_PANTHER")
106 return false
107 end if
109 -- Check for that OOo can be found
110 if (not isRealPath(getOOProgramPath())) then
111 display dialog getMessage("ERROR_OOO_NOT_FOUND")
112 return false
113 end if
115 if (not isRealPath(getOOUserFontPath())) then
116 set createUserFont to "mkdir -p " & (quoted form of POSIX path of getOOUserFontPath()) & "; "
117 do shell script createUserFont
118 end if
120 -- If no crash occured before, ~/Library/Logs does not exist, and OpenOffice.org cannot be started
121 if (not isRealPath(getUserLogsPath())) then
122 set createUserLogsPath to "mkdir -p " & (quoted form of POSIX path of getUserLogsPath()) & "; "
123 do shell script createUserLogsPath
124 end if
126 -- Checks are ok, now do the PostInstall stuff (e.g. fondu)
128 -- load helper library
129 set postinstall to load script alias Â
130 (getScriptPath() & "PostInstall.scpt")
131 -- execute the postinstall script
132 run of postinstall
135 return true
136 end preRun
139 on hasOOoQuit()
140 if (isRealPath(getOOProgramPath())) then
141 -- set the location of soffice binary
142 set soffice to (quoted form of (POSIX path of getOOProgramPath() & "soffice"))
144 set isRunning to do shell script "_FOUND_=`ps -wx -o command | grep " & soffice & " | grep -v grep`; echo $_FOUND_"
145 if isRunning ­ "" then
146 return false
147 else
148 return true
149 end if
150 else
151 return true
152 end if
153 end hasOOoQuit
156 on openSoffice(aFile)
157 set theDisplay to startXServer()
158 if (theDisplay is equal to "error") then
159 return
160 end if
161 set theEnv to "DISPLAY=" & theDisplay & " ; export DISPLAY; "
162 set theCmd to "sh " & (quoted form of (POSIX path of getOOProgramPath() & "soffice")) & " "
163 do shell script theEnv & theCmd & aFile & shellTerminator()
164 -- logEvent("open CMD: " & theEnv & theCmd & aFile)
165 end openSoffice
168 -- helper function to start X11 server
169 on startXServer()
171 -- get X settings
172 set XSettingsList to findXServer()
173 set whichserver to item 1 of XSettingsList
174 set Xserverloc to item 2 of XSettingsList
176 -- debug:
177 -- logEvent("(scripts/main) X settings: " & whichserver & "--" & POSIX path of Xserverloc)
178 -- set whichserver to "NOXSERVER"
180 -- if nothing really was found, display an error message.
181 if (whichserver is equal to "NOXSERVER") then
183 display dialog getMessage("ERROR_NEED_X11") buttons {"Quit", "More Info"} default button "More Info"
184 if the button returned of the result is "Quit" then
185 quit
186 else
187 -- if more info is chosen, then open a help web page
188 do shell script "open http://porting.openoffice.org/mac/faq/installing/X11.html"
189 -- cannot continue, so quit the script
190 return "error"
191 end if
193 end if
195 set now_running to ""
196 set now_running to do shell script "INX=`ps -wcx | grep " & quoted form of (whichserver & "$") & "`; echo $INX"
197 if whichserver = "NOXSERVER" then
198 -- display dialog "No XServer Found"
199 set now_running to "Skip"
200 end if
201 if now_running = "" then
202 if whichserver = "X11" then
203 set x11cmd to quoted form of (Xserverloc & "/Contents/MacOS/X11") & shellTerminator()
204 do shell script x11cmd
205 -- save process id
206 set x11pid to the result
207 -- wait until the window manager is started which is the second child process of x11
208 set numchildrencmd to "ps -x -o ppid | grep " & x11pid & " | wc -l"
209 set numchildren to 0
210 set d to current date
211 set t1 to time of d
212 repeat while numchildren ­ 2
213 set d to current date
214 set t2 to time of d
215 -- give up after 30 seconds
216 if t2 - t1 > 30 then
217 display dialog "Command timed out"
218 exit repeat
219 end if
220 set result to do shell script numchildrencmd
221 set numchildren to result as integer
222 end repeat
223 else -- startup XDarwin
224 do shell script "open " & quoted form of Xserverloc & shellTerminator()
225 do shell script "sleep 4"
226 end if
227 end if
228 if whichserver is equal to "X11" then
229 -- the DISPLAY variable is different for every user currently logged in
230 -- X11 passes the DISPLAY as the last command line parameter to its child process
231 -- we can use ps to read the command line and parse the trailing :0, :1, or whatever
232 set xdisplay to do shell script "ps -wx -o command | grep X11.app | grep \":.$\" | sed \"s/^.*:/:/g\""
233 --display dialog xdisplay
234 return xdisplay
235 else
236 -- TODO: find out how XDarwin does it
237 return ":0"
238 end if
239 end startXServer
242 on openFiles(fileList)
243 if (count of fileList) > 0 then
244 repeat with i from 1 to the count of fileList
245 set theDocument to (item i of fileList)
246 set theFilePath to (quoted form of POSIX path of theDocument)
247 set theFileInfo to (info for theDocument)
248 openSoffice(theFilePath)
249 logEvent("(Scripts/main) Open file: " & theFilePath)
250 end repeat
251 end if
252 end openFiles
255 (* ===== (Helper functions) ======= *)
257 -- get a localized string
258 on getMessage(aKey)
260 if (aKey is equal to "YES_KEY") then
261 return "Yes"
262 end if
264 if (aKey is equal to "NO_KEY") then
265 return "No"
266 end if
268 if (aKey is equal to "ERROR_OOO_NOT_FOUND") then
269 return "OpenOffice.org was not found on your system. Please (re-)install OpenOffice.org first."
270 end if
272 if (aKey is equal to "ERROR_NEED_PANTHER") then
273 return "This build of OpenOffice.org cannot be run on this system, OpenOffice.org requires MacOSX 10.3 (Panther) or newer system"
274 end if
276 if (aKey is equal to "ERROR_NEED_X11") then
277 return "OpenOffice.org for Mac OS X cannot be started, because the X11 software is not installed. Please install Apple X11 first from the Mac OS X install DVD. More information: http://porting.openoffice.org/mac/faq/installing/X11.html"
278 end if
279 end try
280 end getMessage
283 -- function for logging script messages
284 on logEvent(themessage)
285 set theLine to (do shell script Â
286 "date +'%Y-%m-%d %H:%M:%S'" as string) Â
287 & " " & themessage
288 do shell script "echo " & quoted form of theLine & Â
289 " >> ~/Library/Logs/OpenOffice%USERDIRPRODUCTVERSION.log"
290 end logEvent
293 -- function for checking if a path exists
294 on isRealPath(aPath)
296 alias aPath
297 return true
298 on error
299 -- error number -43 from aPath
300 -- display dialog "NotRP -- " & aPath
301 return false
302 end try
303 end isRealPath
305 -- try to find X11 server on the Mac OS X system
306 -- return value: the found server or "NOXSERVER"
307 on findXServer()
308 -- First try standard X11 location, then try standard XDarwin location
310 set whichserver to "NOXSERVER"
311 --Utilities folder of system
312 set Xserverloc to ((path to utilities folder from system domain) as string) & "X11.app:"
313 --display dialog " Xserverloc" & Xserverloc
314 if (isRealPath(Xserverloc)) then
315 set whichserver to "X11"
316 set Xserverloc to (POSIX path of Xserverloc)
317 else
318 --Applications folder of system
319 set Xserverloc to ((path to applications folder from system domain) as string) & "XDarwin.app:"
320 if (isRealPath(Xserverloc)) then
321 set whichserver to "XDarwin"
322 set Xserverloc to (POSIX path of Xserverloc)
323 end if
324 end if
326 -- if nothing found yet try using locate, first with X11.app and then with XDarwin.app
327 if (whichserver is equal to "NOXSERVER") then
328 set Xserverloc to do shell script "locate X11.app/Contents/MacOS/X11 | sed -e 's-/Contents/MacOS/X11--g'"
329 if Xserverloc ­ "" then
330 set whichserver to "X11"
331 end if
332 end if
334 if (whichserver is equal to "NOXSERVER") then
335 set Xserverloc to do shell script "locate XDarwin.app/Contents/MacOS/XDarwin | sed -e 's-/Contents/MacOS/XDarwin--g'"
336 if Xserverloc ­ "" then
337 set whichserver to "XDarwin"
338 end if
339 end if
341 return {whichserver, Xserverloc}
342 end findXServer
345 -- Test for a minimum version of Mac OS X
346 on atLeastOSXVersion(verMajor, verMinor, verStep)
347 -- The StandardAdditions's 'system attribute' used to be the Finder's 'computer' command.
348 tell application "Finder" to set sysv to (system attribute "sysv")
350 -- Generate sysv-compatible number from given version
351 set reqVer to ((verMajor div 10) * 4096 + (verMajor mod 10) * 256 + verMinor * 16 + verStep)
353 -- DEBUGGING:
354 -- display dialog ("RV:" & reqVer & " < " & sysv as string)
356 -- set major to ((sysv div 4096) * 10 + (sysv mod 4096 div 256))
357 -- set minor to (sysv mod 256 div 16)
358 -- set step to (sysv mod 16)
359 -- display dialog ("Your Mac OS X version: " & major & "." & minor & "." & step)
361 if (reqVer > sysv) then
362 return false
363 else
364 return true
365 end if
366 end atLeastOSXVersion