Dead simple "Compile Draft"

When I compile draft right now I need to hit enter 3 times: first to export it, then to select the file name, and then to confirm that I really want to replace the contents in the file “Untitled”.

Could a shortcut be put in that doesn’t ask any questions at all, just uses the current settings and simply compiles the draft? It would be perfectly fine if this was hidden somewhere so users don’t accidentally compile their drafts, but those of us who wish to do so often wouldn’t have to hit enter as much.

Could you record an AppleScript macro to do that?

I would also like to ask for a single action to re-compile a draft. I’ll describe why I would find this very helpful and post the Applescript that I use as a hack, and why that script doesn’t work well for me.

I use Scrivener primarily for writing academic papers using LaTeX. I find it really helpful for helping to grow fragments of thoughts into a structured paper. I use Scrivener right up to the point where I submit the paper; I don’t reach a point where I decide to export the draft and clean it up in another program. The reason I want to finish the paper in Scrivener is that I don’t want the Scrivener version to miss out on even minor edits to the final version, because I know it is the first place I will come back to in the future when I need to pull up what I wrote.

Since I do the final twiddling in Scrivener, this means that I often edit a line and then need to export the draft to see if I fixed an issue. I then tab over to the terminal and execute a shell script that runs the LaTeX dance: xelatex, bibtex, xelatex, xelatex, open myPaper.pdf. I then tab back to Scrivener and make an edit, then compile again, and tab back to the terminal again.

I realize that I’m breaking the Scrivener philosophy of writing without the formatting distractions by twiddling LaTeX codes from within Scrivener. However, I hide most of this markup code within a “Frontmatter” section, so I can largely ignore LaTeX markup while I concentrate on finishing the paper. It’s when the deadline is looming close and I’m close to finishing the paper that I start to compile frequently and this friction starts to draw my attention away from writing.

Speaking of drawing my attention away from writing, I did try to write an Applescript that would recompile a draft (see below). I looked through the Applescript dictionary for Scrivener, but could not find any relevant commands, so I resorted to GUI scripting. This script works, but it has a serious flaw. After clicking the Compile… menu item and the Compile button, it waits for about 8 seconds on my (recent) machine before it hits the Export button. I tried versions without the repeat statements, but it takes the same amount of time, and it seems risky to leave them out. So the Applescript is actually slower than the keyboard shortcut I assigned to the Compile… menu item and hitting the return key three times. The one advantage is that I can execute the applescript from the shell script and reduce the steps I have to take when I compile to tabbing over to the terminal, hitting the up arrow to bring up the shell script again and hitting return.

It would greatly help to have a single action to recompile a draft with the same settings I used to compile the draft, in the same file path. I leave it up to the developer to decide whether this would fit best as a menu item or Applescript item; either option would be a delight when my next deadline nears.

-- Thanks Mac OS Automation
-- from:  http://www.macosxautomation.com/applescript/uiscripting/index.html
on enabledGUIScripting(switch)
	tell application "System Events"
		activate
		set UI elements enabled to switch
		return UI elements enabled
	end tell
end enabledGUIScripting

-- Thanks Mac OS Automation
-- from:  http://www.macosxautomation.com/applescript/uiscripting/index.html
on do_menu(app_name, menu_name, menu_item)
	try
		-- bring the target application to the front
		tell application app_name
			activate
		end tell
		tell application "System Events"
			tell process app_name
				tell menu bar 1
					tell menu bar item menu_name
						tell menu menu_name
							click menu item menu_item
						end tell
					end tell
				end tell
			end tell
		end tell
		return true
	on error error_message
		return false
	end try
end do_menu




enabledGUIScripting(true)
do_menu("Scrivener", "File", "Compile...")
-- Adam Faeth
-- November 2010
tell application "System Events"
	tell process "Scrivener"
		repeat until sheet 1 of window 1 exists
		end repeat
		click button "Compile" of sheet 1 of window 1
		--not sure why it takes so much time to click the Export button
		repeat until name of window 1 is "Export"
		end repeat
		click button "Export" of window 1
		if sheet 1 of window 1 exists then
			click button "Replace" of sheet 1 of window 1
		end if
	end tell
end tell

I’d just like to second this idea.

I, too, am using Scrivener to generate LaTeX code and being able to compile the project again with the last used settings using one click (or shortcut) would be really great. Being on Linux (and /so/ not planning on switching to Mac) the AppleScript solution won’t work for me.