Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Joseph Gray

macrumors newbie
Original poster
May 25, 2017
7
0
Hey, I know nothing about code and found this online
--

-- Toggle Airport Power On and Off

--

if (offset of "On" in (do shell script "networksetup -getairportpower en0")) > 0 then

do shell script "networksetup -setairportpower en0 off"

else

do shell script "networksetup -setairportpower en0 on"

end if

I need to assign this to fn6, because my wifi constantly has to be reloaded (10-20 minutes). Can anyone write the full code so i can copy and paste into applescript and whatever else i need to do.
 
When you say you need to assign this to fn6.
What is fn6 ?.

I'm guessing it is some network port on your system, but as far as I'm aware there is only one WiFi port on Mac's,
Mine has an ID of en1, and it appears yours has an ID of en0.
So your script should check the WiFi port's ID, to make it more universal to any system.
Which I will show you how to do if you clarify what fn6 is.
If fn6 is not a WiFi port, what kind of hardware network port is it ?.

I'm guessing again that it may be a firewall protected port.
 
When you say you need to assign this to fn6.
What is fn6 ?.

I'm guessing it is some network port on your system, but as far as I'm aware there is only one WiFi port on Mac's,
Mine has an ID of en1, and it appears yours has an ID of en0.
So your script should check the WiFi port's ID, to make it more universal to any system.
Which I will show you how to do if you clarify what fn6 is.
If fn6 is not a WiFi port, what kind of hardware network port is it ?.

I'm guessing again that it may be a firewall protected port.

no its the button, so i can reload my wifi by pressing fn and then 6.. and my wifi is en0
 
So to clarify, you have set the keyboard keys fn6 to switch your WiFi off and on again ?.

And you want an AppleScript to toggle the Wifi off and on instead ?.

you do realize that plain vanilla AppleScript's can't receive keyboard events directly.
 
So to clarify, you have set the keyboard keys fn6 to switch your WiFi off and on again ?.

And you want an AppleScript to toggle the Wifi off and on instead ?.

you do realize that plain vanilla AppleScript's can't receive keyboard events directly.
no i want to be able to make it so my mac can turn wifi on and off with fn6. it doesn't now but if it could that'd be really awesome
 
OK, the AppleScript code you posted will toggle the WiFi On and Off.
But you would have to double click the saved AppleScript file to make it run, like any other application.

Or you could setup a keyboard shortcut to run the AppleScript application in System Preferences, as you would for any other application you wanted a keyboard shortcut too.
this is done with the "Keyboard" settings in System Preferences, then the "Shortcuts" Tab, then the "App Shortcuts" item in the list, then clicking the "+" add button.

So save your Script as an application in Script Editor in your Applications folder, then set a keyboard shortcut in System Preferences as described above.
 
OK, the AppleScript code you posted will toggle the WiFi On and Off.
But you would have to double click the saved AppleScript file to make it run, like any other application.

Or you could setup a keyboard shortcut to run the AppleScript application in System Preferences, as you would for any other application you wanted a keyboard shortcut too.
this is done with the "Keyboard" settings in System Preferences, then the "Shortcuts" Tab, then the "App Shortcuts" item in the list, then clicking the "+" add button.

So save your Script as an application in Script Editor in your Applications folder, then set a keyboard shortcut in System Preferences as described above.
I couldn't use enter my script that i saved as an application when i had to select from other. I got confused after the + sign. I might've saved it as an application wrong.
 
I can't help feeling your in the wrong forum here, this is a Mac Programming forum.
Your issue appears to be setting up Keyboard shortcuts to Applications in System Preferences.

Open Script Editor, which can be found in the "Utilities" Folder, which is in the "Applications" Folder.
You should have an empty AppleScript document when you activate Script Editor.

Copy and Paste the code below into the empty Script Editor document.

Code:
set theWiFiPortID to (do shell script "networksetup -listallhardwareports | grep -A 1 Wi-Fi | grep Device | awk '{print $NF}'") as text

if (offset of "off" in (do shell script "networksetup -getairportpower " & theWiFiPortID)) > 0 then
    set theWiFiState to (do shell script "networksetup -setairportpower " & theWiFiPortID & " on")
else
    set theWiFiState to (do shell script "networksetup -setairportpower " & theWiFiPortID & " off")
end if

Then go to the "File" menu of the Script Editor, then the "Save" menu in the "File" menu.
When the save dialog box appears, double click the "Applications" item in the left hand list.
And at the bottom of the save dialog box, there is a "File Format" list, where you should select "Application", insead of "Script", then give your new app a name like "WiFiOnOff.app", and lastley click the "Save" button.

Having done all of the above, you should be able to navigate to your "Applications" folder, and click your new app's icon to switch your WiFi on or Off.

Do all of that first, and then we will move onto Keyboard Shortcuts.
 
  • Like
Reactions: Joseph Gray
I can't help feeling your in the wrong forum here, this is a Mac Programming forum.
Your issue appears to be setting up Keyboard shortcuts to Applications in System Preferences.

Open Script Editor, which can be found in the "Utilities" Folder, which is in the "Applications" Folder.
You should have an empty AppleScript document when you activate Script Editor.

Copy and Paste the code below into the empty Script Editor document.

Code:
set theWiFiPortID to (do shell script "networksetup -listallhardwareports | grep -A 1 Wi-Fi | grep Device | awk '{print $NF}'") as text

if (offset of "off" in (do shell script "networksetup -getairportpower " & theWiFiPortID)) > 0 then
    set theWiFiState to (do shell script "networksetup -setairportpower " & theWiFiPortID & " on")
else
    set theWiFiState to (do shell script "networksetup -setairportpower " & theWiFiPortID & " off")
end if

Then go to the "File" menu of the Script Editor, then the "Save" menu in the "File" menu.
When the save dialog box appears, double click the "Applications" item in the left hand list.
And at the bottom of the save dialog box, there is a "File Format" list, where you should select "Application", insead of "Script", then give your new app a name like "WiFiOnOff.app", and lastley click the "Save" button.

Having done all of the above, you should be able to navigate to your "Applications" folder, and click your new app's icon to switch your WiFi on or Off.

Do all of that first, and then we will move onto Keyboard Shortcuts.
Done, ready to move on. Thank you very much
 
Can you see your new WiFiOnOff app in the "Applications" folder ?.

Have you tried double clicking it to switch your WiFi on and off ?.
 
Sorry for the slow reply Joseph.

OK it seems that Apple has removed the ability to start Applications with kleyboard shortcuts, this includes Apple's own app's as well, it certainly was the case in the past that you could do this, but they have now restricted keyboard shortcuts to application menus only.
Which leaves only a few other options.
First you can press the "Command" and "Space" key, to bring up "Spotlight".
By typing in "W" in Spotlight should show only the items and app's that start with a "W", selecting your new app from the list and pressing "Enter" key will activate your app.
This seems to work quickly.

I'm just looking into using "Launchpad" for launching app's as another option.

And lastly it might meen making our script an Automater Service, launched from the Services menu, which also seems to have keyboard shortcuts available.

I'll get back to you on this one.
Maybe others reading this post might have some other options.
 
Sorry for the slow reply Joseph.

OK it seems that Apple has removed the ability to start Applications with kleyboard shortcuts, this includes Apple's own app's as well, it certainly was the case in the past that you could do this, but they have now restricted keyboard shortcuts to application menus only.
Which leaves only a few other options.
First you can press the "Command" and "Space" key, to bring up "Spotlight".
By typing in "W" in Spotlight should show only the items and app's that start with a "W", selecting your new app from the list and pressing "Enter" key will activate your app.
This seems to work quickly.

I'm just looking into using "Launchpad" for launching app's as another option.

And lastly it might meen making our script an Automater Service, launched from the Services menu, which also seems to have keyboard shortcuts available.

I'll get back to you on this one.
Maybe others reading this post might have some other options.
Ok thank you for all the time, if you find a way around this i'll be glad to try it.
 
Try the "Spotlight" feature with "Command" and "Space", and then typing "Wi", and then pressing "Enter".
You can get similar results to Spotlight with "Command" and "L" to launch Launchpad, and then type in the first couple of letters of the WiFiOnOff.app.

I'll look into creating an Automator Service out of the AppleScript,, and then assigning a keyboard shortcut to the Service.

It might have to be tommorrow for me.
I need a lay down.
Because I'm getting to old for all this excitement.

At least you've learnt how to create a very simple app.
 
  • Like
Reactions: superscape
OK Joseph, to acheive toggling WiFi via keyboard shortcuts, the only solution I could come up with is to create an Automator Service with the AppleScript.
So you can trash the WiFiOnOff app you created before.

Open up the Automator app in the "Applications" Folder, and select the "Service" item in the drop down box.
And click the "Choose" button.
At the top of the screen click the "Service receives selected" combo box, and select the bottom most item "no input", and leave the any application combo box as it is.
Then to the left of that, you'll see a list of selectable actions, scroll down to the "Run AppleScript" item, and double click it, to add it to the workflow.
Then copy and paste the AppleScript code I posted above, where it says "(* Your script goes here *)", replacing that line of text, but leaving all of the rest of on run handler in tact.
Lastly click the "options" button underneath the script, and select the "ignore this action's input", while leaving the other options as they are.
Goto Automator's "File" menu, and click the "Save" sub menu item, and enter "WiFiOnOff" in the service name text field.
And then you can close the Automator app.

Finaly goto the keyboard shortcuts feature in the System Preferences app, like you did before, and instead of selecting the "App Shortcuts" item in the list, select "Services" item instead.
Scroll down to the bottom of the shortcuts list, and you should see the WiFiOnOff service you just created in Automator, so click the "none" item to the right of the WiFiOnOff service, and enter the keyboard shortcut keys you want to run the Service.
I wasn't able to set up with fn6, so I used "Command + Shift + 6", but you could experiment with other key combinations, or change the combinations buy clicking the keyboard shortcuts at any time.
And finally select the check box to the left of the WiFiOnOff service to acivate it.

Hope this helps you out.

Regards Mark
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.