Paste clipboard contents to open file in a background app?

I am looking for a way, preferably using AppleScript and TextExpander, to paste the clipboard contents to the currently active file in nvALT — without bringing nvALT to the front.

I am trying to paste info as I do web research. Would like to not have to switch back and forth from web browser to nvALT.

I think if you don’t want to foreground the application you would have to update the file it is referencing directly. The nvALT AppleScript dictionary suggests it should use a document based object model, but I can’t get it to reference a document by index, or access the loaded file directly.

Unless someone more familiar with scripting nvALT can suggest something, I can’t see a way to access the info you would need. I suspect you would have to brute force it and program a foreground interaction.

However, once nvUltra is available, it looks like the AppleScripting will support you grabbing the current file path (see the AppleScript example in the docs), and at the point, you could then use AppleScript (and maybe some shell scripting) to insert the content of the clipboard to the end (or start) of the file.

Thanks, yeah I have some TextExpander snippets that use AppleScript and shell scripts to do just that (reference exact file). But the arbitrary “frontmost document” approach seemed unlikely but wasn’t sure.

But cool to know nvU will allow for this.

To clarify, do you mean here that AppleScript and shell scripts reference a file that belongs to TextExpander ? Or were you referring the file used by nvALT to store a note ?

I only invoke the scripts via TextExpander. And to clarify, those scripts reference file paths for the specific documents in the Finder (in the Dropbox directory, which I use for nvALT).

So the scripts do not interact with nvALT at all. And TextExpander is only used to invoke them at will.

OK, I was just making sure I hadn’t missed something before offering a possible AppleScript solution to your request above.

The script below should allow you to either append or insert text into the currently selected note. The main window doesn’t need to be in focus or visible to the user, but it does need to be open (even if it’s just left at the back, underneath other windows from other applications). As such, the script will attempt to open it if it isn’t already, but it won’t take the focus away from whatever you’re currently doing.

use application id "com.apple.systemevents"
use scripting additions

property append : true -- false = insert text at cursor position

property process : a reference to application process "nvALT"
property window : a reference to window "nvALT" of my process
property table : a reference to table 1 of scroll area 1 of my window
property item : a reference to (row 1 in my table whose selected = true)
property text area : a reference to text area 1 of scroll area 2 of my window

if not (my process exists) then return 0
if not (my window exists) then tell ¬
	application id "net.elasticthreads.nv" to ¬
	tell window "nvALT" to set visible to true

set input to the clipboard as text

tell my text area
	if append then
		if value of its attribute "AXNumberOfCharacters" > 0 ¬
			then set input to linefeed & linefeed & input
		set its value to its value & input
	else
		set the value of its attribute "AXSelectedText" to input
	end if
end tell

The property called append (currently set to true at the top of the script) is one that you can change to your preference. If it is set to true, then the clipboard’s contents gets added to the end of the currently selected note as a new paragraph, separated from the text above with a blank line in between. If it is set to false, then the clipboard’s contents is inserted into the currently selected note at the cursor’s present position without adding any additional whitespace.

Hopefully, this will be of some help until the release of nvUltra arrives.