Pasting text at insertion point

I understand how to

set contents of text field "thisField" to thisVariable

but what if I want to have a button set text at an insertion point between words that already exist in the text field?
The “paste” command doesn’t seem to be an option. Or is it?
If it helps, I don’t know what text might already be in the text field or where the variable may need to be pasted by the user.

Hey bud, I figured this out, but there’s a bit of a hitch that I haven’t found a way around yet. I’m no obj-c whiz but I’m pretty sure that this can be done cleaner, so I’ll post what I’ve found does work, and then what I think “should” work for someone with a bit more experience to figure out.

Step 1 >>
In IB, hold the ‘control’ key and click on your button. Drag the little blue line that appears to the “First Responder” item in your instances pane, and let go when it’s highlited. In the info window, go to the “Connections” pane, and a list of possible connections will be available under “target >”. Scroll down to “paste:”, select it, and then hit the “connect” button at the bottom. Now, whenever you hit your button it will paste whatever is in the pasteboard (the clipboard) into the active text field, wherever the cursor is. Essentially, you’re using the button to press “command-V”.

Step 2 >>
The hitch is that you can’t use the button to execute an applescript event AND to handle a connection at the same time. So, you’ll need to use some other handler in a separate event to set the contents of the pasteboard, and then use the button to insert it. For example purposes, I used a second button, and attached it to this code…

on clicked theObject
	if name of theObject is "setPB" then
		set preferred type of pasteboard "general" to "string"
		set contents of pasteboard "general" to "qwerty"
	end if
end clicked

So, you click on the button “setPB” which sets the contents of the pasteboard to “qwerty”. Then, you have to click on the second button (the one connected to ‘paste:’) which inserts “qwerty” wherever the cursor is. You could get creative with how you set the pasteboard, like in an ‘on open’ event, in an ‘on changed’ event for a text field, with a “on choose menu item” event, etc.

I think that there is a better way to do this, but I know very little about obj-c and can’t figure out what the call method command would be to perform the ‘paste:’ command via applescript. Perhaps one of the obj-c guys here could make something like the following a reality…

on clicked theObject
	if name of theObject is "insertText" then
		set preferred type of pasteboard "general" to "string"
		set contents of pasteboard "general" to "qwerty"
		call method "PasteFromPasteboard:" with arguments "generalPasteboard"
	end if
end clicked

Essentially, I’d want a call method command to get rid of step 1 above so the pasting can be done entirely by applescript code rather than using the connection of the button to the paste command, as outlined above. The two-step method above worked for me, but it would be cleaner and more streamlined to have it written like this last example.

Hope this gets you started…

I kinda like your idea, but it sure would be nice if everything could be done with one button.
This just occured to me, so I haven’t tried it yet…
What about placing an Image View in the window, set it to Enabled & Hidden, Give it a Mouse Entered event to set the pasteboard to the desiredText… and then…
Place the button in the middle of the hidden Image View, and make the connection that you mentioned? That almost sounds do-able. I’ll have to try that later. You have an excellent idea started, even if my idea fails.
On to other depressing things…
I used your code above replacing the names with my own.
Here is what I have:
on open names
set theFile to item 1 of names
tell application “Finder”
set fileType to file type of theFile
end tell
if fileType is “TEXT” then
set theContent to read theFile as string
set theContent to “The file was not a ‘TEXT’ file!”
end if
set contents of text view “view” of scroll view “view” of window “main” to theContent
end open
The icon accepts the drop, and the application opens, but all I see is my applications name in the menu. Where the heck is my main window? Am I supposed to make a connection somewhere in order to see the rest of my app?

Sorry about asking the “Drag and Drop” Question here…
I thought we were back at my other post
-G <—javascript:emoticon(‘:oops:’)