Copy IP to clipboard

Hello MacScripters,
thank you very much for this helpful and patient community.

Could you please help me out ? Thank you very very much !!

I want to copy the IP Address to the clipboard,
and then show it in an AppleScript Studio Text Field named “IP”.

This is my Shell script I am referring to:

#!/bin/sh

ifconfig | grep "inet " | grep -vn 127.0.0.1 | cut -c9-21 | head -1 | pbcopy

This is my AppleScript:

property foo : "foo"
on clicked
	do shell script "/Applications/Utilities/USE.app/Contents/Resources/USE.app/Contents/Resources/copyIP.sh"
	set foo to the clipboard
	tell window "Interface"
		set contents of text field "IP" to foo as string
	end tell
end clicked

Everthing works fine except that the Text copied
to the clipboard is two lines long, with one line
containing the address and the other containing
nothing. I suppose it has to do with unix line
endings but I am not sure.

But therefore also the Text Field “IP” does not
contain the one preferred line, but two.

I think you can just:

set contents of text field "IP" of window "Interface" to ¬
	(do shell script "ifconfig | grep 'inet ' | grep -vn 127.0.0.1 | cut -c9-21 | head -1")

This doesn’t return an extra return here… But if it does for you, you can:

set contents of text field "IP" of window "Interface" to ¬
	paragraph 1 of (do shell script "ifconfig | grep 'inet ' | grep -vn 127.0.0.1 | cut -c9-21 | head -1")

Thank you so much jj !

I also managed the script to copy it to the clipboard:

property x : "Hello"
on clicked
	set contents of text field "IP" of window "Interface" to (do shell script "ifconfig | grep 'inet ' | grep -vn 127.0.0.1 | cut -c9-21 | head -1")
	tell window "Interface"
		set x to contents of text field "IP"
		set the clipboard to x
	end tell
end clicked
word 2 of (do shell script "ifconfig en0 inet | grep inet")