Apple Script does not work on Sierra ... mapping network drives

Hi all

the old IT guy for our mac labs used applescript for the computers to auto log on / connect to the network shared drives, the depending on who was log gin in. but it does not work in Sierra. I have no idea why… help please

Save the AppleScript as an Application

To allow the app to run at every login & as the user logging in, create a LaunchAgent. Use a plist editor to create a plist with the info below and save it as ca.ab.gprc.APPLICATIONNAME.plist in /Library/LaunchAgents/ with 755 permissions (chmod 755 ca.ab.gprc.APPLCATIONNAME.plist). Replace APPLICATIONNAME with the AppleScript Application Name.

######################################################################

<?xml version="1.0" encoding="UTF-8"?>

Label

domain.my.MapDrives

Program

/Applications/APPLICATIONNAME.app/Contents/MacOS/applet

RunAtLoad

#######################################################################

Download ./jq and copy it to /usr/sbin (must use sudo).

I use PackageMaker (part of Xcode) to install files in the correct locations.

curl “https://my.domain.ca/AppService/api/NetworkMappings?UserName=MDiPasquale&ComputerName=TAG028106” | jq ‘.[] | .NetworkPath’ | cut -c 2- | sed ‘s:\\\\://:g’ | sed ‘s:\\:/:g’ | sed ‘s:.$::’

The above code grabs the network path of each record of the object and replaces the slashes; “\\” with “//” and “\” with “/”. Cut removes the quotes at the beginning and the last sed command removes the quotes at the end. This works for command line but not applescript.

See the main body of code for the proper AppleScript command/format to achieve this.

Get Username and Tag Number. Define the URL for the json request.

set user to do shell script “whoami”
set tag to do shell script “hostname -s”
set link to “https://my.domain.ca/AppService/api/NetworkMappings?UserName=” & user & “&ComputerName=” & tag

Get the number of disks currently mounted.

set diskLength to length of (list disks)

Get number of records in the json object.

Define the first record number in the object.

set recordLength to do shell script “curl " & quoted form of link & " | jq ‘length’”
set recordNumber to 0

For each json record, determine if the object is a drive or printer and mount/install accordingly.

repeat recordLength times

# Define a variable pointing to the first record returned by list disks.
# set diskItem to 1

try
	set networkPathVar to do shell script "curl " & quoted form of link & " | jq '.[" & recordNumber & "] | .NetworkPath' | cut -c 2- | sed 's:\\\\\\\\\\\\\\\\://:g' | sed 's:\\\\\\\\:/:g'  | sed 's:.$::'"
	set deviceType to do shell script "curl " & quoted form of link & " | jq '.[" & recordNumber & "] | .Type'"
on error
	return
end try


if deviceType contains "Drive" then
	
	set recordNumber to recordNumber + 1
	
	# Check to see if the drive is already mounted.  If not mount it.
	
	# repeat diskLength times
	if networkPathVar does not contain items 1 thru diskLength of (list disks) then
		
		mount volume "smb:" & networkPathVar
		
	end if
	# end repeat
	
else
	
	set recordNumber to recordNumber + 1
end if # End of Device Type IF

end repeat

Hi. Welcome to MacSripter.

It’s difficult to check the code you’ve posted because it depends on things having been installed in various parts of the system, connects to a particular Internet address, and apparently logs hard drives and printers onto the server at the other end ” which people here will obviously be reluctant to have happen with their own machines. Also some of the ‘sed’ code has been converted to smileys by this site’s posting software. (If you post with the code between [applescript] and [/applescript] tags, this won’t happen.)

But if the script was working before you updated to Sierra, the first thing to check is obviously that it and the files on which running it depends are (still) installed in the required locations with the necessary permissions, as detailed in the comments.

Whoever wrote the script has chosen to use "# " at the beginning of every comment line. The plist text should actually look like this in the plist file .

. replacing “APPLICATIONNAME.app” with the name of the script applet.

everything seems to be installed. as this is the error i get .

I can’t see the error because instead of posting it, you’ve posted a link to an attachment on another forum where you’ve cross-posted your query, the attachment only being accessible to members of that forum.

Assuming everything’s installed and that your user name and computer name haven’t changed in the update to Sierra (ie. that the server still recognises your computer), the only issues I can definitely identify in the code concern the use of list disks:

  1. It was deprecated several system versions ago, although I believe it still works in Sierra. Nowadays you’d use something like:
-- set diskLength to length of (list disks) -- Deprecated.
tell application "System Events" to set diskLength to (count disks)
  1. It returns an AppleScript ‘list’, each item of which is the name of a mounted volume. According to the comments in the script, the variable networkPathVar is set to a network path each time round the repeat. So the line .
if networkPathVar does not contain items 1 thru diskLength of (list disks) then

. is rubbish. A text can’t contain a list ” let alone a path a list of volume names ” unless, that is, the list only contains one item and that item is a substring of the text. So if more than one volume’s already mounted when the script’s run, diskLength will be greater than 1, networkPathVal will never contain items 1 thru diskLength of (list disks), and the script will try to mount every network path it gets. If there’s only one volume beforehand, its likely to be local to the machine anyway, but no further volumes with the same name will be mounted.

The version below is patched to address the situation, but I don’t know if it fixes the problem you were having. It still won’t mount any volumes if there are none mounted before it runs.

# Save the AppleScript as an Application
# To allow the app to run at every login & as the user logging in, create a LaunchAgent.  Use a plist editor to create a plist with the info below and save it as ca.ab.gprc.APPLICATIONNAME.plist in /Library/LaunchAgents/ with 755 permissions (chmod 755 ca.ab.gprc.APPLCATIONNAME.plist).  Replace APPLICATIONNAME with the AppleScript Application Name.

######################################################################
# <?xml version="1.0" encoding="UTF-8"?>
# <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
# <plist version="1.0">
# <dict>
#     <key>Label</key>
#     <string>domain.my.MapDrives</string>
#     <key>Program</key>
#     <string>/Applications/APPLICATIONNAME.app/Contents/MacOS/applet</string>
#     <key>RunAtLoad</key>
#     <true/>
# </dict>
# </plist>
#######################################################################

# Download ./jq  and copy it to /usr/sbin (must use sudo).
# I use PackageMaker (part of Xcode) to install files in the correct locations.


# 	curl "https://my.domain.ca/AppService/api/NetworkMappings?UserName=MDiPasquale&ComputerName=TAG028106" | jq '.[] | .NetworkPath' | cut -c 2- | sed 's:\\\\\\\\:\/\/:g' | sed 's:\\\\:\/:g' | sed 's:.$::'
# 	The above code grabs the network path of each record of the object and replaces the slashes; "\\\\" with "//" and "\\" with "/".  Cut removes the quotes at the beginning and the last sed command removes the quotes at the end.  This works for command line but not applescript.
#	See the main body of code for the proper AppleScript command/format to achieve this.


# Get Username and Tag Number.  Define the URL for the json request.

set user to do shell script "whoami"
set tag to do shell script "hostname -s"
set link to "https://my.domain.ca/AppService/api/NetworkMappings?UserName=" & user & "&ComputerName=" & tag

# Get the names of the currently mounted disks.

tell application "System Events" to set diskNames to name of disks

# Get number of records in the json object.
# Define the first record number in the object.

set recordLength to do shell script "curl " & quoted form of link & " | jq 'length'"
set recordNumber to 0

# For each json record, determine if the object is a drive or printer and mount/install accordingly.

repeat recordLength times
	
	# Define a variable pointing to the first record returned by list disks.
	# set diskItem to 1
	
	try
		set networkPathVar to do shell script "curl " & quoted form of link & " | jq '.[" & recordNumber & "] | .NetworkPath' | cut -c 2- | sed 's:\\\\\\\\\\\\\\\\://:g' | sed 's:\\\\\\\\:/:g'  | sed 's:.$::'"
		set deviceType to do shell script "curl " & quoted form of link & " | jq '.[" & recordNumber & "] | .Type'"
	on error
		return
	end try
	
	
	if deviceType contains "Drive" then
		
		set recordNumber to recordNumber + 1
		
		# Check to see if the drive is already mounted.  If not mount it.
		
		repeat with thisName in diskNames
			if networkPathVar does not end with ("/" & thisName) then
				
				mount volume "smb:" & networkPathVar
				
			end if
		end repeat
		
	else
		
		set recordNumber to recordNumber + 1
	end if # End of Device Type IF
end repeat

it has worked for the last 4 years.

no matter who sits down and logs into the MAC , their proper home drive would appear.

anyone of 2000 people could log in and get their proper home drive

It may not be possible to help you. You haven’t described what “doesn’t work” means. (Doesn’t run? Runs but doesn’t do anything? Runs but errors?) You haven’t bothered to edit your first post in the way I suggested so that we can see the actual script characters instead of the smileys. You haven’t bothered to edit your second post so that we can see the error message mentioned there. Your only reaction to the fixing of the visible faults in the script has been three lines emphasising that it was working before. There’s no way for us to check if the script and the files on which running it depends have been installed as per the comments.

One thing to try would be to run the script in Script Editor on a Sierra machine. If the shared drive successfully mounted, that would eliminate the network connection, the jq installation, the script code, and any Sierra incompatibilities as possible causes of the problem and leave just the set-up for running the script automatically to be investigated.

And of course if your old IT guy’s still alive and willing to help, you might try consulting him.


# Get Username and Tag Number.  Define the URL for the json request.

set user to do shell script "whoami"
set tag to do shell script "hostname -s"
set link to "https://my.gprc.ab.ca/AppService/api/NetworkMappings?UserName=" & user & "&ComputerName=" & tag
# Get the number of disks currently mounted.

set diskLength to length of (list disks)

# Get number of records in the json object.
# Define the first record number in the object.

set recordLength to do shell script "curl " & quoted form of link & " | jq 'length'"
set recordNumber to 0

# For each json record, determine if the object is a drive or printer and mount/install accordingly.

repeat recordLength times
	
	# Define a variable pointing to the first record returned by list disks.
	# set diskItem to 1
	
	try
		set networkPathVar to do shell script "curl " & quoted form of link & " | jq '.[" & recordNumber & "] | .NetworkPath' | cut -c 2- | sed 's:\\\\\\\\\\\\\\\\://:g' | sed 's:\\\\\\\\:/:g'  | sed 's:.$::'"
		set deviceType to do shell script "curl " & quoted form of link & " | jq '.[" & recordNumber & "] | .Type'"
	on error
		return
	end try
	
	
	if deviceType contains "Drive" then
		
		set recordNumber to recordNumber + 1
		
		# Check to see if the drive is already mounted.  If not mount it.
		
		# repeat diskLength times
		if networkPathVar does not contain items 1 thru diskLength of (list disks) then
			
			mount volume "smb:" & networkPathVar
			
		end if
		# end repeat
		
	else
		
		set recordNumber to recordNumber + 1
	end if # End of Device Type IF
end repeat


the errors I get are below

Blockquote sh: jq: command not found % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed

0 0 0 0 0 0 0 --:–:-- --:–:-- --:–:-- 0 100 36 100 36 0 0 495 0 --:–:-- --:–:-- --:–:-- 500 (23) Failed writing body

sh: jq: command not found % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed

0 0 0 0 0 0 0 0 --:–:-- --:–:-- --:–:-- 0 100 36 100 36 0 0 495 0 --:–:-- --:–:-- --:–:-- 500 (23) Failed writing body (127)

It is clear that bash cannot find the 3rd party jq command. It isn’t installed, you need an absolute path or the directory containing the jq executable is not added to the global $PATH bash variable.

When curl tries to write data to an closed pipe (closed by the secondary process), it will throw the 23 error. The error returned by curl is only an understatement.

When you say something like this my guess will be that you have forgotten to install jq on the new/updated machine? Don’t forget that an major update of Mac OS X can remove 3rd party command line utilities you had installed in the previous version.