Quick clipboard query, help required please...

Hi All,

I’ve been trying to write a quick script to return a list, of data copied to the clipboard from Excel, back to the clipboard.

Let’s say in cells A1 to A4, in Excel, I have the values A,B,C,D, one in each cell. I then select those cells and ‘copy’ them to the clipboard. I then run the script below:-

set x to paragraphs of (the clipboard) as list

In the ‘Results’ window of the Script Editor I get the result below (which I can copy to the clipboard).

{“A”,“B”,“C”,“D”}

What I’d like to be able to do is set the clipboard to that value or the variable ‘x’.
I’ve tried a few variations (such as text, string, list etc) but as yet I’ve not been able to do it.

Please can someone point me in the right direction.

Thanks in advance,

Nick

Hello.

I am not absolutey sure if I understood the question, but I gather that you want to get “{A,B,C,D}” back onto the clip board. I hope I am right.

set AppleScript's text item delimiters to ""
set x to (the clipboard)
set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, tab}
set Y to text items of x
set AppleScript's text item delimiters to ","
set Y to Y as text
set AppleScript's text item delimiters to tids
set the clipboard to "{" & Y & "}" as text

Thanks for the help McUsrll, yep you were thinking along right lines.

I’d come up with pretty much the same :-

set x to paragraphs of (the clipboard) as list

set y to ""
set listCount to count of x

repeat with i from 1 to listCount
	
	set y to y & quote & (item i of x) & quote
	
	if i ≠ listCount then
		set y to y & ","
	end if
	
end repeat

set the clipboard to "{" & y & "}"

My query was more that if I select the cells and run this:-

set x to paragraphs of (the clipboard) as list

I can see what I need in the ‘Result’ window of the Script Editor and I wondered if it were possible to get that ‘result’ back onto the clipboard without having to write the sort of routines we have.

Thank you once again for your time on this.

Nick

Hello.

You can actually make it somewhat shorter:

set x to words of (get the clipboard)
try
	set Y to text 0 of x
on error e
	set the clipboard to text 21 thru -2 of e
	# this works with english error messages only
	-- adjust the front offset for other languages
end try

Fantastic! :slight_smile:

What a great idea, catch the error and use that.:lol:

Thanks for your time McUsrll, a really nice little solution.
Gonna have to remember that trick in future.

Nick

Well, now you have learned that trick too. :slight_smile:

I learned it here.

Enhanced version which is no longer localization dependant :

set x to words of (get the clipboard)
try
	set Y to text 0 of x
on error e
	set the clipboard to text (offset of "{" in e) thru -2 of e
	# this works with every languages which I tested
end try

KOENIG Yvan (VALLAURIS, France) lundi 14 octobre 2013 15:45:53

Thanks for the updated version Yvan. :slight_smile: