Script Editor cannot "see" iPhone in private local wifi-network - But Terminal finds it without issue

I’m using an old Mac (Big Sur) and Script Editor v2.11.
I am not turning my script into an app, I am just letting it run in the background in script editor.

While running my main script on my Mac, I want to scan for the IP of my iPhone in the WIFI network. Both the Mac and the iPhone are in the same wifi-network.
If the iPhone is there, I want to do X, if it is not logged into the wifi, I do Y.

I noticed that I - in general, so even in a new fresh empty script - get different results scanning for the iPhone depending on if I execute from script editor or from a normal terminal window. Running ping in terminal will find the local iPhone IP, but doing it from inside script editor via do shell script will not.

To troubleshoot, I downloaded nmap.
nmap -sn “local IP of Mac”/24 executed from terminal can “see” my iPhone without issue (and the other connected devices).

Running any of the following 4 options in Script Editor, will not find the iPhone (but the other connected devices it will find - for the last 3 options).

I don’t understand why a normal terminal window will find it, but when I spawn a terminal window from Script Editor, the result changes.

do shell script ("ping -t 15 -q [local IP of iPhone]")

do shell script ("usr/local/bin/nmap -sn [local IP of Mac]/24")

do shell script "eval $(usr/libexec/path_helper -s);" & "nmap -sn [local IP of Mac]/24"

tell application "Terminal"
do script ("nmap -sn [local IP of Mac]/24")
end tell

I did check the mac-firewall and did not see Script Editor listed in there. To troubleshoot, I added Script Editor and set it to allow incoming connections, but it made no difference.

Thank you for reading!

I wonder if this is related to the macOS permission for an app to “see others devices on your network.” Maybe Terminal is grandfathered in, but Script Editor is not?
Maybe there is some way to get Script Editor to request that permission?

Almost certainly a permission/security issue.

I downloaded nmap and the first time I ran nmap -sn from Terminal.app, I got no results, plus a pop-up saying that Terminal.app wanted to access resources on my local network.
A subsequent run of the app produced a list of local network devices.

So clearly, by default, terminal (and therefore processes that run within it) are not able to scan the network until permission has been granted.

As for why it doesn’t run in Script Editor - simple… Script Editor is not Terminal.app, not does it even use Terminal.app to run scripts, they’re launched directly via a sub-process.

The exact mechanism for this has changed over the years/MacOS versions, but OMM (15.5) it looks like it runs under a subprocess of the script app (in my case, Script Debugger).

FWIW, the do shell script examples do work on my system, although it’s possible I previously granted Script Debugger access to the local network at some point in history.

Check System Settings → Privacy & Security → Local Network to see if your script Editor is listed there.

Installed nmap, but it’s not for me. Anyone know of a way to uninstall nmap? The website is vague at best.

Sorry Homer712, I haven’t tried uninstalling it yet, so I don’t know.

@Krioni and @Camelot: Thank you for your response. I have new findings.

I don’t have the Local Network menu / permission system, my MacOS is too old (can’t update, too old).

Today I found different behavior than yesterday and I could not make sense of it.

Then I found a new variable: If I activate the iPhone or not. Activate as in touching the screen, waking it up, unlocking it.

If I have just activated the phone, I can successfully ping/nmap it from both script editor and terminal.
So, sorry, my assumption from yesterday that there is a hard difference between script editor and terminal on my system seems to be wrong. But to my defense, I did not realize I was unwittingly modulating the variable “iPhone activated or not” while I was testing stuff.

Once I lock the iPhone (screen goes black), ping/nmap can still find it for a few seconds.
But then, quickly afterwards, it is not discoverable anymore - neither by terminal or script editor.

If I then unlock/activate the phone again, it’s discoverable once again by both terminal or script editor.

Anyone have any tips how I can reliably detect if the iPhone is logged into the wifi network, whether the screen is on or not?

I did also test with nmap -T4 -Pn [local IP of iPhone] … but this one says the host is up even if I log out of the wifi network and stay logged out for a while…

edit: The router web dashboard network map, however, is more robust. It does show - and keeps showing - the iPhone is connected, even if I turn off the screen. It only disappears in this dashboard when I manually deactivate wifi on my iPhone. So at least the router can tell the difference somehow.

No worries on the uninstall, which by the way, is next to impossible. I ended up using System Settings/General/Transfer or Reset and then just using a Carbon Copy Cloner backup to do the Migration. Back up and running as we speak.