Please share your AutoHotKey macros for Scrivener

AutoHotKey: DownloadTutorialFAQReferenceSample Scripts

Please share any AutoHotKey (or PhraseExpress) scripts you are using in Scrivener.

SCRIVENER KEYBOARD SHORTCUT TIPS:
Customize Scrivener’s keyboard shortcuts via Tools>Options>Keyboard and use the “Edit Shortcut” line at the bottom of the window that pops up.
View, browse or print all keyboard shortcuts for Windows
Some regular shortcuts not working properly? Try deleting the [HKEY_CURRENT_USER]Software/Scrivener/Scrivener/Options/Shortcut key folder in Windows with the software closed and re-open. HT AmberV via this post

AUTOHOTKEY SCRIPTS:
TIP: [url=http://www.literatureandlatte.com/forum/viewtopic.php?p=130238#p130238]Turn off Windows “Sticky Keys” before using AutoHotKey[/url]
Open PDF at specific page in external viewer
Split a document and retain metadata, because for now, Scrivener’s built-in split function does not carrly the metadata to the newly split document.
Convert to uppercase or lowercase, by StefanG (I’ve PM’d StefanG for script details)

Hat Tip to narrsd and almansur for the idea of using Scrivener’s built-in duplicate function, and to AmberV for patiently and wisely contributing so much to my understanding of Scrivener

This is from Uku’s post here:

^!F5::
clipboard =
Send, ^c
ClipWait, 2
If ErrorLevel
{
MsgBox, Page number copy to clipboard failed
Return
}
FileDelete, c:\tempcmd.txt
FileAppend, c:\pdfxcview.exe /A "page=, c:\tempcmd.txt
FileAppend, %Clipboard%, c:\tempcmd.txt
FileAppend, " “C:\Dropbox\source.pdf”, c:\tempcmd.txt
FileRead, pdfcommand, c:\tempcmd.txt
Run %pdfcommand%
Return

Now I put the corresponding PDF page numbers into the Scrivener text wherever appropriate, and when I want to call the PDF, I mark the page number and then press Ctrl-Alt-F5: PDF is opened in PDF X-ChangeViewer at the correct page. I can also annotate and highlight the source PDF, as it stays in the Dropbox folder.

In order to split and retain metadata, I have mapped the Windows-z (hold down Windows key, press z) to split a document and retain the metadata using this AutoHotKey script, which only works when the AutoHotKey script is running and the Scrivener window is active:

SPLIT, ADD “.1” TO TITLE

#IfWinActive ahk_class QWidget
#z::
SendInput ^+{End}^x^+d{End}.1{Enter}^{Tab}^a^v
return


SPLIT, MAKE SELECTED TEXT TITLE

This requires you to select text in the created split document, which I’ll call the “child” document. After splitting, select the text and then use this script, which I have mapped to Windows-a (hold down Windows key, press a):

#IfWinActive ahk_class QWidget
#a::
SendInput ^c^{Tab}{F2}^v{Enter}^{Tab}{Right}{Left}
return

NOTE: If you change the initial ^c to ^x it will cut the text from the document before placing it as the title. In that case, remove the {Right}{Left} from the end, as they are superfluous.

Before beginning to use AutoHotKey, I suggest turning off “Sticky Keys” to avoid problems In Windows 7 (similar to Windows Vista).

Start from:
Control Panel\All Control Panel Items\Ease of Access Center\Make the keyboard easier to use
and under “Make it easier to type” uncheck box for “Turn on Sticky Keys…”
Then click link for “Set up Sticky Keys” and uncheck box for “Turn on Sticky Keys when SHIFT is pressed five times.”

These are fantastic, thanks for these! Do you mind if I move this over to the Tips and Tricks forum?

Great idea! Apologies for my carelessness in overlooking that more appropriate forum.

No worries! I’m going to have to check out AutoHotKey now, never played with it before.

If you want to browse the keyboard shortcuts, bookmark this page.

While you can print them from there, Scrivener will export a text file with all keyboard shortcuts in it if you go to Tools>Options>Keyboard within Scrivener, and near the bottom of the window that pops up, click Export and then choose “For printing” to get a file you can print from.

You can also view them from Tools>Options>Keyboard, but it’s not as easy as this page or printing them yourself.

Every time I open Scrivener I want my Keywords list to appear along the right edge of my monitor, stretched to almost the height of my monitor, and sorted. I wrote the following AHK script to do this; it uses Windows key + S to open Scrivener, switch to the keywords pane, open the keywords list, move it to the right edge of the screen near the top and resize it so the bottom of the keywords pane stretches to the bottom of the screen, click the Sort checkbox and sort the list. At the end it puts the cursor back in the upper left corner of the Editor window.

A few caveats: This uses screen coordinates relative to the Scrivener window, as well as absolute screen coordinates, so the values in the Click and WinMove commands may be different for different monitor sizes and resolutions, in which case you’ll have to use Window Spy (included with AHK) to determine the right values for your computer.

If anyone has questions or needs help getting this going, let me know.

#S:: ; use the Windows + S key combination to open scrivener, open the Keywords dialog, and position it stretched out on the right side of the screen
{ Run Scrivener ; start Scrivener
WinWait, Scrivener ; wait for Scrivener to load
Click 1169, 1120 ; Switch to the keywords pane
Click 1322, 449 ; click on the Keyword menu
Send {Down}{Down}{Down}{Enter} ; select the 3rd item on the menu - show Project keywords
Sleep, 333 ; pause so Windows can catch up
WinMove, Keywords, , 1640, 127, 272, 1008 ; move the Keywords pane to the right side of the screen and resize it
Click 1532, 1115 ; click the Sort checkbox
Click 91, 42 ; click the sort bar at the top of the dialog
Click -1152, 76 ; put the cursor at the top left of the editor pane
return
}

Here’s a simple script that pops up a dialog where you can type the name of a new item, then inserts that item as a child of the currently selected item in the browser. It uses the Ctrl+; key combination to trigger the script. Note that this is only useful if your cursor is at the bottom of a hierarchy in the browser. Note that if your on a folder, the Ctrl+N combo built in to Scrivener does already does this - it inserts a new child item under the folder your cursor is on. In that case, if you use this AHK script, it inserts the new item at the bottom of the lowest part of the hierarchy under that folder. This is a little hard to explain in words, but if you experiment with it you’ll see how it works. It’s a bit confusing because Ctrl+N functions differently in different contexts in the browser.

Let me know if you questions or need help with this.

#IfWinActive, ahk_class QWidget ; only execute if Scrivener is running
^;:: ; this script sets Control + ; to open a dialog where you type the document name, then hits Enter, then Ctrl+Right. In Scrivener this creates a new document indented to the right below the current document.
{InputBox, vfilename, Document Name ; set ctrl+; to pop up input box and get name of new file
Send ^n %vfilename% {Enter} ^{Right}
}

This macro puts a toolbar overlay on the Scrivener window, with buttons that run the alt-key combos for “Character Map” and “Reveal in Binder”. You’ll probably have to change the height, width and coordinates at Gui, Show. And you’ll want to be running Scriv in full screen, else this overlay will float out on its own.

I’ve got about a 5000-line script running in conjunction with Scrivener, and it all started with something simple like this.

Rgds – Jerome

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
FileEncoding, UTF-8
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
;
SetTitleMatchMode, 2 ; any match
WinGet ScrivName,ID,- Scrivener
;
Gui Scrivo:Default
Gui  -SysMenu -Caption
Gui  font, s8 bold, Verdana
Gui, Add, Button,x20 gCharMap, CharMap
Gui, Add, Button,ys gQReveal, Reveal in Binder
Gui +Owner%ScrivName%
Gui Color, e5e5e5, aaccaa
Gui, Show, h39 w300 x547 y46
Return
;
CharMap:
WinActivate, - Scrivener
Send !e
Send e
return
;
QReveal:
WinActivate, - Scrivener
Send !v 
Send r 
return

Rgds – Jerome

Jerome - this is great! Thanks for posting, it will save me considerable time.

Tom

Okay, it’s been nearly 6 years since this thread’s seen daylight. Time to bump it with a new AutoHotKey script. :smiley:

tl;dr:
This script enables ‘Freewrite Mode’, which will turn your feature-rich Scrivener software into a low-tech “distraction-free” editor that only supports backspace and a select few shortcuts.

Backstory:
The Astrohaus Freewrite is a $600 ‘smart’ typewriter with the following features: 10 line eInk screen, Cherry MX Brown mechanical keyboard, and cloud backups. It adopts an aggressively “distraction-free” “intended only for first drafts” “Hemingway mode” posture, in that the only editing capability it supports is the backspace key. The assumption is that you’ll use the Freewrite to bang out your first draft, and then use some other tool like Scrivener for subsequent drafts.

I have to admit, I’ve always been intrigued by the thing. I can see the use for it for someone like me. Once I get started, I can focus on a task for hours, but it’s the getting started part where I sometimes have to trick myself into cooperating with the flow. However, I couldn’t imagine trying to integrate yet another device into my workflow. Not to mention that price tag for a one-trick pony.

But–I’ve been learning AHK and looking for things to do with it, and the other day I realized–Hey! I already have a Cherry MX Brown mech keyboard, and cloud backups, and I can set Scriv Comp Mode to only view 10 lines–and dark mode is kinda like eInk, right?–so all I have to do to turn my $50 copy of Scrivener into a $600 Freewrite is disable a few keys with AHK!

The Script:
So that’s what this script does. All keys are disabled in Scrivener but the typing and backspace keys. Press F10 to toggle Freewrite Mode on and off.

If you prefer backspace disabled, simply tweak the two lines explicitly called out in the script. That will give you the full Hemingway “only move forward” treatment. :nerd_face:

Ctrl-S Save and Shift-F6 Sync with External folder are allowed, as backups are NEVER distractions. :sunglasses: I’ve also allowed myself Ctrl-U underline for emphasis. Feel free to enable or disable whatever you like.

Breaking this into two scripts greatly simplified the code. It also minimized interaction with Scrivener when Freewrite Mode is off, while maximizing disruption when Freewrite Mode is on. :metal:

Be sure to name the scripts as noted below–Freewrite Mode.ahk and Freewrite Mode_proc.ahk–and keep them both in the same folder. Launch script Freewrite Mode.ahk by hand or add a shortcut to it in your Windows Startup folder. That script does nothing but monitor for F10, so it won’t impact Scrivener.

How to use it:
Get Scrivener set up as you prefer for heads down drafting. (I like Comp Mode with a reduced vertical window.) Put the cursor in the Scrivener editor where you want to start writing. Press F10 and off you go.

This script disables keys and shortcuts when the Scrivener main window is the active window. To navigate to another program, use Win+Tab or Alt+Tab. Or just press F10 to turn Freewrite Mode off.

Note that you can navigate to Quick Ref panels, so if you’ve launched Quick Ref panels prior to pressing F10, you can view notes or research while the main editor is locked down.

Enjoy!


Freewrite Mode.ahk

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
#Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

;--------------------------------------------------------------------------	

SetTitleMatchMode, 2

#IfWinActive, Scrivener

;--------------------------------------------------------------------------	

;	F10 engages Freewrite Mode
f10::

RunWait, Freewrite Mode_proc.ahk
return


Freewrite Mode_proc.ahk


;--------------------------------------------------------------------------	
; Auto-Execute section
;--------------------------------------------------------------------------	

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
#Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

SetTitleMatchMode, 2
SetStoreCapsLockMode, Off ; Necessary to allow capslock

#IfWinActive, Scrivener

;--------------------------------------------------------------------------	
; Progress bars use Scriv html Editor & Text colors
;	File > Options > Appearance > Main Editor > Colors
;
; Default        - Window color = CWffffff / Text Color = CT000000
; Dark Mode      - Window color = CW3f3f3f / Text Color = CTFFFFFF
; Mellow Yellow  - Window color = CW020202 / Text Color = CTffff00
; Solarized Dark - Window color = CW002c36 / Text Color = CTDEDEDE
; Midnight 	 - Window color = CW02141f / Text Color = CTffffff
;	Download Midnight here: https://forum.literatureandlatte.com/t/scrivener-3-1-accessibility/119430

CW_color := "CW02141f"
CT_color  := "CTffffff"

;--------------------------------------------------------------------------	

Progress, b2 fs16 zh0 %CW_color% %CT_color%, Freewrite Mode ON 
Sleep, 750
Progress, Off

;--------------------------------------------------------------------------	
; F10 disables Freewrite Mode
;--------------------------------------------------------------------------	

f10::

Progress, b2 fs16 zh0 %CW_color% %CT_color%, Freewrite Mode OFF 
Sleep, 750
Progress, Off
ExitApp

return

;--------------------------------------------------------------------------	
; Shortcuts to allow
;--------------------------------------------------------------------------	

$^u::Send ^u  ; Allow Ctrl-U underline (to disable underline, comment out this line)

$^s::Send ^s  ; Allow Ctrl-S manual save

$+f6::Send +{f6}  ; Allow Sync with External Folder Now

; Allow shift + letters
$+a::Send +a
$+b::Send +b
$+c::Send +c
$+d::Send +d
$+e::Send +e
$+f::Send +f
$+g::Send +g
$+h::Send +h
$+i::Send +i
$+j::Send +j
$+k::Send +k
$+l::Send +l
$+m::Send +m
$+n::Send +n
$+o::Send +o
$+p::Send +p
$+q::Send +q
$+r::Send +r
$+s::Send +s
$+t::Send +t
$+u::Send +u
$+v::Send +v
$+w::Send +w
$+x::Send +x
$+y::Send +y
$+z::Send +z

; Allow shift + symbols/numbers
$+-::Send +{-}
$+;::Send +{;}
$+/::Send +{/}
$+1::Send +1
$+2::Send +2
$+3::Send +3
$+4::Send +4
$+5::Send +5
$+6::Send +6
$+7::Send +7
$+8::Send +8
$+9::Send +9
$+0::Send +0

;--------------------------------------------------------------------------	
; Shortcuts to disable
;--------------------------------------------------------------------------	

$^Backspace::Send {Backspace} ; Allow backspace, but not Ctrl-backspace (To disable backspace, comment out this line) 

; Disable modifiers + letters
$*a::Send a
$*b::Send b
$*c::Send c
$*d::Send d
$*e::Send e
$*f::Send f
$*g::Send g
$*h::Send h
$*i::Send i
$*j::Send j
$*k::Send k
$*l::Send l
$*m::Send m
$*n::Send n
$*o::Send o
$*p::Send p
$*q::Send q
$*r::Send r
$*s::Send s
$*t::Send t
$*u::Send u
$*v::Send v
$*w::Send w
$*x::Send x
$*y::Send y
$*z::Send z

; Disable modifiers + symbols
$*'::Send {'}
$*-::Send {-}
$*"::Send {"}
$*(::Send {(}
$*)::Send {)}
$*,::Send {,}
$*/::Send {/}
$*;::Send {;}
$*[::Send {[}
$*\::Send {\}
$*]::Send {]}
$*^::Send {^}
; $*`::Send {`}
$*{::Send {{}
$*|::Send {|}
$*}::Send {}}
$*~::Send {~}
$*+::Send {+}
$*<::Send {<}
$*=::Send {=}
$*>::Send {>}

; Disable modifiers + numbers
; $*1::Send 1
$*2::Send 2
$*3::Send 3
$*4::Send 4
$*5::Send 5
$*6::Send 6
$*7::Send 7
$*8::Send 8
$*9::Send 9
$*0::Send 0

;--------------------------------------------------------------------------	
; Keys to disable
;--------------------------------------------------------------------------	

*Home::
*End::

*PgUp::
*PgDn::

*Up::
*Down::
*Left::
*Right::

*WheelDown::
*WheelUp::
*WheelLeft::
*WheelRight::

*LButton::
*RButton::
*XButton1::
*XButton2::

; *Backspace::  ;  To disable backspace, remove the comment on this line 
*Delete::
*Insert::

*Ctrl::
*LCtrl::
*RCtrl::

*Alt::
*LAlt::
*RAlt::

*AppsKey::

*Escape::

*f1::
*f2::
*f3::
*f4::
*f5::
*f6::
*f7::
*f8::
*f9::
*f10::
*f11::
*f12::
*f13::
*f14::
*f15::
*f16::
*f17::
*f18::
*f19::
*f20::
*f21::
*f22::
*f23::
*f24::

return

@jwhitten created a script that assigns highlighter colors to hotkeys. Thanks John!

1 Like