[OS X TeX] question regarding spacing after a period

Herbert Schulz herbs at wideopenwest.com
Fri Mar 13 00:42:39 CET 2009

On Mar 12, 2009, at 6:32 PM, Peter Dyballa wrote:

> Am 12.03.2009 um 22:11 schrieb Herbert Schulz:
>> Packages are not part of TeXShop itself but part of a underlying  
>> TeX Distribution.
> One could put all this command line stuff into ENGINE files ...
> --
> Greetings
>  Pete


Huh? Certainly not in an Engine file.

Maybe better done in a macro. As a matter of fact there is a nice  
Macro, written by Matthew Hill, that will open up a package, class,  
etc., when you select the name of the package, etc., in its command in  
the source file and then run the macro. I use it quite often.

-- applescript direct
-- ---------------------------------------------------------------
--     Open Selection
-- ---------------------------------------------------------------
--  This script is designed to run from TeXShop and iTeXMac.
--  It is designed to open a file that has been selected.
--  (e.g., if you select "blagh" in "\input{blagh}", it will then
--  try to open "blagh.tex")
-- --------------------------
--  Detailed Description:
-- --------------------------
--   1.  The script gets the current selection from the top document
--         and the path to the file
--   2.  If search_context is true and there is no 3-letter file  
--        then grab a chunk of preceding characters, and use the last
--        paragraph of that chunk as the selection's "context."  Look in
--        this context for commands with known file-extension mappings.
--        (e.g., "\bibliographystyle" will map to the extension ".bst")
--   3.  Run kpsewhich to look for the file (if kpsewhich isn't in  
--        location, then just check current working directory)
--  Known Issues:
--       iTeXMac document is opened behind other documents
--       Context search can fail if linefeeds in middle of statements

-- ----------------------------------------
--     Dev Notes
-- ----------------------------------------
--  Original Version:  10/15/2004 - mdh (Matthew Hills)
--  Modified:
--     -- none yet --

-- ----------------------------------------
--     Properties
-- ----------------------------------------

--   search_context:    (boolean)
--        If the selection does not include a 3-letter filename
--        extension and search_context is true, then the script
--        will check for a preceding latex command to indicate what
--        file type to look for.  (also see file_extension_map and  
property search_context : true

--   file_extension_map:    (list of records)
--        When trying to determine the context of the selection,
--        will look for these tags preceding the selection and then try 
--        to use the corresponding filename extension.
--         The PREFIX field is a hack to handle files outside the  
teTeX path.
--         It gets prefixed to the file name prior to the file search.
--         For example, if you set up a command to include figures:
--               \newcommand{\myfig}[1]{ \includegraphics{figs/#1}}
--         Such that all figures are PDFs in the subdirectory "./figs",
--         then you could add a record to file_extension_map:
--                {tag:"\\myfig", extension:".pdf", prefix:"figs/"},
--        Note: watch the ordering, as the list is checked in order  
and the
--        first match will be used.
--        (e.g., \includegraphics should come before \include,
--         and \bibliographystyle should come before \bibliography)
property file_extension_map : {¬
	{tag:"\\includegraphics", extension:".pdf", prefix:""}, ¬
	{tag:"\\includeonly", extension:".tex", prefix:""}, ¬
	{tag:"\\include", extension:".tex", prefix:""}, ¬
	{tag:"\\input", extension:".tex", prefix:""}, ¬
	{tag:"\\bibliographystyle", extension:".bst", prefix:""}, ¬
	{tag:"\\bibliography", extension:".bib", prefix:""}, ¬
	{tag:"\\usepackage", extension:".sty", prefix:""}, ¬
	{tag:"\\documentclass", extension:".cls", prefix:""}, ¬
	{tag:"\\cfig", extension:".pdf", prefix:""}, ¬
	{tag:"\\RequirePackage", extension:".sty", prefix:""} ¬

--   context_search_range:    (integer)
--        When trying to determine the context of the selection,
--        will grab this many preceding characters to sift through.
property context_search_range : 100

--   kpsewhich_path:    (string)
--        This is the path for the kpsewhich program.
property kpsewhich_path : "/usr/texbin/kpsewhich"

--   default_tex_app:   (string)
--        This is the target application when running the script
--        from Apple's Script Editor.
property default_tex_app : "TeXShop" -- "iTeXMac"

on run
		set include_fname to ""
		set front_app to get_front_app(default_tex_app, true)
		tell application front_app
			set doc_path to (path of front document as string)
			if (doc_path = "") then
				display dialog ("Can not get a path to the currrent document. Try  
saving it and running the script again.") buttons {"Okay"}
			end if
			set f to (doc_path as POSIX file)
			set document_text to the text of the front document
			if front_app = "TeXShop" then
				set include_fname to the «class CONT» of the «class SELE» of the  
front document
				-- Note:  <<class CONT>> is the content class of the selection  
(«class SELE») from the TeXShop Text dictionary...
				set selection_index to the «class LOC » of the «class SELE» of the  
front document
				-- Note:  <<class LOC >> is the index of the selection («class  
SELE») from the TeXShop Text dictionary...
			end if
			if front_app = "iTeXMac" then
				set include_fname to the «class SLXN» of the text of the front  
				-- Note:  <<class SLXN>> is the selection class from the iTeXMac  
Text dictionary...
				set selection_index to the («class STRT» of the text of the front  
document) as integer
				-- Note:  <<class STRT>> is the "first selected index" from the  
iTeXMac Text dictionary...
			end if
		end tell
		-- Search context if no 3-character file extension
		if (search_context and (((count of include_fname) is less than 4) or ¬
			(item 4 of reverse of characters of include_fname is not "."))) then
			-- get context
			set context_start to selection_index - context_search_range
			if context_start < 1 then set context_start to 1
			set selection_context to last paragraph of ((characters  
context_start through selection_index of document_text) as string)
			-- match context to file type
			repeat with t in file_extension_map
				if selection_context contains (tag of t) then
					set include_fname to (prefix of t) & include_fname & (extension  
of t)
					exit repeat
				end if
			end repeat
		end if
		tell application "Finder" to set current_folder to POSIX path of  
(the container of (f as alias) as string)
		set newfile to (get_kpath_file(include_fname, current_folder))
		if newfile is not equal to {} then
			tell application front_app to open newfile
			display dialog ("Sorry, could not find a file named \"" &  
include_fname & "\" in the default search path.") buttons {"Okay"}
		end if
	on error e
		tell application front_app
			if ((count of e) is greater than 200) then
				set e to ((characters 1 thru 200 of e) as string) & "..."
			end if
			display dialog ("Error: " & e) buttons {"Okay"}
		end tell
	end try
end run

-- --------------------------------
-- get_front_app
-- --------------------------------
--  will attempt to identify the calling program
--    default_target - the return value if running from Script Editor
--    scriptrunner_is_error - if false, will throw an error if running  
from ScriptRunner
on get_front_app(default_target, scriptrunner_is_error)
		tell the current application to set front_app to my name
		if front_app is equal to "Script Editor" then set front_app to  
	on error
		-- If running from TeXShop's Script Runner, the preceding will throw  
an error.
		-- The script is launched from ScriptRunner in TeXShop 1.34 or  
earlier, or
		-- if the applescript does not have an early tag line with "--  
applescript direct"
		if scriptrunner_is_error then
			error "Unable to identify host application." & return & return &  
"Maybe you are running from an old version of TeXShop? Or maybe the  
script does not start with the line '-- applescript direct'?"
			set front_app to "TeXShop"
		end if
	end try
	return front_app
end get_front_app

-- --------------------------------
-- get_kpath_file
-- --------------------------------
--  given a filename, will query kpsewhich
--  to look for it. (note, if kpsewhich not available,
--  only checks current directory)
--  Parameters:
--     -  fname -- string 
--     -  current_directory is POSIX path of current directory
-- Returns POSIX file if successful, otherwise returns empty list, {}.
on get_kpath_file(fname, current_directory)
	set set_cwd_script to "cd " & (quoted form of current_directory) &  
" ; "
	set kpse_lookup to kpsewhich_path & " " & fname
	set else_default to " ; else echo ./" & fname & "; fi"
	set lookup_texfile_script to "if ( [[ -x `which " & kpsewhich_path &  
"` ]] ) then " & kpse_lookup & else_default
		set returned_kpath to (do shell script set_cwd_script &  
		if returned_kpath begins with "./" then -- handle files in current  
			set AppleScript's text item delimiters to ""
			set returned_kpath to current_directory & ((rest of (every  
character of returned_kpath)) as string)
		end if
		return (returned_kpath as POSIX file)
	end try
	return {}
end get_kpath_file

Good Luck,

Herb Schulz
(herbs at wideopenwest dot com)

More information about the macostex-archives mailing list