AppleScript to paste highlighted text into textedit

I am a complete newbie. I am trying to make it very easy to take notes off documents and websites. My goal is to write a script that will allow me to highlight text in a webpage, or document, etc and then use a hotkey to copy that text, paste it into textedit, have text edit hit the return key twice (to create a space between each selection), and return to the original document.

so far i have this:

tell application "System Events" to tell text area 1 of scroll area 1 of front window to if exists then
	set {x, y} to value of attribute "AXSelectedTextRange"
	if x ≤ y then keystroke "c" using {command down}
end if
activate application "TextEdit"
tell application "System Events"
	tell process "textedit"
		keystroke "v" using {command down}
	end tell
end tell

this gets me to paste the text, but i can’t figure out how to have it "return"x2 and i don’t know how to get back to the original application. further, i don’t know how to set it up so i can have a hotkey combination activate this script (something like cmd+fn+c). any and all help will be vastly appreciated. THANK YOU

Sean

tell application "System Events"
	tell text area 1 of scroll area 1 of front window of front process to if exists then
		set {x, y} to value of attribute "AXSelectedTextRange"
		if x ≤ y then keystroke "c" using {command down}
	end if
end tell

tell application "TextEdit"
	launch
	tell front document to set it's text to it's text & (the clipboard) & (ASCII character 10) & (ASCII character 10)
end tell

That TextEdit part should help you out. I’m a little confused by the first part though; You are putting stuff into TextEdit even if there isn’t a text area or selected text.

You might be interested in FastScripts.

Thanks a lot!

but the ascii character 10 part didnt seem to work…
this is what i have now:

tell application "System Events" to tell text area 1 of scroll area 1 of front window to if exists then
	set {x, y} to value of attribute "AXSelectedTextRange"
end if
tell application "System Events" to keystroke "c" using {command down}
activate application "TextEdit"
tell application "System Events"
	tell process "textedit"
		keystroke "v" using {command down}
	end tell
end tell
activate front window

so now i just need a way to insert a couple of “return key” hits (ascii character 13?)

and to respond to your confusion
I copied that from someone else’s script. I believe it it specifically meant to prevent you from pasting anything if nothing is selected, essentially it requires that the selected text area be greater than 0.

but i removed it, cause it seems a little redundant

That’s not doing anything either.

Do you have any problems with this?

tell application "System Events" to keystroke "c" using {command down}

tell application "TextEdit"
	launch
	tell front document to set it's text to it's text & (the clipboard) & (ASCII character 10) & (ASCII character 10)
end tell

hmmm, everything works well, except now it is pasting the previous thing i had highlighted and copied instead of the current one. i wonder if there is a lag in copying to the clipboard, do i need to insert a slight delay between telling it to copy and telling textedit to paste?

one last question, would i be able to substitute “pages” or “abiword” or “neooffice” for “textedit” if i preferred, or is it best to use textedit because it is so native to applescript?

otherwise it seems to be working great! thanks a ton!

Sean

You can try adding a delay. Also, for Pages you have to use body text:

tell application "System Events" to keystroke "c" using {command down}
delay 2 -- seconds

tell application "Pages"
	launch
	tell front document to set it's body text to it's body text & (the clipboard) & (ASCII character 10) & (ASCII character 10)
end tell

Offhand, I don’t know how well Abiword or NeoOffice would work. I doubt they are properly scriptable; You would have to go back to using keystroke to paste, and then getting the proper application to come back to the front.

Ok. it works fine when simply testing it in the script editor, but when I run it with spark hotkey’s in any other application, the clipboard does not seem to copy what i have highlighted. Or rather, it will do so the first time (in fact i can see the “edit” tab briefly flash). but then with each subsequent hotkey press all it does is continue to paste that first clipboard copy regardless of what i have highlighted. . . could it be that after it pastes to textedit that the clipboard needs to be cleared each time? i was so excited that it was going to work too. thanks again for your help

sean

Hi.

Try using ‘activate’ instead of ‘launch’. Normally, an application accessing the clipboard should be frontmost at the time.

By the way, in AppleScript, as in English, there’s no apostrophe in the possessive pronoun “its”. :wink:

Thanks for the help guys… I am still having problems, though. See it is coming from the first part of the script which is telling system properties to “copy” via cmd+c … apparently that action is not actually taking place, or if it is that action is not copying anything to the clipboard, because when i run the script my clipboard stays the same, hence what it pastes into textedit stays the same. is there a different way to copy text to the clipboard with scripts? or is there a way to erase the clipboard with scripts? or are there any other ideas?

i should add, sometimes it does copy to the clipboard. sometimes it doesn’t. and i am pretty sure that those times that it does not i hear a little “error” beep from the computer. i dunno if that helps anything.

thanks again!

My guess is that when you run the script, the application with the selected text is pushed from the frontmost slot by the process used to run the script, and therefore the Command-“c” is applied to the invisible process rather than to the application. That would explain the beep and the lack of change to the clipboard. I get a similar result using Script Menu.

I don’t use hot keys myself, so I don’t know how you’d get round that, but the following hack works for me when run from Script Menu on my machine. Hopefully it’ll work with, or be adaptable for, your hot key system.

-- Identify the "frontmost application" and make sure it's frontmost!
set displayedName to displayed name of (info for (path to frontmost application))
tell application "System Events"
	tell (first application process whose displayed name is displayedName)
		set frontmost to true
		set appName to its name
	end tell
	-- Use Command-"c" to copy the selection to the keyboard.
	keystroke "c" using {command down}
end tell
-- No feedback from keystrokes, so simply allow time for the clipboard contents to change.
delay 0.2 -- May need to be longer.

-- While it's still frontmost, get the application to read the clipboard into an AppleScript variable.
tell application appName to set theText to (the clipboard)

-- Write the text from the variable into the frontmost TextEdit document.
tell application "TextEdit"
	activate
	make new paragraph at end of front document with data theText & (ASCII character 10) & (ASCII character 10)
end tell

I don’t have the hot keys set up but this script can handle the cut and paste with the returns and send you right back to your other program.

tell application "System Events"
	activate application "TextEdit"
	delay 2
	keystroke tab using {command down}
	delay 1
	keystroke "c" using {command down}
	keystroke tab using {command down}
	delay 1
	keystroke "v" using {command down}
	keystroke return & return
	delay 1
	keystroke tab using {command down}
end tell

See if that works out for you.

Brian

Hey guys. Again thank you so much for the help. here is what I have come up with that seems to be working, for now .


-- Identify the "frontmost application" and make sure it's frontmost!
set displayedName to displayed name of (info for (path to frontmost application))
tell application "System Events"
    tell (first application process whose displayed name is displayedName)
        set frontmost to true
        set appName to its name
    end tell
    -- Use Command-"c" to copy the selection to the keyboard.
    keystroke {command down}  -- I know this is a total redundancy, but this seems so have cleared up the error of not copying occasionally which resulted in an error beep and pasting the previous clipboard to textedit
    keystroke "c" using {command down}
end tell
-- No feedback from keystrokes, so simply allow time for the clipboard contents to change.
delay 0.4 -- May need to be longer.
-- While it's still frontmost, get the application to read the clipboard into an AppleScript variable.
tell application appName to set theText to (the clipboard)
delay 0.4
-- Write the text from the variable into the frontmost TextEdit document.
tell application "TextEdit"
    launch -- this allows it to paste in text edit without bringing the application to the front
    make new paragraph at end of front document with data theText & (ASCII character 10) & (ASCII character 10)
end tell


i then used Spark and created a hotkey for it and yeah, it seems to be working for now. and its going to be a HUGE time saver. so again, thank you everyone for your help. I am sure there are more fun things I code try to do with this in terms of adding bullets or dashes or creating an outline format… any other ideas or suggestions to make it even better would be welcomed as well.

Thanks again!

Sean

So I am a complete beginner with scripts and I have no idea how tomake them work, how can I use this script to get highlighted text from skim? I’ve tried to run the script but it didn’t work… Please help!!
thank you so much!

Hello

I didn’t check under 10.5 but under 10.4.11, it returns the name of the script itself which is useless.
It behaves the same if the script is saved as an Application Bundle with ‘stay open’ unchecked.

Yvan KOENIG (from FRANCE mercredi 11 février 2009 16:42:51)

:wink:

Hi Stefan,

I saw that, but here, I tested from Script Menu under 10.4.11 and got the name of the script itself.

Yvan KOENIG (from FRANCE mercredi 11 février 2009 17:56:04)