Create a new Wireless Network (ad-hoc) on Mac OS using an applescript.

AppleScript Editor

I know it’s easy Blush.png 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:

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: Sticking Out Tongue.png

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 (Gasp.png 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? Confused.png

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 Wink.png.

Gasp.png Additional Script to disconnect from current Wireless Network:

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

  • Maxi730

    It gives me this error:
    Result:
    error “System Events got an error: Can’t get menu item “Create Network…” of menu 1 of menu bar item 8 of menu bar 1 of process “SystemUIServer”.” number -1728 from menu item “Create Network…” of menu 1 of menu bar item 8 of menu bar 1 of process “SystemUIServer”

    • umm.. what Mac OS version are you using? Is it Snow Leopard? |・。・)?

      • craig

        this is the script I’m using, well, with my own network name and password, and it works most of the time, but occasionally I’ll get the error message in the photo below. Anyone know why?

        –Change to your desired network details.

        –Include quotes.

        –Password must be 5 characters.

        property NetworkName : “your-network-name”

        property NetworkPassword : “paswd”

        tell application “System Events”

        end tell

    • It’s because wifi was turned off, you need to turn wifi on before running the script or add this code before your script:

      tell application “System Events”
      tell process “SystemUIServer”
      tell menu bar 1
      set menuExtras to (value of attribute “AXChildren”)
      tell menuExtras
      set bluetooth to (first item whose (value of attribute “AXDescription”) contains “bluetooth”)
      end tell
      tell bluetooth
      perform action “AXPress”
      tell menu 1
      if name of menu item 1 ends with “Off” then
      click menu item “Turn Bluetooth On”
      else
      — just close the menu if Bluetooth is already on
      tell bluetooth to perform action “AXPress”
      end if
      end tell
      end tell

      end tell

      • TomiV

        I did that and I still get this error:
        error “System Events ha detectado un error: No puede obtenerse menu item “Create Network…” of menu 1 of menu bar item 6 of menu bar 1 of process “SystemUIServer”.” number -1728 from menu item “Create Network…” of menu 1 of menu bar item 6 of menu bar 1 of process “SystemUIServer”

  • jeepguy_1980

    Lion doesn’t seem to like the line
    “perform action “AXPress” of menu item CreateMenuName of menu 1″

    What is the menu number in Lion?

    • jeepguy_1980

      Nevermind. I had to change “Airport Menu Extra” to “Wi-Fi, four of four bars.”

      • I’m using Snow Leopard when I created the script. so… I’m not sure about “Lion”. At least you figure it out already ・~ヾ(゚ー^*) 

  • Stefan

    Tested Version for (German-)Lion:

    Check Menu-Names for other Languages:
    -> change this line “repeat with the_menu from 1 to the count of menu_extras”
    -> to ” repeat with the_menu from 0 to the count of menu_extras”
    -> with the Error-Message you can get the language specific name

    property NetworkName : “Network”
    property NetworkPassword : “password”

    property WifiName : “Wi-Fi, Wi-Fi ist nicht aktiviert”
    property WifiNameActive : “Wi-Fi, Keine Signal”

    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 WifiName then exit repeat
                end repeat
                tell menu bar item the_menu
                    perform action “AXPress”
                    if title of menu item 2 of menu 1 is “Wi-Fi aktivieren” then
                        perform action “AXPress” of menu item “Wi-Fi aktivieren” of menu 1
                        perform action “AXPress”
                    end if
                end tell
               
                repeat with the_menu from 1 to the count of menu_extras
                    if item the_menu of menu_extras is WifiNameActive then exit repeat
                end repeat
                tell menu bar item the_menu
                    perform action “AXPress”
                    delay 0.2
                    perform action “AXPress” of menu item “Netzwerk anlegen …” of menu 1
                end tell
            end tell
            repeat until exists window 1
                delay 0.5
            end repeat
            tell window 1
                click pop up button 2
                click menu item 4 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

    • Thanks for pointing it out Stefan (>ω<)ノ
      I’m sure it will come in handy for the others . . .

    • I see you greyed out the click button one.

      I found by adding changing the order this script will work fully automatic 🙂

      set value of text field 1 to NetworkName

      click button 1

  • Brian Naess

    Any thoughts on how to get this to work with Lion AND using an internet connection derived from sharing a VM powering a USB wifi device, like what’s outlined here: http://atastypixel.com/blog/using-a-virtualbox-vm-to-operate-a-network-device-for-your-mac/

    • Hmm.. Since I’m not using Mac OS Lion, I don’t know much either.. but maybe you can tweak around the phrases or texts in the applescript as pointed by others in previous comments.

  • Joshua Hecker

    For anyone who is interested, the fix for Lion requires changing this line:

    if item the_menu of menu_extras is “Airport Menu Extra” then exit repeat

    to this line:

    if item the_menu of menu_extras contains “Wi-Fi” then exit repeat

    • Thanks for pointing it out Joshua!

    • morganjenks.com

      along with the above change, I edited the sequence of commands to the ad-hoc set-up window in order to get results on 10.7.4. the delay was necessary because the automator was trying to fill the text fields before they appeared in the window

      tell window 1

      end tell

  • Zstephenson1

    I get the error message 
    System Events got an error: Access for assistive devices is disabled.
    What am I doing wrong?  I’m using Lion on a Mac Mini

    • Hi Zstephenson,

      It seems that the feature is somewhat disabled in your device, try to enter the following command from Terminal:

      sudo touch /private/var/db/.AccessibilityAPIEnabled

  • Morganjenks.com

    Thank you!!!! 😀 I’ve got a Mac mini mounted in a rack for playing concerts, and this script will allow me to connect with my iPad at strange venues!

  • Mountain Lion 10.8.2 Italian

    this works for me

    property CreateMenuName : “Crea Network…”

    property NetworkName : “moonbox”

    property NetworkPassword : “rajan”

    tell application “System Events”

    end tell

  • I was setting up a Mac Mini that needed to create it’s own ad hoc network on startup, or join it if it already existed. I added a little bit to the above code to accomplish that and it seems to work. This is my first foray into Applescript, and can hardly bear the thought of spending more time with it but here it is: https://github.com/dugweb/AutoAdHoc/blob/master/AutoAdHoc

    Thanks for sharing your code.

  • ron ross

    Will any of these codes below work well with 10.8.5? Just updated from Lion and didn’t know if i needed to change anything in the code.
    Thanks for any help you guys can offer.

Search