Line 0
Link Here
|
|
|
1 |
(************************************************************************* |
2 |
* |
3 |
* OpenOffice.org - a multi-platform office productivity suite |
4 |
* |
5 |
* $RCSfile: main.applescript,v $ |
6 |
* |
7 |
* $Revision: 1.7 $ |
8 |
* |
9 |
* last change: $Author: vg $ $Date: 2006/11/22 11:56:26 $ |
10 |
* |
11 |
* The Contents of this file are made available subject to |
12 |
* the terms of GNU Lesser General Public License Version 2.1. |
13 |
* |
14 |
* |
15 |
* GNU Lesser General Public License Version 2.1 |
16 |
* ============================================= |
17 |
* Copyright 2005 by Sun Microsystems, Inc. |
18 |
* 901 San Antonio Road, Palo Alto, CA 94303, USA |
19 |
* |
20 |
* This library is free software; you can redistribute it and/or |
21 |
* modify it under the terms of the GNU Lesser General Public |
22 |
* License version 2.1, as published by the Free Software Foundation. |
23 |
* |
24 |
* This library is distributed in the hope that it will be useful, |
25 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
26 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
27 |
* Lesser General Public License for more details. |
28 |
* |
29 |
* You should have received a copy of the GNU Lesser General Public |
30 |
* License along with this library; if not, write to the Free Software |
31 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
32 |
* MA 02111-1307 USA |
33 |
* |
34 |
*************************************************************************) |
35 |
|
36 |
(*==== (global variables as get-functions) ====*) |
37 |
|
38 |
on getOOInstallPath() |
39 |
-- return (((path to applications folder from system domain) as string) & "OpenOffice XXXOOO_VER_MAJORMINORXXX.app:" & "Contents:MacOS:") |
40 |
return (((path to me) as string) & "Contents:MacOS:") |
41 |
end getOOInstallPath |
42 |
|
43 |
on getOOResourcesPath() |
44 |
return (((path to me) as string) & "Contents:Resources:") |
45 |
end getOOResourcesPath |
46 |
|
47 |
on getOOProgramPath() |
48 |
return (getOOInstallPath() & "program:") |
49 |
end getOOProgramPath |
50 |
|
51 |
on getScriptPath() |
52 |
-- set this to absolute path when debugging |
53 |
return (((path to me) as string) & "Contents:Resources:Scripts:") |
54 |
end getScriptPath |
55 |
|
56 |
on getOOUserSettingsPath() |
57 |
return (((path to home folder) as string) & "Library:Application Support:OpenOffice.org:XXXOOO_VER_MAJORXXX:") |
58 |
end getOOUserSettingsPath |
59 |
|
60 |
on getOOUserFontPath() |
61 |
return (getOOUserSettingsPath() & "user:fonts:") |
62 |
end getOOUserFontPath |
63 |
|
64 |
on getUserLogsPath() |
65 |
return (((path to home folder) as string) & "Library:Logs:") |
66 |
end getUserLogsPath |
67 |
|
68 |
on shellTerminator() |
69 |
return (" &>/dev/null & echo $!") as string |
70 |
end shellTerminator |
71 |
|
72 |
-- |
73 |
-- the default handlers: run, open, idle, quit |
74 |
-- |
75 |
|
76 |
on run |
77 |
if (preRun()) then |
78 |
logEvent("(Scripts/main) Running OpenOffice.org") |
79 |
openSoffice("-") |
80 |
end if |
81 |
end run |
82 |
|
83 |
on open (theFiles) |
84 |
if (preRun()) then |
85 |
openFiles(theFiles) |
86 |
end if |
87 |
end open |
88 |
|
89 |
on idle |
90 |
-- close icon only if ooo has terminated |
91 |
if (hasOOoQuit()) then |
92 |
tell me to quit |
93 |
end if |
94 |
-- check all x seconds if ok to quit |
95 |
return 3 |
96 |
end idle |
97 |
|
98 |
on quit |
99 |
if (hasOOoQuit()) then |
100 |
continue quit |
101 |
end if |
102 |
end quit |
103 |
|
104 |
------------------------------------------------------------- |
105 |
|
106 |
on preRun() |
107 |
|
108 |
|
109 |
-- Check for the required version of Mac OS X |
110 |
if (not atLeastOSXVersion(10, 3, 0)) then |
111 |
display dialog getMessage("ERROR_NEED_PANTHER") |
112 |
return false |
113 |
end if |
114 |
|
115 |
-- Check for that OOo can be found |
116 |
if (not isRealPath(getOOProgramPath())) then |
117 |
display dialog getMessage("ERROR_OOO_NOT_FOUND") |
118 |
return false |
119 |
end if |
120 |
|
121 |
-- Migrate the old user preference folder, if possible. (~/Library/Application Support/OpenOffice.org 2.0/) |
122 |
if (not isRealPath(getOOUserSettingsPath())) then |
123 |
set oldPrefsPath to (((path to home folder) as string) & "Library:Application Support:OpenOffice.org 2.0:") |
124 |
migrateUserPrefs(oldPrefsPath) |
125 |
end if |
126 |
|
127 |
-- Migrate the old user preference folder, if possible. (~/.openoffice.org2/) |
128 |
if (not isRealPath(getOOUserSettingsPath())) then |
129 |
set oldPrefsPath to (((path to home folder) as string) & ".openoffice.org2:") |
130 |
migrateUserPrefs(oldPrefsPath) |
131 |
end if |
132 |
|
133 |
if (not isRealPath(getOOUserFontPath())) then |
134 |
set createUserFont to "mkdir -p " & (quoted form of POSIX path of getOOUserFontPath()) & "; " |
135 |
do shell script createUserFont |
136 |
end if |
137 |
-- If no crash occured before, ~/Library/Logs does not exist, and OpenOffice.org cannot be started |
138 |
if (not isRealPath(getUserLogsPath())) then |
139 |
set createUserLogsPath to "mkdir -p " & (quoted form of POSIX path of getUserLogsPath()) & "; " |
140 |
do shell script createUserLogsPath |
141 |
end if |
142 |
-- Checks are ok, now do the PostInstall stuff (e.g. fondu) |
143 |
|
144 |
-- load helper library |
145 |
set postinstall to load script alias  |
146 |
(getScriptPath() & "PostInstall.scpt") |
147 |
-- execute the postinstall script |
148 |
run of postinstall |
149 |
|
150 |
|
151 |
return true |
152 |
end preRun |
153 |
|
154 |
|
155 |
on hasOOoQuit() |
156 |
if (isRealPath(getOOProgramPath())) then |
157 |
-- set the location of soffice binary |
158 |
set soffice to (quoted form of (POSIX path of getOOProgramPath() & "soffice")) |
159 |
|
160 |
set isRunning to do shell script "_FOUND_=`ps -wx -o command | grep " & soffice & " | grep -v grep`; echo $_FOUND_" |
161 |
if isRunning "" then |
162 |
return false |
163 |
else |
164 |
return true |
165 |
end if |
166 |
else |
167 |
return true |
168 |
end if |
169 |
end hasOOoQuit |
170 |
|
171 |
|
172 |
on openSoffice(aFile) |
173 |
set theDisplay to startXServer() |
174 |
if (theDisplay is equal to "error") then |
175 |
return |
176 |
end if |
177 |
set theEnv to "DISPLAY=" & theDisplay & " ; export DISPLAY; " |
178 |
set theCmd to "sh " & (quoted form of (POSIX path of getOOProgramPath() & "soffice")) & " " |
179 |
do shell script theEnv & theCmd & aFile & shellTerminator() |
180 |
-- logEvent("open CMD: " & theEnv & theCmd & aFile) |
181 |
end openSoffice |
182 |
|
183 |
|
184 |
-- helper function to start X11 server |
185 |
on startXServer() |
186 |
|
187 |
-- get X settings |
188 |
set XSettingsList to findXServer() |
189 |
set whichserver to item 1 of XSettingsList |
190 |
set Xserverloc to item 2 of XSettingsList |
191 |
|
192 |
-- debug: |
193 |
-- logEvent("(scripts/main) X settings: " & whichserver & "--" & POSIX path of Xserverloc) |
194 |
-- set whichserver to "NOXSERVER" |
195 |
|
196 |
-- if nothing really was found, display an error message. |
197 |
if (whichserver is equal to "NOXSERVER") then |
198 |
display dialog getMessage("ERROR_NEED_X11") |
199 |
return "error" |
200 |
end if |
201 |
|
202 |
set now_running to "" |
203 |
set now_running to do shell script "INX=`ps -wcx | grep " & quoted form of (whichserver & "$") & "`; echo $INX" |
204 |
if whichserver = "NOXSERVER" then |
205 |
-- display dialog "No XServer Found" |
206 |
set now_running to "Skip" |
207 |
end if |
208 |
if now_running = "" then |
209 |
if whichserver = "X11" then |
210 |
set x11cmd to quoted form of (Xserverloc & "/Contents/MacOS/X11") & shellTerminator() |
211 |
do shell script x11cmd |
212 |
-- save process id |
213 |
set x11pid to the result |
214 |
-- wait until the window manager is started which is the second child process of x11 |
215 |
set numchildrencmd to "ps -x -o ppid | grep " & x11pid & " | wc -l" |
216 |
set numchildren to 0 |
217 |
set d to current date |
218 |
set t1 to time of d |
219 |
repeat while numchildren 2 |
220 |
set d to current date |
221 |
set t2 to time of d |
222 |
-- give up after 30 seconds |
223 |
if t2 - t1 > 30 then |
224 |
display dialog "Command timed out" |
225 |
exit repeat |
226 |
end if |
227 |
set result to do shell script numchildrencmd |
228 |
set numchildren to result as integer |
229 |
end repeat |
230 |
else -- startup XDarwin |
231 |
do shell script "open " & quoted form of Xserverloc & shellTerminator() |
232 |
do shell script "sleep 4" |
233 |
end if |
234 |
end if |
235 |
if whichserver is equal to "X11" then |
236 |
-- the DISPLAY variable is different for every user currently logged in |
237 |
-- X11 passes the DISPLAY as the last command line parameter to its child process |
238 |
-- we can use ps to read the command line and parse the trailing :0, :1, or whatever |
239 |
set xdisplay to do shell script "ps -wx -o command | grep X11.app | grep \":.$\" | sed \"s/^.*:/:/g\"" |
240 |
--display dialog xdisplay |
241 |
return xdisplay |
242 |
else |
243 |
-- TODO: find out how XDarwin does it |
244 |
return ":0" |
245 |
end if |
246 |
end startXServer |
247 |
|
248 |
|
249 |
on openFiles(fileList) |
250 |
if (count of fileList) > 0 then |
251 |
repeat with i from 1 to the count of fileList |
252 |
set theDocument to (item i of fileList) |
253 |
set theFilePath to (quoted form of POSIX path of theDocument) |
254 |
set theFileInfo to (info for theDocument) |
255 |
openSoffice(theFilePath) |
256 |
logEvent("(Scripts/main) Open file: " & theFilePath) |
257 |
end repeat |
258 |
end if |
259 |
end openFiles |
260 |
|
261 |
|
262 |
(* ===== (Helper functions) ======= *) |
263 |
|
264 |
-- get a localized string |
265 |
on getMessage(aKey) |
266 |
try |
267 |
if (aKey is equal to "YES_KEY") then |
268 |
return "Yes" |
269 |
end if |
270 |
|
271 |
if (aKey is equal to "NO_KEY") then |
272 |
return "No" |
273 |
end if |
274 |
|
275 |
if (aKey is equal to "ERROR_OOO_NOT_FOUND") then |
276 |
return "OpenOffice.org was not found on your system. Please (re-)install OpenOffice.org first." |
277 |
end if |
278 |
|
279 |
if (aKey is equal to "ERROR_NEED_PANTHER") then |
280 |
return "This build of OpenOffice.org cannot be run on this system, OpenOffice.org requires MacOSX 10.3 (Panther) or newer system" |
281 |
end if |
282 |
|
283 |
if (aKey is equal to "ERROR_NEED_X11") then |
284 |
return "OpenOffice.org cannot be started, because X11 is not installed. Please install Apple X11 first." |
285 |
end if |
286 |
end try |
287 |
end getMessage |
288 |
|
289 |
|
290 |
-- function for logging script messages |
291 |
on logEvent(themessage) |
292 |
set theLine to (do shell script  |
293 |
"date +'%Y-%m-%d %H:%M:%S'" as string) Â |
294 |
& " " & themessage |
295 |
do shell script "echo " & quoted form of theLine & Â |
296 |
" >> ~/Library/Logs/OpenOfficeXXXOOO_VER_MAJORXXX.log" |
297 |
end logEvent |
298 |
|
299 |
|
300 |
-- function for checking if a path exists |
301 |
on isRealPath(aPath) |
302 |
try |
303 |
alias aPath |
304 |
return true |
305 |
on error |
306 |
-- error number -43 from aPath |
307 |
-- display dialog "NotRP -- " & aPath |
308 |
return false |
309 |
end try |
310 |
end isRealPath |
311 |
|
312 |
-- try to find X11 server on the Mac OS X system |
313 |
-- return value: the found server or "NOXSERVER" |
314 |
on findXServer() |
315 |
-- First try standard X11 location, then try standard XDarwin location |
316 |
|
317 |
set whichserver to "NOXSERVER" |
318 |
--Utilities folder of system |
319 |
set Xserverloc to ((path to utilities folder from system domain) as string) & "X11.app:" |
320 |
--display dialog " Xserverloc" & Xserverloc |
321 |
if (isRealPath(Xserverloc)) then |
322 |
set whichserver to "X11" |
323 |
set Xserverloc to (POSIX path of Xserverloc) |
324 |
else |
325 |
--Applications folder of system |
326 |
set Xserverloc to ((path to applications folder from system domain) as string) & "XDarwin.app:" |
327 |
if (isRealPath(Xserverloc)) then |
328 |
set whichserver to "XDarwin" |
329 |
set Xserverloc to (POSIX path of Xserverloc) |
330 |
end if |
331 |
end if |
332 |
|
333 |
-- if nothing found yet try using locate, first with X11.app and then with XDarwin.app |
334 |
if (whichserver is equal to "NOXSERVER") then |
335 |
set Xserverloc to do shell script "locate X11.app/Contents/MacOS/X11 | sed -e 's-/Contents/MacOS/X11--g'" |
336 |
if Xserverloc "" then |
337 |
set whichserver to "X11" |
338 |
end if |
339 |
end if |
340 |
|
341 |
if (whichserver is equal to "NOXSERVER") then |
342 |
set Xserverloc to do shell script "locate XDarwin.app/Contents/MacOS/XDarwin | sed -e 's-/Contents/MacOS/XDarwin--g'" |
343 |
if Xserverloc "" then |
344 |
set whichserver to "XDarwin" |
345 |
end if |
346 |
end if |
347 |
|
348 |
return {whichserver, Xserverloc} |
349 |
end findXServer |
350 |
|
351 |
|
352 |
-- Test for a minimum version of Mac OS X |
353 |
on atLeastOSXVersion(verMajor, verMinor, verStep) |
354 |
-- The StandardAdditions's 'system attribute' used to be the Finder's 'computer' command. |
355 |
tell application "Finder" to set sysv to (system attribute "sysv") |
356 |
|
357 |
-- Generate sysv-compatible number from given version |
358 |
set reqVer to ((verMajor div 10) * 4096 + (verMajor mod 10) * 256 + verMinor * 16 + verStep) |
359 |
|
360 |
-- DEBUGGING: |
361 |
-- display dialog ("RV:" & reqVer & " < " & sysv as string) |
362 |
|
363 |
-- set major to ((sysv div 4096) * 10 + (sysv mod 4096 div 256)) |
364 |
-- set minor to (sysv mod 256 div 16) |
365 |
-- set step to (sysv mod 16) |
366 |
-- display dialog ("Your Mac OS X version: " & major & "." & minor & "." & step) |
367 |
|
368 |
if (reqVer > sysv) then |
369 |
return false |
370 |
else |
371 |
return true |
372 |
end if |
373 |
end atLeastOSXVersion |
374 |
|
375 |
-- Migrate older user pref dir paths, if possible |
376 |
on migrateUserPrefs(oldpath) |
377 |
-- if the old prefs can be found... |
378 |
if (isRealPath(oldpath)) then |
379 |
set newPathParent to (((path to home folder) as string) & "Library:Application Support:OpenOffice.org:") |
380 |
-- check if the new base user prefs directory exists, and create the folder if it doesn't |
381 |
if (not isRealPath(newPathParent)) then |
382 |
set createUserPrefsBasePath to "mkdir -p " & (quoted form of POSIX path of newPathParent) & "; " |
383 |
do shell script createUserPrefsBasePath |
384 |
end if |
385 |
-- migrate the old prefs to the new folder |
386 |
set newPath to (quoted form of POSIX path of getOOUserSettingsPath()) |
387 |
set migratePrefs to "mv " & (quoted form of POSIX path of oldpath) & " " & newPath & "; " |
388 |
do shell script migratePrefs |
389 |
end if |
390 |
end migrateUserPrefs |
391 |
|