Insert text at cursor?

I’m writing an Applescript Studio app and I’d like to be able to insert tags into my text area at the cursor point. I can set the entire contents of the text view, but I can’t figure out how to insert into it. This seems like an easy thing to do and I’d really appreciate any help I can get. Here’s what I’ve got (the problem line is commented):

on choose menu item theObject
	tell window "mainWindow"
		if theObject is equal to popup button "insertTag" then
			set theTag to title of popup button "insertTag"
			tell text view 1 of scroll view 1 of tab view item "textTab" of tab view "bodyView"
				make new text with data theTag at insertion point  -- *** THIS DOESN'T WORK ***
				--set contents to theTag  -- <-- this works, but it's not what I want to do
				-- set text of selection of contents to theTag -- <-- this also does not work
			end tell
		end if
	end tell
end choose menu item

It seems like the simplest stuff is the hardest to find documentation on with AppleScript. Any help or a pointer to a script that does something similar will be greatly appreciated. Thanks!



Check out my post here…[url=][/url]. This method would work well for you because it seems like your inserted values (“tags”) would probably be predefined. I forgot about this topic and was going to try and figure out how to do this entire thing in one step using obj-c/call-method. I’ll look into that a bit more and post if I find anything new.

Good luck…

Does that thread offer an actual answer? I saw it before I posted but it didn’t look like there was any actual conclusion. I’ve tried to place the code into my script but the line:

call method “PasteFromPasteboard:” with arguments “generalPasteboard”

won’t parse.

Is there really no way to paste something in or insert text into a text view without leaving the cushy warmth of AppleScript? Those Objective C call method lines make me nervous. :wink:

I thought the answer to this one was going to be easy.

Hey there. Sorry, I should have made clear that the first part was the only working part of that post. Technically there is an answer there, but not the one you wanted ;). Unfortunately this is still an unresolved issue that I haven’t figured out yet, and haven’t heard from any of the experienced guys about. It will not be possible in one step without a call method, as applescript does not support a “paste” function.

Say you want to make an html editor, that inserts tags on the press of a button. The only way I’ve gotten this to work is a two-step process. The first step, is setting what the pasteboard contains. For example, use a menu to select the html tag you want to insert…

on choose menu item theObject
	set theObjectName to name of theObject
	set theItemName to (name of current menu item of theObject)
	set preferred type of pasteboard "general" to "string"

	if theObjectName is "Tags_Menu" then
		if theItemName is "html_open" then
			set contents of pasteboard "general" to "<html>"
		else if theItemName is "html_close" then
			set contents of pasteboard "general" to "</html>"
		-- etc...
		end if
	end if
end choose menu item

Then, use a button to actually insert the information. This requires connecting the button to the ‘paste’ action of the first responder of the app (see the other thread for details). Whenever the button is pressed, it just sends a paste: command to the app, which pastes whatever the clipboard contents are into the field at the insertion point.

Ideally, you’d use one control (button, menu item, etc) to… declare what the contents to paste is, set it to the pasteboard, and then paste it…all in one step. That way you could just press the button and be done.

I’m still looking into this, and just don’t have the experience to figure the whole thing out yet. I’ll post it here and at the other thread when I do get it. And sorry, but you’ll have to stray from the “cushy warmth” of AS to get this one done. If no one else chimes in, I’ll try to provide a cut and paste example if I can find it myself.

Take care,

Perhaps this will help. Select some text in the target text view then run this:

To just insert some text at the insertion point without checking for a selection (this will overwrite the selection if something is selected):


[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]

That did it! Exactly what I needed. Thanks so much!

Now you’ve got me curious about what other wonders lie on the other side of the “call method” command…