Script no longer gets SSID. macOS 14.4 Sonoma

set mySSID to do shell script "/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I | awk '/ SSID/{print substr($0, index($0, $2))}'"


set feedback to display dialog ("You are currently connected to:


WiFi SSID: " & mySSID as string) & ". 


I used to see: "WiFi SSID: <current SSID>. Where <current SSID> is the wifi I am connected to.


Now I see: "Wifi SSID ."


This happened after the update to macOS 14.4 Sonoma.


Can anyone point me to the problem or suggest a correction that will work on macOS 14.4>



MacBook Pro 16″, macOS 14.4

Posted on Mar 11, 2024 1:34 PM

Reply
Question marked as Top-ranking reply

Posted on Mar 11, 2024 2:13 PM

The airport command line tool is now deprecated in Sonoma 14.4 and no longer works other than to inform you that it is deprecated and will be removed in a future release. Instead, use this:


use framework "CoreWLAN"
use scripting additions

property ca : current application

set wifi to (ca's CWWiFiClient's sharedWiFiClient)'s interface()
display dialog "You are connected to: " & wifi's ssid() as text with title "Current SSID"
return


Tested: Sonoma 14.4

31 replies
Question marked as Top-ranking reply

Mar 11, 2024 2:13 PM in response to Diveboss164

The airport command line tool is now deprecated in Sonoma 14.4 and no longer works other than to inform you that it is deprecated and will be removed in a future release. Instead, use this:


use framework "CoreWLAN"
use scripting additions

property ca : current application

set wifi to (ca's CWWiFiClient's sharedWiFiClient)'s interface()
display dialog "You are connected to: " & wifi's ssid() as text with title "Current SSID"
return


Tested: Sonoma 14.4

May 4, 2024 6:31 AM in response to Diveboss164

I see two things I would change:


Separate your handlers from the script.

--end routine
return


and replace:

	set mySSID to (ca's CWWiFiClient's sharedWiFiClient)'s interface()'s ssid()
	set feedback to display dialog "You are currently connected to " & mySSID & " LAN.


With:

	set wif to ca's CWInterface's interface
	set mySSID to (do shell script "networksetup -getairportnetwork " & (wif's |name|) & " | awk '{print $4}'")
    set feedback to display dialog "You are currently connected to " & mySSID & " LAN.



This definitely works to get the SSID name string in a compiled Automator application.


Tested: Sonoma 14.4.1.

Mar 13, 2024 8:09 AM in response to Diveboss164

And, how to use Apple's osascript in a Zsh script:


#!/bin/zsh

function get_SSID () {
    /usr/bin/osascript <<-AS
    use framework "CoreWLan"
    use scripting additions

    property ca : current application

    return ((ca's CWWiFiClient's sharedWiFiClient)'s interface()'s ssid()) as text
AS
}

mySSID=$(get_SSID)
print ${mySSID}
exit 0


chmod +x ./getWiFi.zsh

./getWiFi.zsh

Bifrost6


Tested: Sonoma 14.4, Zsh 5.9

Mar 11, 2024 5:22 PM in response to Diveboss164

Let's look closer at that Get_User_Feedback handler.


Here is how I would write it:


use framework "CoreWLAN"
use scripting additions

property ca : current application
# property dialog_icon : "Macintosh SSD:Users:me:Automator Custom Icons:icon.icns"
property app_icon : ((path to pictures folder) & "brain512x512_300.icns") as text

set Refresh_time to 5 as integer
set wait_time to 60 as integer
set Decision to 0 as integer

repeat until (Decision > 0) or (wait_time = 0)
	set Decision to my Get_User_Feedback(wait_time, Refresh_time)
	set wait_time to wait_time - Refresh_time
end repeat
log (Decision) as integer

# the rest of your Decision if tests after this comment
return

on Get_User_Feedback(Remain_time, Step_delay)
	
	set mySSID to (ca's CWWiFiClient's sharedWiFiClient)'s interface()'s ssid()
	
	try
		# AppleScript needs the option+L line continuation ( ¬ ) character as text does not flow from one line to the next without it.
		set {gave up:gaveUP, button returned:buttonReturned} to display dialog ("You are currently connected to WiFi SSID: " & mySSID as text) & return & return & ¬
			"You will be connected to the xyzzy Server in " & Remain_time & ¬
			" seconds. If your are not on xyz Local Area Network, please select 'Don't Connect'." buttons {"Don't connect", "Connect Now"} default button ¬
			"Connect Now" with icon app_icon as alias giving up after Remain_time
		if gaveUP then
			return 0
		else if buttonReturned is "Connect Now" then
			return 1
		else
			return 2
		end if

end try

end Get_User_Feedback


With no user input and two gave up events:



Apr 25, 2024 6:28 AM in response to SRS1985

With the exception of the FileMaker Pro tell block, the preceding AppleScript works fine here on Sonoma 14.4.1 in Script Editor. You can use either cast: as string, or as text, and it will run without errors in the Script Editor.


If you cannot get that AppleScript to work, then replace it with this:


use scripting additions

set ssid to (do shell script "wdutil info | awk '/SSID/ { print $3;exit}'" with administrator privileges) as text

and then feed that ssid result to your FileMaker Pro block.



May 6, 2024 12:05 PM in response to MrHoffman

If y'all can run Swift, here's an Xcode Swift command-line tool that displays all of the SSIDs currently connected:


//
//  GetSSID
//
//  Mostly from https://developer.apple.com/forums/thread/50302
//

import Foundation
import CoreWLAN

print("SSIDs:", currentSSIDs())

func currentSSIDs() -> [String] {
    let client = CWWiFiClient.shared()
    return client.interfaces()?.compactMap { interface in
        return interface.ssid()
    } ?? []
}


Slightly less code using this path than using AppleScript, and it'll run on most recent versions of macOS.


Tested with the following:

$ swiftc -v
Apple Swift version 5.9.2 (swiftlang-5.9.2.2.56 clang-1500.1.0.2.5)
Target: x86_64-apple-macosx13.0
$ /usr/bin/xcodebuild -version
Xcode 15.2
Build version 15C500b
$ 



The following is a restructured script that can be used without compiling the Swift code, with a shebang-based workaround for a bug (#68785 / #71004 / #71119) in the command line Swift tooling circa Xcode 15.2:


#!/usr/bin/env DYLD_FRAMEWORK_PATH=/System/Library/Frameworks /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift
// 
//  GetSSID
//
//  Mostly https://developer.apple.com/forums/thread/50302
//

import Foundation
import CoreWLAN
 func currentSSIDs() -> [String] {
    let client = CWWiFiClient.shared()
    return client.interfaces()?.compactMap { interface in
        return interface.ssid()
     } ?? []
 }
 print(currentSSIDs())

Mar 12, 2024 8:12 PM in response to Diveboss164

Hi MrHoffman,

Thanks for the link. I created a shell script with Automator using this option,


!/bin/sh

##Get the wireless port ID

WirelessPort=$(networksetup -listallhardwareports | awk '/Wi-Fi|AirPort/{getline; print $NF}')

##What SSID is the machine connected to

SSID=$(networksetup -getairportnetwork "$WirelessPort" | cut -d " " -f4)

echo "$SSID"


and it does return the SSID.


Would you mind explaining to me how I can link this to my AppleScript and pass the variable $SSID to my dialog box? Please keep in mind this is new to me and I'm learning. I still haven't how I can change/save the shell script in anything except the default 'workflow' file.

Thanks

Mar 11, 2024 9:14 PM in response to VikingOSX

This application runs on startup and gives me 60 seconds to confirm I’m connected to the correct wifi before connecting to my server. If I fail to make a decision in 60 seconds the app should attempt to connect to my server by default.


I’m getting closer, but several things still not working.


I am getting the SSID displayed in the dialog box and it’s configured properly. 


However, here is a list of issues I cannot seem to resolve. It’s after midnight and I’m calling it a day. 


  1. Refresh time is taking 60 seconds to go from 60 to 55 seconds remaining. It appears to be shorter intervals as it approaches 5 second remaining but it’s not clocking properly.
  2. Selecting the 'Connect Now' button does not run script. 
  3. Failing to make a selection does not run script.
  4. Cancel works


MacBook Pro M3 Max

Most of this script with the exception of your input comes from my older application. My older original is working with the exception of getting and displaying the SSID of the connected wifi.


Thanks for all your help.


Here is what I have so far.


use framework "CoreWLAN"

use scripting additions


property ca : current application

# property dialog_icon : "Macintosh SSD:Users:user:Automator Custom Icons:Synology 5 bay DiskStation DS1512+.icns"

property app_icon : "Macintosh SSD:Users:user:Automator Custom Icons:Synology 5 bay DiskStation DS1512+.icns" as text


set Refresh_time to 5 as integer

set wait_time to 60 as integer

set Decision to 0 as integer


repeat until (Decision > 0) or (wait_time = 0)

set Decision to my Get_User_Feedback(wait_time, Refresh_time)

set wait_time to wait_time - Refresh_time

end repeat

log (Decision) as integer


# the rest of your Decision if tests after this comment

return


# display alert "Decision is " & Decision

# 0 means no answer - Should connect without input after 60 seconds

# 1 means Connect Now

# 2 means Don't Connect


if Decision is 2 then

quit


else if Decision is 1 then

tell application "Finder"

try

mount volume "smb://VS/home/Drive"

delay 1

end try

end tell

tell application "Finder"

try

mount volume "smb://VS/Movies"

delay 1

end try

end tell

tell application "Finder"

try

mount volume "smb://VS/music"

delay 1

end try

end tell

tell application "Finder"

try

mount volume "smb://VS/photo"

delay 1

end try

end tell

tell application "Finder"

try

mount volume "smb://VS/TV Shows"

delay 1

end try

end tell

tell application "Finder"

try

mount volume "smb://VS/video"

delay 1

end try

end tell

end if


on Get_User_Feedback(Remain_time, Step_delay)

set mySSID to (ca's CWWiFiClient's sharedWiFiClient)'s interface()'s ssid()

try

# AppleScript needs the option+L line continuation ( ¬ ) character as text does not flow from one line to the next without it.

set {gave up:gaveUP, button returned:buttonReturned} to display dialog ("You are currently connected to WiFi SSID: " & mySSID as text) & return & return & ¬

"You will be connected to the VS Server in " & Remain_time & ¬

" seconds. If your are not on VS Local Area Network, please select 'Don't Connect'." buttons {"Don't connect", "Connect Now"} default button ¬

"Connect Now" with icon app_icon as alias giving up after Remain_time

if gaveUP then

return 0

else if buttonReturned is "Connect Now" then

return 1

else

return 2

end if

end try

end Get_User_Feedback



Mar 11, 2024 2:47 PM in response to VikingOSX

Thanks for the reply. I'm real rusty with scripting.

Can you show me where to change the script? What to remove and where to add the changes.

This is compiled to run as an application.


The BoldFace section is where I'm sure I need to change, but just removing that section and adding the scripting you provided "as is" didn't work. Yes, I'm an idiot sometimes. I've been away from this for too long.


All your help is appreciated.


Here is my original script.


set Refresh_time to 5 -- the delay between refresh of the dialog

set wait_time to 60 --total delay for user to decide

set Decision to 0

repeat until (Decision > 0) or (wait_time = 0)

set Decision to Get_User_Feedback(wait_time, Refresh_time)

set wait_time to wait_time - Refresh_time

end repeat


-- display alert "Decision is " & Decision

-- 0 means no answer

-- 1 means Connect

-- 2 means don't connect


if Decision is 2 then

quit

end if


-- your program here


tell application "Finder"

try

mount volume "smb://xyz/home/folder1"

delay 1

end try

end tell


tell application "Finder"

try

mount volume "smb://xyz/folder2"

delay 1

end try

end tell


tell application "Finder"

try

mount volume "smb://xyz/folder3"

delay 1

end try

end tell


tell application "Finder"

try

mount volume "smb://xyz/folder4"

delay 1

end try

end tell


tell application "Finder"

try

mount volume "smb://xyz/folder5"

delay 1

end try

end tell


tell application "Finder"

try

mount volume "smb://xyz/folder6"

delay 1

end try

end tell


-- end routine


on Get_User_Feedback(Remain_time, Step_delay)


set mySSID to do shell script "/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I | awk '/ SSID/ {print substr($0, index($0, $2))}'"


set feedback to display dialog "You are currently connected to:

WiFi SSID: " & mySSID & ". 


You will be connected to the xyzzy Server in " & Remain_time & " seconds. If your are not on xyz Local Area Network; please select 'Don't Connect'." buttons {"Don't connect", "Connect Now"} default button "Connect Now" giving up after Step_delay with icon file (("MacIntosh SSD:Users:me:Automator Custom Icons:") & " icon.icns")


if gave up of feedback then

return 0

else if button returned of feedback is "Connect Now" then

return 1

else

return 2

end if

end Get_User_Feedback

Mar 12, 2024 2:16 AM in response to Diveboss164

Having slept on this, I would do the following:


  1. I would not use a repeat while block at all.
    1. Instead of 12 iterations of decrementing 5 seconds — do one dialog with a timeout value of however many seconds you will wait for the user response. In the message, briefly state what will happen if they fail to take action (the gaveUP condition).
  2. Obtain the SSID outside of the handler and pass that string to the handler with the time delay before the dialog gives up — as two arguments to the handler:
    1. on Get_User_Feedback(mySSID, timeout_secs)


And remove the following from your code:


# the rest of your Decision if tests after this comment
return


But, you do want a return statement just prior to your handler to separate your main script code from it.


return

on Get_User_Feedback(Remain_time, Step_delay)


Mar 12, 2024 12:14 PM in response to Diveboss164

Ok I moved the return from here


# the rest of your Decision if tests after this comment

return


to here


return



on Get_User_Feedback(Remain_time, Step_delay)


It helps when you show me where in the code to make changes.


After doing this, it now processes the button pushes and runs automatically on timeout. That's a start, but the countdown timer is still not working.

I still want a countdown timer either continuous or incremental, which ever is easiest.


I currently left this part of the code in, but want to get a fix to it. Like I said, it's been a long time since I have done any coding.



set Refresh_time to 5 as integer

set wait_time to 60 as integer

set Decision to 0 as integer


repeat until (Decision > 0) or (wait_time = 0)

set Decision to my Get_User_Feedback(wait_time, Refresh_time)

set wait_time to wait_time - Refresh_time

end repeat


Again thanks.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Script no longer gets SSID. macOS 14.4 Sonoma

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.