Automated Scrivener to TeXShop workflow?

ri
richardk
Posts: 9
Joined: Wed Aug 06, 2008 3:05 pm

Mon Aug 25, 2008 1:10 pm Post

Hi,

as will become pretty apparent from subsequent posts from me, I'm rather new to this whole MMD/LaTeX thing. Boy, this is a Beast!

I'm not that much of a tech person/programmer by your standard, probably, so have some patience please.

I aim to do as much as possible in Scrivener itself, and have no real desire to hack XSLT/LaTeX files unless absolutely necessary.
As a consequence my mode of work is p(r)etty trial-and-error. This means I have to take the following steps very often:

  1. <alt>-<command>-E to open Scrivener's "Compile Draft" pane
  2. hitting <Enter> to start the Export, which opens a Save File dialog
  3. entering a filename
  4. hitting <Enter> twice to export and confirm a file overwrite
  5. switching to TeXShop with <command>-<Tab>
  6. hitting <command>-W twice to close the Preview and TeX-file windows of the previous version of my document
  7. mousing to File - Open Recent - filename.tex
  8. hitting <command>-T to typeset my document

Any suggestions for automating (part of) these steps are welcomed.
I was hoping to create Folder Actions with OS X's Automator, but TeXShop doesn't support this.

Thanks, Richard

co
cormullion
Posts: 20
Joined: Sun Jan 13, 2008 5:27 pm

Mon Aug 25, 2008 6:36 pm Post

You could probably eliminate some of the TeXShop steps because that's mostly a GUI way of running commands on the command line. Those commands could also be run by AppleScript. The Finder can have folder actions so that a file added to a folder is automatically processed by an AppleScript. You could choose an AppleScript command from the Scripts menu inside Scrivener to do all this (it doesn't require Scrivener to be completely applescriptable).

It's all possible, just yet more stuff to learn!

to
tobinharris
Posts: 3
Joined: Wed Nov 19, 2008 1:43 pm

Sat Dec 13, 2008 3:53 pm Post

I'm wondering the same thing. I'm using ruby Rake to automate my book writing process (extracting code from relevant solutions, clobbering temp files etc).

I'd love to find a way of getting Scrivener to export the Multimarkdown file from the command line.

Something like:

Code: Select all

Scrivener -export mmd > ./Book/Export/export.txt


Or even if I could invoke an AppleScript to automate it whilst Scrivener is open.

Are any of these options possible?

User avatar
AmberV
Posts: 24325
Joined: Sun Jun 18, 2006 4:30 am
Platform: Mac + Linux
Location: Ourense, Galiza
Contact:

Mon Dec 15, 2008 8:21 pm Post

Scrivener doesn't have an AS dictionary yet, so no, I know of no way to automate the compile process. You can certainly automate the rest though. Just throw an AS onto the folder you export to, and have it handle the automation as soon as a .txt file hits it. So while you couldn't automate Scrivener to do its thing, you could initiate the entire string of events from within Scrivener.
.:.
Ioa Petra'ka
“Whole sight, or all the rest is desolation.” —John Fowles

wh
whaynes
Posts: 23
Joined: Sat Dec 27, 2008 5:53 pm

Tue Feb 24, 2009 8:11 pm Post

I wrote a little Applescript which does exactly this. When I use it my workflow becomes

1. Open Scrivener's "Compile Draft" pane
2. Hit <Enter> twice to start the Export, and export "Untitled.tex"

It's a folder action script, and it uses Growl http://growl.info/ for notification.

To install and use: save the script in ~/library/Scripts/Folder Action Scripts/ and then attach it to a convenient folder by control clicking on the selected folder, and choosing More...>Attach a folder action.... From Scrivener, Compile Draft... a MMD->LaTeX file into the selected folder, and the script will take over and typeset and display it.

No warrantees, use at your own risk, of course! :) Read the script before using. You can download it from http://dl.getdropbox.com/u/371846/TexFolderWatcher.scpt

si
signinstranger
Posts: 268
Joined: Sun Oct 21, 2007 4:15 pm
Platform: Mac
Location: Germany

Tue Feb 24, 2009 11:29 pm Post

Thanks for sharing this! :) I never got round to making my own, so his is very helpful.

BTW: I've noticed that in your script you check the file type against "TeX Document", which will only work when the system locale is english.

So
Sophie
Posts: 216
Joined: Thu Aug 21, 2008 12:45 am

Sat Mar 21, 2009 7:50 am Post

Thanks, that makes the flow much nicer.

I had to make TexShop run typeset twice (so it gets things like Contents correct), by trial and error yielding this:

tell application "TeXShop"
close every document
open f
typeset document 1
activate
close every document
open f
typeset document 1
activate
end tell

Thanks again.
I'm sure someone will shortly explain what I am missing :-) ... Sophie

User avatar
AmberV
Posts: 24325
Joined: Sun Jun 18, 2006 4:30 am
Platform: Mac + Linux
Location: Ourense, Galiza
Contact:

Sat Mar 21, 2009 4:06 pm Post

That is fairly standard LaTeX procedure for documents containing dynamic data. Anything with cross-references, indexing, table of contents, word counts, et cetera should be rendered twice or thrice. I believe it has something to do with the LaTeX engine being a strictly linear process. It starts at the top of your file and renders downward, thus it doesn't have data about the specifics falling below the current render line. If the cross-reference points to a label that hasn't been instantiated yet, it ignores it. Second time through it has become of aware of all the label names and page number positions from the first pass, so it can fill in that missing data. Some say you should run thrice, but twice has always worked fine for me.
.:.
Ioa Petra'ka
“Whole sight, or all the rest is desolation.” —John Fowles

wh
whaynes
Posts: 23
Joined: Sat Dec 27, 2008 5:53 pm

Sat Mar 21, 2009 11:39 pm Post

Glad to hear that people are finding this script useful.

User avatar
fastfingers
Posts: 6
Joined: Fri Feb 29, 2008 5:11 am
Location: Omaha, NE
Contact:

Sun Mar 29, 2009 3:34 pm Post

Whyanes, first of all, thank you for your script and also the lead it provides into further capabilities. Very handy. And thanks to Sophie for the explanation showing how to extend the script to do multiple type setting passes on a document.

There was a comment made about the number of "passes" TeXShop needs to make on a document and I thought I would add my own experience. For the book I am writing, the following process is used. I keep this documented inside my Scrivener Manuscript as a page that is not included in the final document. A handy aid within Scrivener. Here are the steps I use:

To create a PDF file of this book:
    1. compile this document (Command-Option-E)
    2. open up the .tex file with TexShop
    3. process the file the first time for latex (Command-Shift-L)
    4. fix any errors and restart whole process if they happened
    5. run latex processing again to create table of contents info (Command-Shift-L)
    6. convert the index file (Command-Shift-I)
    7. click back on main tex file in TexShop to continue
    8. run one more time to include the index in the table of contents (Command-Shift-L)
    9. run one latex again in case adding the index changed the length of the table of contents (Command-Shift-L)
    10. click back on main tex document again in TexShop
    11. clean up temporary files (Command-Control-A)
    12. In the Finder you will find your completed PDF
The short version:
    Scrivener:
    Cmd-Opt-E
    TexShop:
    Cmd-Shift-L
    Cmd-Shift-L
    Cmd-Shift-I
    Cmd-Shift-L
    Cmd-Shift-L
    Cmd-Cntrl-A
So you can see I actually make 4 LaTeX passes through the document and also perform an Index creation step. Automating all this would be awesome, so I was excited to read this forum thread.

However, things don't seem to be working so smoothly for me. Just getting the 2nd pass with TeXShop seems to fail. I've tried a number of script ideas, here's one that I thought would work:

Code: Select all

on TypesetDoc(f)
   set frontName to f's name
   growl("Typesetting", frontName)
   tell application "TeXShop"
      close every document
      open f
   end tell
   tell document frontName of application "TeXShop"
      latexinteractive
      growl("Just launched latex...", frontName)
      repeat
         delay 2
         if taskdone then
            exit repeat
         end if
      end repeat
      growl(frontName, "First pass completed.  Continue...")
      latex
      growl("Just launched 2nd pass latex...", frontName)
      repeat
         delay 2
         if taskdone then
            exit repeat
         end if
      end repeat
      growl("Done", frontName)
   end tell
end TypesetDoc

You can see some new things I learned from research. The issuing of the "latex" or "latexinteractive" command immediately returns to Applescript. You need to add in a check to see when TeXShop is completed, hence the repeat delay loop. However, it doesn't work. I never see Growl report that the first pass completed.

Okay, figuring that maybe there's a bug in the "taskdone" logic, I thought I could cheat by removing that bit of logic and just put in a long enough delay. That's a bit of hack to depend in timing, but I'm troubleshooting. So I tried something like this:

Code: Select all

      latexinteractive
      growl("Just launched latex...", frontName)
      delay 15
      growl(frontName, "First pass completed.  Continue...")
      latex
      growl("Just launched 2nd pass latex...", frontName)

Still no luck.

If there's an issue with installed software versions, here's what I'm running:
    OS X 10.5.6
    Applescript 2.0.1
    TeXShop 2.26
I'm beginning to think that I could probably just create a custom Macro inside TeXShop to run the process for me each time. The create Bibliography script shows how to do something like that. Here's my new Macro in TeXShop:

Code: Select all

--Applescript

set fileName to #FILEPATH#
if fileName is equal to  ""
activate
display dialog "Please save the file first"  buttons {"OK"} default button "OK"
return
end if

set frontName to #DOCUMENTNAME#

tell document frontName of application "TeXShop"

   latexinteractive

   repeat
      delay 2
      if taskdone
         exit repeat
      end if
   end repeat

   latex

   repeat
      delay 2
      if taskdone
         exit repeat
      end if
   end repeat

   makeindex

   repeat
      delay 2
      if taskdone
         exit repeat
      end if
   end repeat

   latex

   repeat
      delay 2
      if taskdone
         exit repeat
      end if
   end repeat

   latex

   repeat
      delay 2
      if taskdone
         exit repeat
      end if
   end repeat

end tell

And guess what? When I run TeXShop and manually select that new Macro, it works. Maybe if I can get the Applescript folder watcher script to just tell TeXShop to run this Macro that would be great.

Any ideas?

si
signinstranger
Posts: 268
Joined: Sun Oct 21, 2007 4:15 pm
Platform: Mac
Location: Germany

Sun Mar 29, 2009 5:26 pm Post

fastfingers wrote:So you can see I actually make 4 LaTeX passes through the document and also perform an Index creation step. Automating all this would be awesome, so I was excited to read this forum thread.


I probably shouldn't tell you this, because it's super secret, but TeXShop does actually come with latexmk, a perl script that automatically runs latex, makeindex, bibtex and other related commands for you. :twisted:

1. Locate the following folder in Finder:

~/Library/TeXShop/Engines/Inactive


2. Move the files pdflatexmk.engine and xelatexmk.engine to

~/Library/TeXShop/Engines/


3. Make the following line the first line in all your .tex files

Code: Select all

%!TEX TS-program = pdflatexmk


or

Code: Select all

%!TEX TS-program = xelatexmk


if you want to use xelatex instead of pdflatex.

EDIT: Make sure that you have the latest version of TeXShop. You probably have to remove the folder

~/Library/TeXShop


first and restart TeXShop to update all scripts.