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

Oneechan69

macrumors 6502
Original poster
Mar 29, 2022
286
34
US
I use my iPad on walks and keep it in my handbag when I'm outside and not using it. I recently got Jump Desktop to remotely connect to the MacBook. What I'd like to do is when I go outside, I could connect my MacBook to my phone's mobile hotspot without having to take it out of my backpack and open it, then connect via Jump Desktop while the MacBook and iPad are connected to the hotspot.
While normally the MacBook screen turns off while the lid is closed, I can keep it awake with Amphetamine and it makes Jump Desktop work even when its closed. I would connect to my mobile hotspot before leaving but unfortunately I live on a very high floor and there's no signal in the elevator, if I had the mobile hotspot on and connected, it would disconnect in the elevator.
 
So I found a solution: this requires having the MacBook awake, which can be done with an app like Amphetamine if the lid is closed. Create a shortcut for my iPhone or iPad to AirDrop a link to the MacBook, which runs a shortcut with a terminal command to connect to the mobile hotspot. It does take a lot of steps to set it up, but here it is:

Command to connect to a specific wifi

You can try this command in the Terminal: networksetup -setairportnetwork en0 'Users’s iPhone', replacing the phrase in quotes with your wifi name.

For iPhone hotspots, Notice that the apostrophe is different from the apostrophe on your keyboard / MacBook keyboards '. I spent like 15 mins wondering why the network name didn't work until I found the command to list my preferred networks (explain the bullet points).

If this command doesn't work then follow all of the steps below.


  • You need the network name. Use the command networksetup -listallhardwareports to list all of the network devices and their names on your mac. Wifi should say Hardware Port: Wi-Fi. For me the device name is en0 which I will use for the next commands. Replace it with your device name is different.
  • Get the name of the wifi you want to connect (if not then skip this). This command will show a list the names of all the wifis in your preferecnes:
    • networksetup -listpreferredwirelessnetworks en0
    • Copy and paste the name somewhere to keep track of it.
  • Now replace the device and wifi names with the ones you have in the following command: networksetup -setairportnetwork en0 'Users’s iPhone'.
The rest of the steps

  • Create an AppleScript with the Script Editor app and paste this in, replacing the command in ' quotation marks with your command:


Code:
tell application "Terminal"
    do script "networksetup -setairportnetwork en0 'Users’s iPhone'"
end tell
  • I think it's better to run the command in a new Terminal tab or window rather than in background so that you could see if afterwards, especially if it doesn't work for whatever reason.
  • Save the AppleScript in a place to keep it long term and give it whatever name you want.
  • In Finder, go to the AppleScript you created, hold down the option key, and right click on it.
  • Select copy ... as pathname and copy it somewhere to keep track.
  • Create a new command, replacing the path inside the quotation marks with the one to your file
  • osascript "/path/to/script.scpt"
  • Open Shortcuts and create a new shortcut, then click on the icon circled in red in the image below, and check of the boxes checked off in the image (this is required for the shortcut to work).
  • Add "Run Shell Script" and make it look like the image below.
  • Name and customize the shortcut how you wish. This is the shortcut that will be run on the Mac.
  • Create a new shortcut, and make it look like the image below
    • You can get the URL block by just searching "URL"
    • Sample URL: shortcuts://run-shortcut?name=Change%20Wifi. Replace the shortcut name with the other one you created, and replace spaces with %20.
  • This is the shortcut you will run on your iPhone or iPad.
  • Now try it out and make sure it works. Again, in order for all of this to work, the mobile hotspot must be on, the laptop must be awake (use an app like Amphetamine) and both devices must have wifi and bluetooth on, as well be close enough for AirDrop (basically AirDrop has to work).
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.