wiki:AppleScript

AppleScript for running EXPGUI

Versions of an AppleScript for starting EXPGUI are provided below, different versions appear to be needed for leopard/10.5, snowleopard/10.6 and tiger/10.4. For later versions of OS X, I recommend using the makeMacApp.py script that is distributed with EXPGUI [1240] and later. Certainly in 10.9, the AppleScript below is reported to not work. The AppleScript must be placed in the EXPGUI folder (so that it can find the EXPGUI files). You can then drag this EXPGUI app to the dock or make an alias which can be moved, but don't move the script unless you plan to edit and hard-code the file locations. Clicking on the app starts EXPGUI and dragging .EXP files onto the app opens the program with those files. Dropping a folder onto the EXPGUI app will cause EXPGUI to be started within that folder. It is possible to drag both a directory and files onto the EXPGUI app.

I find that sometimes it works better to recompile the AppleScript in the AppleScript Editor (Applications/Utilities or in 10.4 Applications/AppleScript/AppleScript Utility)

This version of the script has been tested in 10.6 and similar versions have been tested in 10.5 so it should run there too.

Note that is version differs from what has been distributed to date. It has been revised to make debugging easier. To see the output from launching EXPGUI in Tcl/Tk, create file /tmp/EXPGUIdebug.txt (from the command line: touch /tmp/EXPGUIdebug.txt). Error output from launching the program will then be placed in that file.

(*   Launch EXPGUI: launches EXPGUI by double clicking on app or by dropping a data file or folder on the app

version for 10.6 (& 10.5?)

original script by F. Farges; farges@univ-mlv.fr ;  changes by B. Toby brian.toby@anl.gov

To debug this script, create file /tmp/EXPGUIdebug.txt (from command line: touch /tmp/EXPGUIdebug.txt)
The output from launching EXPGUI will be appended to the end of that file.
*)


(*--------------------------------------------------------
Define subroutines  used in later sections of code
----------------------------------------------------------- 
*)

(*  get  directory  from a file name *)
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"
	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, " & WishExe & " 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 expgui, provided 
the expgui script is in the expgui folder of the location of this applescript
 o it finds the location of the current script and finds the expgui 
     tcl script relative to the applescript
 o it finds a version of Tcl/Tk to run either relative to the script location
     or if not present there, in the place where Fink would install it
 o it opens X11 (if needed)
-------------------------------------------------------------- 
*)
on run
	set Prefix to "cd ~;DISPLAY=:0.0 PATH=$PATH:/usr/local/bin:/usr/X11R6/bin "
	tell application "Finder"
		if POSIX file "/tmp/EXPGUIdebug.txt" exists then
			set Output to "/tmp/EXPGUIdebug.txt"
		else
			set Output to "/dev/null"
		end if
	end tell
	set myPath to (path to me)
	set ParentPath to GetParentPath(myPath)
	set appwithpath to ParentPath & "expgui:expgui"
	TestFilePresent(appwithpath)
	set posixapp to quoted form of the POSIX path of appwithpath
	set WishExe to the quoted form of GetWishExe(ParentPath)
	
	tell application "Finder"
		launch application "X11"
	end tell
	
	set results to do shell script Prefix & WishExe & " " & posixapp & "  >> " & Output & " 2>&1 &"
end run

(*
----------------------------------------------------------------------
this section handles opening files dragged into the AppleScript
 o it finds the location of the current script and finds the expgui 
     tcl script relative to the applescript
 o it finds a version of Tcl/Tk to run either relative to the script location
     or if not present there, in the place where Fink would install it
 o it opens X11 (if needed)
 o it goes through 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 the script
-----------------------------------------------------------------------
*)

on open names
	
	set Prefix to "DISPLAY=:0.0 PATH=$PATH:/usr/local/bin:/usr/X11R6/bin "
	tell application "Finder"
		if POSIX file "/tmp/EXPGUIdebug.txt" exists then
			set Output to "/tmp/EXPGUIdebug.txt"
		else
			set Output to "/dev/null"
		end if
	end tell
	set myPath to (path to me)
	set ParentPath to GetParentPath(myPath)
	set appwithpath to GetParentPath(myPath) & "expgui:expgui"
	TestFilePresent(appwithpath)
	set posixapp to the quoted form of the POSIX path of appwithpath
	set WishExe to the quoted form of GetWishExe(ParentPath)
	
	tell application "Finder"
		launch application "X11"
	end tell
	
	set filePath to ""
	repeat with i in names
		set macpath to (i as string)
		if macpath ends with ":" then
			(* if this is a directory; start EXPGUI in that folder *)
			set filename to the quoted form of the POSIX path of macpath
			set results to do shell script "cd " & filename & ";" & Prefix & WishExe & "  " & posixapp & "  >> " & Output & " 2>&1 &"
		else if macpath ends with ".EXP" then
			(* if this is an experiment file, open it *)
			set filename to the quoted form of the POSIX path of macpath
			set results to do shell script "cd ~;" & Prefix & WishExe & " " & posixapp & " " & filename & "  >> " & Output & " 2>&1 &"
		else
			display dialog "This is not a valid GSAS Experiment file: " & macpath with icon caution buttons {"OK"}
		end if
	end repeat
end open

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

(*   Launch EXPGUI: launches EXPGUI by double clicking on app or by dropping a data file or folder on the app

version for 10.4

original script by F. Farges; farges@univ-mlv.fr ;  changes by B. Toby brian.toby@anl.gov

corrected 4/14/2010 BHT

*)


(*
--------------------------------------------
Define subroutines  used in later sections of code
--------------------------------------------
*)

(*  get  directory  from a file name *)
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"
	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, " & WishExe & " 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 expgui, provided 
the expgui script is in the expgui folder of the location of this applescript
 o it finds the location of the current script and finds the expgui 
     tcl script relative to the applescript
 o it finds a version of Tcl/Tk to run either relative to the script location
     or if not present there, in the place where Fink would install it
 o it opens X11 (if needed)
 o it makes X11 active (brings to the front)
 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 run
	set myPath to (path to me)
	set ParentPath to GetParentPath(myPath)
	set appwithpath to ParentPath & "expgui:expgui"
	TestFilePresent(appwithpath)
	set posixapp to quoted form of the POSIX path of appwithpath
	set WishExe to the quoted form of GetWishExe(ParentPath)
	
	tell application "Finder"
		launch application "X11"
	end tell
	
	tell application "X11" (* needed in <=10.4, I think *)
		activate
	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 finds the location of the current script and finds the expgui 
     tcl script relative to the applescript
 o it finds a version of Tcl/Tk to run either relative to the script location
     or if not present there, in the place where Fink would install it
 o it opens X11 (if needed)
 o it goes through 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 ParentPath to GetParentPath(myPath)
	set appwithpath to GetParentPath(myPath) & "expgui:expgui"
	TestFilePresent(appwithpath)
	set posixapp to the quoted form of the POSIX path of appwithpath
	set WishExe to the quoted form of GetWishExe(ParentPath)
	
	tell application "Finder"
		launch application "X11"
	end tell
	
	set filePath to ""
	repeat with i in names
		set macpath to (i as string)
		if macpath ends with ":" then
			(* if this is a directory; start EXPGUI in that folder *)
			set filename to the quoted form of the POSIX path of macpath
			set results to do shell script "cd " & filename & ";DISPLAY=:0.0 PATH=$PATH:/usr/local/bin:/usr/X11R6/bin " & WishExe & "  " & posixapp & " > /dev/null 2>&1 &"
		else if macpath ends with ".EXP" then
			(* if this is an experiment file, open it *)
			set filename to the quoted form of the POSIX path of macpath
			set results to do shell script "cd ~;DISPLAY=:0.0 PATH=$PATH:/usr/local/bin:/usr/X11R6/bin " & WishExe & " " & posixapp & " " & filename & " > /dev/null 2>&1 &"
		else
			display dialog "This is not a valid GSAS Experiment file: " & macpath with icon caution buttons {"OK"}
		end if
	end repeat
end open
Last modified 7 years ago Last modified on Dec 4, 2013 8:21:13 PM