Scripting the clipboard

Hi,
till now i tried javascript and UI scripting to paste formatted text from safari, but the results i get are still and always in pain text, without format. Formatting is working with tags, right ? a bit of reference material would help.
To start off:

tell application "Safari" to set theContent to do JavaScript "unescape(getSelection())" in document 1
--tell application "System Events" to keystroke "c" using command down
--set the clipboard  to theContent  


tell application "TextEdit" to make new document with properties {text:theContent}

Hello! :slight_smile:

It is usually the opposite way around.

This worked for me right now, copying from your post with it, then making a new TextEdit document, which I told to be rich text, and pasting it in. I even got the background colour!


tell application "Safari"
	activate
	tell application id "sevs" to keystroke "c" using {command down}
end tell

Hi
Ah. :confused:
i overlooked that “Textedit” converts the contents into plain text. Now i’ll try with some other editors i have.
And Yes, UI scripting definitively works.
Uff.unfortunately i dislike UI scripting so much that i would prefer to learn another scripting language instead, like javascript. Ok, im kidding.

Hello

Here is a script which I wrote some times ago. It offers a neat way to force TextEdit to use the RTF format.


on run
	local cod
	my activateGUIscripting()
	tell application "TextEdit"
		activate
		set cod to count documents
		make new document
		repeat until (count documents) > cod
			delay 0.01
		end repeat
		# Grab the status of the lower menu item in the menu "Format". This item is "Table" which is greyed if the format in use is Text.
		tell application "System Events" to tell process "TextEdit" to tell menu bar 1 to tell menu bar item 5 to tell menu 1 to tell menu item -1 to enabled
		# If the item is greyed, trigger the 4th menu item which in this case is "Set format to RTF"
		if not result then tell application "System Events" to tell process "TextEdit" to tell menu bar 1 to tell menu bar item 5 to tell menu 1 to click menu item 4 (* switch from Text to Rich Text (RTF) *)
		tell document 1
			my raccourci("TextEdit", "v", "c") # Paste
			set font of characters to "Courier"
			delay 5
			my raccourci("TextEdit", "x", "c") # Cut
		end tell
		--close document 1 without saving
	end tell -- TextEdit
end run

--=====

on activateGUIscripting()
	tell application "System Events"
		if not (UI elements enabled) then set (UI elements enabled) to true (* to be sure than GUI scripting will be active *)
	end tell
end activateGUIscripting

--=====
(*
==== Uses GUIscripting ==== 
*)
on raccourci(a, t, d)
	local k
	activate application a
	tell application "System Events" to tell application process a
		repeat with k in t
			if d = "c" then
				keystroke (k as text) using {command down}
			else if d contains "c" then
				if d contains "s" then
					if d contains "k" then
						keystroke (k as text) using {command down, shift down, control down}
					else
						keystroke (k as text) using {command down, shift down}
					end if
				else if d contains "k" then
					keystroke (k as text) using {command down, control down}
				end if
			end if
		end repeat
	end tell
end raccourci

--=====

Yvan KOENIG (VALLAURIS, France) vendredi 2 novembre 2012 21:41:18

:lol:
.more UI Scripting. But you’re right, the end-result counts. Mercì beaucoup !

Another way, but not so neat is of course to tell TextEdit to use RTF as default.

But I have just snagged Yvan’s Script !

Mercı Beacoup Yvan!