Preserve clipboard text while performing another copy/paste opperation

I’m trying to do something that seems pretty simple but I can’t get it to work. You may recognize the script from my previous question about reading application specific items from the clipboard http://macscripter.net/viewtopic.php?id=43801 (thanks to Shane Stanley that problem is solved)

This script loads an application specific object (plist) to the clipboard then pastes it into the specified application:

use framework "Foundation"
use framework "AppKit"
use scripting additions


set thePosixPath to POSIX path of (choose file with prompt "Choose a text file:" of type {"plist"})
set theApp to "Final Cut Pro"
set theAppPasteboardUTI to "com.apple.flexo.proFFPasteboardUTI"

its readAppSpecificObj:thePosixPath usingApp:theApp usingUTI:theAppPasteboardUTI

on readAppSpecificObj:thePosixPath usingApp:theApp usingUTI:theAppPasteboardUTI
	set OBJcDict to current application's NSDictionary's dictionaryWithContentsOfFile:thePosixPath
	set theNSPasteboard to current application's NSPasteboard's generalPasteboard()
	theNSPasteboard's clearContents()
	theNSPasteboard's setPropertyList:OBJcDict forType:theAppPasteboardUTI
	theNSPasteboard's writeObjects:OBJcDict
	tell application theApp
		activate
	end tell
	tell application "System Events"
		delay 0.1
		keystroke "v" using command down
	end tell
end readAppSpecificObj:usingApp:usingUTI:

and this script gets text from the clipboard:

use framework "Foundation"
use framework "AppKit"
use scripting additions

its getClipboardText()

on getClipboardText()
	set theNSPasteboard to current application's NSPasteboard's generalPasteboard()
	set theClipboardText to theNSPasteboard's stringForType:(current application's NSPasteboardTypeString)
	return theClipboardText as text
end getClipboardText

This is the script I’m trying to get to work. I want it to set the text on the clipboard to a variable, load another object to the clipboard and paste it into the specified app, then put the text variable back on the clipboard. Currently the script does nothing (except keep the text on the clipboard):

use framework "Foundation"
use framework "AppKit"
use scripting additions


set thePosixPath to POSIX path of (choose file with prompt "Choose a text file:" of type {"plist"})
set theApp to "Final Cut Pro"
set theAppPasteboardUTI to "com.apple.flexo.proFFPasteboardUTI"

its readAppSpecificObj:thePosixPath usingApp:theApp usingUTI:theAppPasteboardUTI

on readAppSpecificObj:thePosixPath usingApp:theApp usingUTI:theAppPasteboardUTI
	
	set theNSPasteboard to current application's NSPasteboard's generalPasteboard()
	set theClipboardText to theNSPasteboard's stringForType:(current application's NSPasteboardTypeString)
	
	set OBJcDict to current application's NSDictionary's dictionaryWithContentsOfFile:thePosixPath
	theNSPasteboard's clearContents()
	theNSPasteboard's setPropertyList:OBJcDict forType:theAppPasteboardUTI
	theNSPasteboard's writeObjects:OBJcDict
	tell application theApp
		activate
	end tell
	tell application "System Events"
		delay 0.1
		keystroke "v" using command down
	end tell
	
	theNSPasteboard's clearContents()
	theNSPasteboard's writeObjects:{theClipboardText}
end readAppSpecificObj:usingApp:usingUTI:

Any ideas?

Try removing that line – the previous line should set the clipboard’s contents.

Actually all I need to do was add a 1 second delay after

keystroke "v" using command down

But you’re right.

theNSPasteboard's writeObjects:OBJcDict

That line is not needed.

Another matter is preserving the clipboard’s contents irregardless of its type(s) (not just text). Is this an easy thing to do?

Mmm. It might be possible. You’d need to get the types, then loop through using dataForType:. After, you’d probably use declareTypes:owner: followed by setData;forType: with each of the data items. But that probably assumes none of the items are actually just promised items.