Skip to content

Instantly share code, notes, and snippets.

@ejangi
Created September 29, 2014 04:51
Show Gist options
  • Save ejangi/abfa2f8529fe453d9b52 to your computer and use it in GitHub Desktop.
Save ejangi/abfa2f8529fe453d9b52 to your computer and use it in GitHub Desktop.
AppleScript to silently Mount a Network Share and tell spotlight to index it so it is searchable
# The MIT License (MIT)
#
# Copyright (c) 2014 SU QLD
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# This script was originally written by James Angus - 29 September 2014
set protocol to "cifs"
set server to "example.com"
set share to "MyShare"
set packet_loss to (do shell script "ping -t1 -c1 " & server & " | grep transmitted | cut -d, -f3 | cut -b2-")
tell application "Finder"
set disklist to list disks
if packet_loss is equal to "0.0% packet loss" and disklist does not contain share then
set cmd to protocol & "://" & server & "/" & share
log "Attempting to connecting to: " & cmd
try
# Mount the volume using the Keychain authentication system:
mount volume cmd
on error errmsg number errnum
log "[" & errnum & "] " & errmsg
return errnum
end try
# Wait a sec for the mount to complete fully:
delay 5
# (re-)enable indexing of this network volume so that it is Spotlight searchable:
do shell script ("mdutil /Volumes/" & share & " -i on")
end if
end tell
@frankeman90
Copy link

So if I understood this right: The script mounts a network share (smb://xx.x.x.x) and starts indexing the volume?

Have tried but get this error:
Resultat:
"/Volumes/FAKTA:
Error: unable to perform operation. Try as root. (-403)
Server search enabled."

Have set it up with theese parameters:
set protocol to "cifs"
set server to "10.0.1.2"
set share to "FAKTA"
set packet_loss to (do shell script "ping -t1 -c1 " & server & " | grep transmitted | cut -d, -f3 | cut -b2-")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment