wiki:OSXAppleScript

AppleScript for running CMPR

Two versions of an AppleScript for starting CMPR is provided below, for leopard/10.5, snowleopard/10.6 and tiger/10.4. This script must be placed in the CMPR folder (so that it can find the files). You can drag this CMPR app to the dock or make an alias which can be moved, but don't move the script unless you plan to hard-code the file locations. Clicking on the app starts CMPR and dragging files onto the app opens the program with those files. Dropping a folder onto the CMPR app will cause CMPR to be started within that folder. It is possible to drag both a directory and files onto the CMPR app.

This version of the script has been tested in 10.5 and 10.6.

(*   Launch CMPR  

tested in 10.6 (SnowLeopard)

based on EXPGUI script by F. Farges; farges@univ-mlv.fr ;  changes by B. Toby brian.toby@nist.gov
	 
	 This script can launch expgui by double clicking or by dropping a data file  or folder over  the app.
*)

(*  get  directory  containing a file (used to get current directory)
*)
on GetParentPath(theFile)
	tell application "Finder" to return container of theFile as text
end GetParentPath


(* find a wish executable or return an error *)
on GetWishExe(myPath)
	set WishExe to myPath & "exe:" & "ncnrpack-osx"
	set WishExe to the POSIX path of WishExe
	tell application "System Events"
		if (file WishExe exists) then
			return WishExe
		end if
		set WishExe to "/sw/bin/wish"
		if (file WishExe exists) then
			return WishExe
		end if
		error ("Error: Tcl/Tk executable was not found. See installation instructions.")
	end tell
end GetWishExe

(* test if a file is present and exit with an error message if it is not  *)
on TestFilePresent(appwithpath)
	tell application "System Events"
		if (file appwithpath exists) then
		else
			error ("Error: file " & appwithpath & " not found. This EXPGUI AppleScript must be run from the gsas directory. Did you move it?")
		end if
	end tell
end TestFilePresent


(*
this section responds to a double-click. It starts X11 and then  CMPR, provided  the  cmpr.tcl script is in the same folder as this applescript
*)
on run
	set myPath to (path to me)
	set appwithpath to GetParentPath(myPath) & "cmpr.tcl"
	set ParentPath to GetParentPath(myPath)
	set WishExe to the quoted form of GetWishExe(ParentPath)
	TestFilePresent(appwithpath)
	set posixapp to quoted form of the POSIX path of appwithpath
	
	tell application "Finder"
		launch application "X11"
	end tell
	
	set results to do shell script "cd ~;PATH=$PATH:/usr/local/bin:/usr/X11R6/bin " & WishExe & " " & posixapp & "  > /dev/null 2>&1 &"
	
end run
(*
this section handles opening files dragged into the AppleScript
 o it opens X11 (if needed)
 o it  passesthe list of files dragged in
 o then it converts the colon-delimited macintosh file location to a POSIX filename
 o for every non-directory file dragged into the icon, it tries to open that file in soffice
*)

on open names
	
	set myPath to (path to me)
	set appwithpath to GetParentPath(myPath) & "cmpr.tcl"
	set ParentPath to GetParentPath(myPath)
	set WishExe to the quoted form of GetWishExe(ParentPath)
	TestFilePresent(appwithpath)
	set posixapp to quoted form of the POSIX path of appwithpath
	
	tell application "Finder"
		launch application "X11"
	end tell
	
	set posixapp to the quoted form of the POSIX path of appwithpath
	set filePath to ""
	set filelist to ""
	repeat with i in names
		set macpath to (i as string)
		set filename to the quoted form of the POSIX path of macpath
		set filelist to filelist & " " & filename
	end repeat
	set results to do shell script "cd ~;PATH=$PATH:/usr/local/bin:/usr/X11R6/bin " & WishExe & " " & posixapp & " " & filelist & "  > /dev/null 2>&1 &"
end open

This version runs in 10.4 (Tiger) and 10.3 as well, I am pretty sure.

(*   Launch CMPR  

tested in 10.4

based on EXPGUI script by F. Farges; farges@univ-mlv.fr ;  changes by B. Toby brian.toby@anl.gov
	 
	 This script can launch expgui by double clicking or by dropping a data file  or folder over  the app.
*)


(*  get  directory  containing a file (used to get current directory)
*)
on GetParentPath(theFile)
	tell application "Finder" to return container of theFile as text
end GetParentPath

(* find a wish executable or return an error *)
on GetWishExe(myPath)
	set WishExe to myPath & "exe:ncnrpack-osx"
	set WishExe to the POSIX path of WishExe
	tell application "System Events"
		if (file WishExe exists) then
			return WishExe
		end if
		set WishExe to "/sw/bin/wish"
		if (file WishExe exists) then
			return WishExe
		end if
		error ("Error: Tcl/Tk executable was not found. See installation instructions.")
	end tell
end GetWishExe

(* test if a file is present and exit with an error message if it is not  *)
on TestFilePresent(appwithpath)
	tell application "System Events"
		if (file appwithpath exists) then
		else
			error ("Error: file " & appwithpath & " not found. This EXPGUI AppleScript must be run from the gsas directory. Did you move it?")
		end if
	end tell
end TestFilePresent


(*
this section responds to a double-click. It starts X11 and then  CMPR, provided  the  cmpr.tcl script is in the same folder as this applescript
*)
on run
	set myPath to (path to me)
	set appwithpath to GetParentPath(myPath) & "cmpr.tcl"
	set ParentPath to GetParentPath(myPath)
	set WishExe to the quoted form of GetWishExe(ParentPath)
	TestFilePresent(appwithpath)
	set posixapp to quoted form of the POSIX path of appwithpath
	
	tell application "Finder"
		launch application "X11"
	end tell
		
	set results to do shell script "cd ~;DISPLAY=:0.0 PATH=$PATH:/usr/local/bin:/usr/X11R6/bin " & WishExe & " " & posixapp & "  > /dev/null 2>&1 &"
	
end run
(*
this section handles opening files dragged into the AppleScript
 o it opens X11 (if needed)
 o it  passes the list of files dragged in
 o then it converts the colon-delimited macintosh file location to a POSIX filename
 o for every non-directory file dragged into the icon, it tries to open that file in soffice
*)

on open names
	
	set myPath to (path to me)
	set appwithpath to GetParentPath(myPath) & "cmpr.tcl"
	set ParentPath to GetParentPath(myPath)
	set WishExe to the quoted form of GetWishExe(ParentPath)
	TestFilePresent(appwithpath)
	set posixapp to quoted form of the POSIX path of appwithpath
	
	tell application "Finder"
		launch application "X11"
	end tell
	
	set posixapp to the quoted form of the POSIX path of appwithpath
	set filePath to ""
	set filelist to ""
	repeat with i in names
		set macpath to (i as string)
		set filename to the quoted form of the POSIX path of macpath
		set filelist to filelist & " " & filename
	end repeat
	set results to do shell script "cd ~;DISPLAY=:0.0 PATH=$PATH:/usr/local/bin:/usr/X11R6/bin " & WishExe & " " & posixapp & " " & filelist & "  > /dev/null 2>&1 &"
end open
Last modified 11 years ago Last modified on Sep 5, 2010 5:39:04 PM