macOS自動掛載smb雲端硬碟

自從製作一個虛擬橋接器之後,發現掛載NAS的需求越來越重,BUT,每過一段時間,掛載的空間就會退載,導致發生一些錯誤與不便。

GPT4o雖然好用,但是在比較小眾或是新內容就會不好用,此文章就是一個例子。

利用macOS內建程式「Automator」,建立應用程式,名稱為「AutoMountNAS」並且放在icloud的Automator應用程式資料夾中。

在程式庫>工具程式,新增「執行Apple Script」。

我的NAS IP為「192.168.111.250」、掛載目錄「JN_NAS_Pro」。此處請依照自家環境調整。

如果已經用Finder掛載並記住帳密,此處就不需要輸入帳號密碼。

on run {input, parameters}
    try
        set volumeName to "JN_NAS_Pro"
        set nasAddress to "smb://192.168.111.250/JN_NAS_Pro"
        tell application "Finder"
            if not (exists disk volumeName) then
                -- 嘗試掛載NAS
                mount volume nasAddress
            end if
        end tell
    end try
    return input
end run

建立服務

建立服務文件

nano ~/Library/LaunchAgents/com.user.mountnas.plist

文件內容,每五分鐘執行一次,路徑可以用右鍵「顯示套件內容」,順著路徑找到執行檔。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.user.mountnas</string>
    <key>ProgramArguments</key>
    <array>
        <string>/Users/jn/Library/Mobile Documents/com~apple~Automator/Documents/AutoMountNAS.app/Contents/MacOS/Automator Application Stub<</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>StartInterval</key>
    <integer>300</integer>
</dict>
</plist>

將文件載入到登入項目中

launchctl load ~/Library/LaunchAgents/com.user.mountnas.plist

經過實測,會定期執行,可以讓NAS經常掛載,即便手動卸載,時間一到還是會再次掛載上次。