Get Hardware (Mac) Address through Applescript

Hi All,

How can I get Hardware (Mac) Address using applescript. Please tell me how can I do it. I have searched in the dictionary but I found nothing there.

Why I need it, I have developed some tool for InDesign & Quark User. I want to add the mentioned module in my application, so that as user will run the tool it will first validate the Hardware address, and if it match then application will run otherwise it will show a piracy message.

Thanks
Rajeev

Hi All

Below is the code but not working at all.

Please give some inputs.


get system info
set theETH to get primary Ethernet address as reference
set theETH to theETH as string
display dialog theETH

Thanks
Rajeev

Hi Rajeev,

two ideas:

set theETH to  primary Ethernet address of (get system info)

or

set theETH to (do shell script "ifconfig | grep ether | awk '{print $2}'")

Both are working here on my machine.

D.

Hi Dominik,

with the line above you get the MAC addresses of all ports. In this case it would be better
to specify the hardware (Ethernet = en0) address

set theETH to do shell script "ifconfig en0 | awk '/ether/ {print $2}'"

For a string without colons (e.g for some .plist file paths) you can use

set myMACaddress to words 2 thru -1 of (do shell script "ifconfig en0 ether | grep -i ether" as string) as string

I like this version (which I use in 10.4 only) because it’s faster than a shell call:


set MAC to primary Ethernet address of (get system info)
set B to button returned of (display dialog "Your MAC address is: " & MAC buttons {"To Clipboard as is", "No Colons -> Clip", "Done"} default button 3)
if B is "To Clipboard as is" then
	set the clipboard to MAC
else if B is "No Colons -> Clip" then
	set tid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to ":"
	set MAC to (text items of MAC)
	set AppleScript's text item delimiters to tid
	set the clipboard to MAC as text
end if

Hi Adam,
how do you time the scripts

by eye they seem just as fast as each other

set MAC to characters 8 thru -2 of (do shell script "ifconfig en0 ether | grep -i ether") as string
set B to button returned of (display dialog "Your MAC address is: " & MAC buttons {"To Clipboard as is", "No Colons -> Clip", "Done"} default button 3)
if B is "To Clipboard as is" then
	set the clipboard to MAC
else if B is "No Colons -> Clip" then
	set the clipboard to (words 2 thru -1 of (do shell script "ifconfig en0 ether | grep -i ether" as string)) as text
end if

On my machine, doing a shell script often has a latency of about 100 mSec - but variable. I usually time any script speed comparison with Nigel Garvey’s comparison script below. GetMilliSec is an osax. We have it on site (osaxen.com)


main()

on main()
	set lotsa to 500
	-- Any other preliminary values here.
	
	-- Dummy loop to absorb a small observed
	-- time handicap in the first repeat.
	repeat lotsa times
	end repeat
	
	-- Test 1.
	set t to GetMilliSec
	repeat lotsa times
		-- First test code or handler call here.
	end repeat
	set t1 to ((GetMilliSec) - t) / 1000
	
	-- Test 2.
	set t to GetMilliSec
	repeat lotsa times
		-- Second test code or handler call here.
	end repeat
	set t2 to ((GetMilliSec) - t) / 1000
	
	-- More test loops here if required.
	
	-- Timings.
	return {t1, t2, t1 / t2}
end main

Thanks,

I see what you mean.

For some reason I could not open the read me, it comes up as a classic app ??

GetMilliSec works in both Classic and OS X and it’s author has never updated the read me, I guess. There’s no mystery though, Mark, it only does one thing: it returns the number of milliseconds since system startup, has no other functions, no arguments of any kind. It’s own latency isn’t bad.


-- usual usage
set A to GetMilliSec
set B to GetMilliSec
B - A --> 2 on my machine.

If you want to use it in an expression, enclose it in parentheses:


(GetMilliSec) - (GetMilliSec) --> small negative number

vis-a-vis latency, this is the best I can figure:


set T to 0
repeat 1000 times
	(GetMilliSec) - (GetMilliSec)
	set T to T + result
end repeat
T / 1000 --> ~  0.5 on mm