Cursor position after compiling

I created an Apple script that compiles my Draft folder to format .txt for LaTeX (I use BibLaTeX so MMD is not an option), which then updates the current window of Sublime Text, which then outputs directly to PDF. All with one click, but there’s a problem. I don’t know how to maintain Scriv’s cursor position into Sublime Text upon compiling. I am not a programmer, so here it is for those interested in using or improving it. (you need to configure compile to your liking, and script will overwrite, in your chosen directory, the current text in active Sublime Text window.

[code]tell application “Scrivener”
activate
end tell

tell application “System Events”
tell process “Scrivener”
keystroke “e” using {option down, command down}
keystroke return
keystroke return
keystroke “r” using {command down}
end tell
end tell

tell application “Sublime Text”
delay 3
activate
end tell

tell application “System Events”
tell process “Sublime Text”
keystroke “b” using {command down}
keystroke “l” using {command down}
keystroke “b”
end tell
end tell
[/code]

The problem is that the compiled output of a project is the combination of many files, however many comprise your draft, and so wherever the cursor currently is in the project is in one single section, the position is relative to that section—but you’re wanting to place the cursor relative to the entire document as compiled, something which doesn’t exist anywhere inside of your project as a singular object. If that doesn’t make sense, say you have your cursor in section of text in the middle of your Draft that is a paragraph long, it is 43 characters away from the start of the file. After compiling, this paragraph is combined with a few hundred other chunks of text so that where the cursor was in the paragraph is now many tens of thousands of characters from the start of the file, in Sublime.

So you can’t use cursor position, leaving only the state of the text around the cursor as a useful anchor. You could script the software to select five words around the cursor and use Cmd-E to load the selection to the global search pasteboard (which Sublime will make use of if you press Cmd-G). It won’t always be perfect, but expanding how much text you search for, if five words isn’t consistently unique enough, could be safe.

I see why the cursor cannot be controlled after compile. This idea of five or ten words around the cursor is very good, but how could I enable the Cmd-E and Cmd-G into the script? I have no idea how that part looks like.

These would be done just like the other shortcuts you have in the script, like:

keystroke "e" using {command down}