wiki:AppleScript

Version 2 (modified by toby, 10 years ago) (diff)

--

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. This script must be placed in the EXPGUI folder (so that it can find the files). You can 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 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.5 and 10.6.

(*   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, I suggest changing the lines with 
	set results to do shell script ...
to send output to path /tmp/expgui.txt rather than /dev/null (use of >> rather than > is a good idea too.)
*)


(*--------------------------------------------------------
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 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
	
	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

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