I know it’s easy to create a new wireless network on your by simply going to Airport Status Bar at the top screen and hit the “Create Network…”, but what if you would like your mac to automatically create and ad-hoc network every time at startup or maybe making it simple for you to create an ad-hoc network by just running an applescript instead of repeating over and over the same process via the Airport Status Bar. Either way, here’s how you do it:

First, open the AppleScript Editor (Applications & Utilities & AppleScript Editor). A new file will be created automatically for you. Copy & Paste the following code into your AppleScript Editor:

property CreateMenuName : "Create Network…"
property NetworkName : "Net"

tell application "System Events"
	tell process "SystemUIServer"
		tell menu bar 1
			set menu_extras to value of attribute "AXDescription" of menu bar items
			repeat with the_menu from 1 to the count of menu_extras
				if item the_menu of menu_extras is "Airport Menu Extra" then exit repeat
			end repeat
			tell menu bar item the_menu
				perform action "AXPress"
				delay 0.2
				perform action "AXPress" of menu item CreateMenuName of menu 1
			end tell
		end tell
		repeat until exists window 1
			delay 0.5
		end repeat
		tell window 1
			keystroke NetworkName
			click button 1
		end tell
	end tell
end tell

Compile the script, and save it in “Application” file format. You can name the file whatever your like. ex. “CreateAdhoc”

The code above will create a wireless network named “Net”, if you want to change the network name, simply change the string “Net” in the “NetworkName” property above to anything you like.

But what if you want to give a password to the wireless network? Well, we just need to modify the script a lil' bit. Take a look at the modified script below: 

property CreateMenuName : "Create Network…"
property NetworkName : "Net"
property NetworkPassword : "paswd"

tell application "System Events"
	tell process "SystemUIServer"
		tell menu bar 1
			set menu_extras to value of attribute "AXDescription" of menu bar items
			repeat with the_menu from 1 to the count of menu_extras
				if item the_menu of menu_extras is "Airport Menu Extra" then exit repeat
			end repeat
			tell menu bar item the_menu
				perform action "AXPress"
				delay 0.2
				perform action "AXPress" of menu item CreateMenuName of menu 1
			end tell
		end tell
		repeat until exists window 1
			delay 0.5
		end repeat
		tell window 1
			click checkbox 1
			click pop up button 2
			click menu item 1 of menu 1 of pop up button 2
			set value of text field 2 to NetworkPassword
			set value of text field 3 to NetworkPassword
			set value of text field 1 to NetworkName
			click button 1
		end tell
	end tell
end tell

The code above will create a wireless network (ad-hoc) with 40-bit WEP password. To change the wireless password, edit the string “paswd” in “Network Password” property above to anything you like ( Note: the password must be exactly 5 ASCII characters or 10 hex digits.

Now let’s say you want to run the AppleScript at start up, how would you do it? 

Make sure you save the AppleScript in “Application” file format, then open the “System Preferences” and go to “Accounts”. Select your account from the list on the left and go to Login Items, click the plus (+) button and point to your ApplescriptName.app. Close the “System Preferences” when you’re done .

Additional Script to disconnect from current Wireless Network:

property DisconnectFromNet : "Disconnect From Net"

tell application "System Events"
	tell process "SystemUIServer"
		tell menu bar 1
			set menu_extras to value of attribute "AXDescription" of menu bar items
			repeat with the_menu from 1 to the count of menu_extras
				if item the_menu of menu_extras is Airport Menu Extra then exit repeat
			end repeat
			tell menu bar item the_menu
				perform action "AXPress"
				perform action "AXPress" of menu item DisconnectFromNet of menu 1
			end tell
		end tell
	end tell
end tell

(paste the code above to your AppleScript Editor and save as “Application”)