(* Find out which editor is running. If both are running, it probably depends on which one was launched first (i.e. seemingly random) *) tell application "System Events" try set callingEditor to 1st process whose name is "BBEdit" on error tell me to quit end try set appName to the name of callingEditor end tell (* Since BBEdit and TextWrangler use the same AppleEvents, we can get away with this. Of course AppleScript still needs to know what to emit. *) tell application appName using terms from application "BBEdit" set numdocs to the number of every text document repeat with ii from 1 to numdocs if (modified of text document ii) then save text document ii end if end repeat end using terms from end tell (* If there are open documents, then "do your thing" *) if numdocs > 0 then tell application appName using terms from application "BBEdit" set mypath to get file of active document of window 1 end using terms from end tell set cmd to do shell script "/Users/chris/Library/TeXShop/bin/Sweave.sh " & (the quoted form of POSIX path of mypath) tell application "Terminal" set numterms to count windows if numterms > 0 then do script cmd in window 1 else do script cmd end if end tell end if