Macos Apple脚本Else/If

Macos Apple脚本Else/If,macos,applescript,drive-mapping,Macos,Applescript,Drive Mapping,我目前正在尝试在连接到网络时自动安装驱动器。唯一的问题是,当我没有连接到网络时,会显示错误,而连接到网络时,不会显示错误。如何禁用此功能 以下是我当前的脚本: tell application "Finder" mount volume "smb://(IP)/(Drive)" as user name "(user)" end tell 这是一个检查网络连接的简单处理程序。它可用于以太网和WiFi连接 如有必要,调整前两行中的值 property primaryEthernetDev

我目前正在尝试在连接到网络时自动安装驱动器。唯一的问题是,当我没有连接到网络时,会显示错误,而连接到网络时,不会显示错误。如何禁用此功能

以下是我当前的脚本:

tell application "Finder"
    mount volume "smb://(IP)/(Drive)" as user name "(user)"
end tell

这是一个检查网络连接的简单处理程序。它可用于以太网和WiFi连接

如有必要,调整前两行中的值

property primaryEthernetDevice : "en0"
property primaryWiFiDevice : "en2"

property myIPAddress : "192.168.1.10"
property mySSID : "mySSID"

set {isConnected, IPAddress, ssid} to checkConnection()
if isConnected and ssid is mySSID then
    mount volume "smb://(IP)/(Drive)" as user name "(user)"
end if

on checkConnection()
    set wiredIP to do shell script "/sbin/ifconfig " & primaryEthernetDevice & " | /usr/bin/awk '/inet / {print $2}'"
    if wiredIP is not "" then return {true, wiredIP, ""}
    set wirelessIP to do shell script "/sbin/ifconfig " & primaryWiFiDevice & " | /usr/bin/awk '/inet / {print $2}'"
    if wirelessIP is not "" then
        set ssid to do shell script "/usr/sbin/networksetup -getairportnetwork " & primaryWiFiDevice & " | sed 's/Current Wi-Fi Network: //g'"
        return {true, wirelessIP, ssid}
    else
        return {false, "", ""}
    end if
end checkConnection

在尝试挂载之前检查网络是否已连接?可能还应该检查连接的网络是否正确。
装载卷
标准添加的一部分。不需要Finder。我将如何执行它,以便它在执行之前检查它是否连接到正确的网络。这对我未连接时很有用。但是当我连接到另一个网络时,它返回一个错误代码-5014,并告诉我无法安装驱动器。你必须找到一种方法来区分网络。如果它是无线的,你可以通过SSID识别它。或者,如果IP地址不同,请使用该地址。第三种解决方案是将
mount volume
行包装在
try-end try
块中,以抑制错误。我不太擅长使用AppleScript,因为这是我第一次实际使用它。最重要的是,它不是我的Mac,所以我正在尝试进行此设置,以便卷仅在连接到某个网络(我当前正在连接)时装载。使用您描述的内容,我将如何在您给我的脚本中实现它?我更新了答案,以检查IP(v4)地址。谢谢,现在我不知道我正在尝试做的是否可行。这仅仅是因为IPv4将发生变化,因为它是一台从一个位置带到另一个位置的笔记本电脑。有没有办法检查路由器的MAC地址?