[OS X TeX] Another TeXShop Applescript bug :(
Ramón Figueroa-Centeno
ramonf at hawaii.edu
Tue Jun 1 06:14:22 EDT 2010
Aloha,
I found a bug in TeXShop's Applescript implementation, pertaining to the
goto line command.
Say that you are writing an applescript for TeXShop and you want to scroll
to the last line of a document. Then you are out of luck if that line is
empty, which by the way is the recommended situation in the UNIX world, that
is text files should end with a linefeed. The built in goto line command is
broken (the workaround that I came up with is subpar; see below).
First, let's reproduce the error.
Open a new window and type "abc<return><return>" where <return> denotes a
carriage return (what matters here is there is more than one line and that
the last line is empty).
Run the following script, and notice that nothing happens (what should
happen is that the last line gets selected).
tell application "TeXShop"
-- the next 4 lines of code are there just to move the cursor for you to
-- somewhere that is not on the last line.
activate
set doc_name to the name of the front document
set offset of the selection of document doc_name to 1
set length of the selection of document doc_name to 1
tell document doc_name to goto line 3 -- this doesn't work
end tell
Now, add a character to the last line of the TeXShop document (a space will
do). Run the script again and the last line will get selected as is
expected.
Here is my fix, (I do not like it because it involves dumping all the
contents of the document into a variable every time, whether or not I am
going to the last line):
tell application "TeXShop"
activate
set doc to the front document
set doc_name to the name of the front document
set offset of the selection of document doc_name to 1
set length of the selection of document doc_name to 1
tell me to go_to(doc_name, 3)
end tell
on go_to(doc_name, line_number)
tell application "TeXShop"
set doc to the document named doc_name
-- dump the whole document into a variable
set whole_document to (the text of the doc) as string
-- count the number of lines in the document
set doc_lines to count paragraphs of the whole_document
-- get the length of the last line
set last_line_length to the count of the last paragraph of the
whole_document
if line_number ≥ doc_lines and last_line_length = 0 then
-- count the number of characters in the document
set doc_length to count characters of the whole_document
-- scroll to the next to last line
tell document doc_name to goto line doc_lines - 1
-- move to the end of the last line (this does not scroll)
set offset of the selection of document doc_name to doc_length
else
tell document doc_name to goto line line_number
end if
end tell
end go_to
I hope this helps someone in the future facing this headache, and if there
is something more elegant let me know.
Ramón
--
View this message in context: http://macosx-tex.576846.n2.nabble.com/Another-TeXShop-Applescript-bug-tp5125363p5125363.html
Sent from the MacOSX-TeX mailing list archive at Nabble.com.
More information about the MacOSX-TeX
mailing list