Whitespace at the end of clipboard text

Hi everybody,

is this the right way to append whitespace at the end of clipboard text?

set my_text to (the clipboard) & " " --whitespace between ""
	set the clipboard to my_text

Thanks.

Hey There,

You can do it that way, but you can make it a trifle simpler:


set the clipboard to (get the clipboard) & " "

-Chris


MacBookPro6,1 · 2.66 GHz Intel Core i7 · 8GB RAM · OSX 10.10.4

Hi Chris,

thank you for the fast response. Works like a charm.

HoHollo

Additional to my last request:
I want to remove all spaces from Richtext in the clipboard and then add one single space at the end of the text.
The script below is working but I wonder if there is a better solution, even with retaining the Richtext format.


--remove spaces from text in clipboard
	set my_richtext to the clipboard
	set {TID, text item delimiters} to {text item delimiters, space}
	set my_richtext to text items of my_richtext
	set AppleScript's text item delimiters to ""
	set the clipboard to my_richtext as text
	set text item delimiters to TID
-- add space at the end of the text in the clipboard
	set the clipboard to (get the clipboard) & space

Thanks.

Doing the job on the text data is easy :

--remove spaces from text in clipboard
set my_richtext to the clipboard
set {TID, text item delimiters} to {text item delimiters, space}
set my_richtext to text items of my_richtext
set AppleScript's text item delimiters to ""
# Fill the clipboard with the stripped text completed by an ending space
set the clipboard to (my_richtext as text) & space
set text item delimiters to TID

Here is a quick and dirty code doing the job and keeping attributes:

tell application "TextEdit"
	activate
	delay 0.2
	make new document at front --with data the clipboard
	delay 0.2
	tell application "System Events" to tell process "TextEdit"
		set frontmost to true
		keystroke "v" using {command down}
	end tell
	delay 0.2
	tell document 1
		set nbchars to count characters
		repeat with i from nbchars to 1 by -1
			if character i is space then set character i to ""
		end repeat
		set nbchars to count characters
		set character nbchars to character nbchars & space
	end tell
	tell application "System Events" to tell process "TextEdit"
		keystroke "a" using {command down}
		keystroke "c" using {command down}
	end tell
end tell

Yvan KOENIG running Yosemite 10.10.4 in French (VALLAURIS, France) samedi 8 aout 2015 19:43:52

This will work with rich text without the need for TextEdit or GUI scripting, although it does require either Yosemite, or being placed in a script library in Mavericks:

use scripting additions
use framework "Foundation"
use framework "AppKit" -- for pasteboard

-- get the clipboard
set theClip to current application's NSPasteboard's generalPasteboard()
-- get list of rich texts off clipboard
set theRichTexts to (theClip's readObjectsForClasses:{current application's NSAttributedString} options:(missing value)) as list
if (count of theRichTexts) = 0 then
	display dialog "No rich text found on the clipboard" buttons {"OK"} default button 1
	error number -128
end if
-- make an editable copy of the first item of the list so we can modify it
set theRichText to (item 1 of theRichTexts)'s mutableCopy()
-- replace spaces
repeat
	-- get the range of a space in the rich text's string, starting from the back
	set theRange to theRichText's |string|()'s rangeOfString:space options:(current application's NSBackwardsSearch)
	-- if it's not found, all done so exit the loop
	if theRange's location = (current application's NSNotFound) then exit repeat
	-- delete the space
	(theRichText's deleteCharactersInRange:theRange)
end repeat
-- add space to end of the rich text, using replace method to pick up styling
set theLength to theRichText's |length|()
theRichText's replaceCharactersInRange:{theLength, 0} withString:space
-- clear clipboard and write new rich text to it
theClip's clearContents()
theClip's writeObjects:{theRichText}

Thanks Shane

I knew the way to get the rich text content but their was no hope for applying the required changes :wink:

Yvan KOENIG running Yosemite 10.10.4 in French (VALLAURIS, France) dimanche 9 août 2015 10:36:35

Thanks Yvan and Shane. That’s great!