I’m working on a script that involves mounting an SMB volume for file transfers.
If I run this portion of the script when the volume is not mounted, it mounts successfully. If it is already mounted, I get error “File smb://xxxxxx/xxx/xxxx/xxx wasn’t found.” (obv. x’s are replacing actual path)
I’m sure I’ve got a syntax wrong, but not sure what is right.
set theVolume to "smb://" & serverName & "." & domainName & pathName1 & pathName2
set home_path to path to home folder as string
set work_folder to alias (home_path & "Desktop:" & "upVideo")
set archiveFolder to alias (home_path & "Desktop:" & "upVideo:" & "archive")
set counter to ""
global counter
set volCheck to "/Volumes" & pathName1
set mountedVolumes to every paragraph of (do shell script "mount | awk -F' on ' '{print $2}' | awk -F' \\\\(' '{print $1}'")
if volCheck is not in mountedVolumes then
-- mount the shared drive
set this_folder to (mount volume theVolume) as alias
else
set this_folder to theVolume as alias
end if
A bit of guesswork: mount volume returns a HFS path, which can be coerced to an A/S alias.
But theVolume contains a file URL, and that cannot be coerced to an A/S alias.
try this, the code can be slightly simplified.
Important note: the variable volumeName (former pathName1) is assumed to have no leading slash
set theVolumeURL to "smb://" & serverName & "." & domainName & "/" & volumeName & pathName2
set desktopFolder to path to desktop folder as text
set work_folder to alias (desktopFolder & "upVideo:")
set archiveFolder to alias (desktopFolder & "upVideo:archive:")
if volumeName is not in (paragraphs of (do shell script "/bin/ls /Volumes")) then
-- mount the shared drive
set this_folder to (mount volume theVolumeURL) as alias
else
set this_folder to volumeName as alias
end if
Thanks very much for the feedback.
I’m now getting the following error:
Below is the full code (test code, based on your input), with server names edited out for security.
set serverName to "xxxxxxxxxx"
set domainName to "xxxnet.yyy.zzz.gov"
set volumeName to "ves"
set pathName2 to "videos"
set theVolumeURL to "smb://" & serverName & "." & domainName & "/" & volumeName & "/" & pathName2
set desktopFolder to path to desktop folder as text
set work_folder to alias (desktopFolder & "upVideo:")
set archiveFolder to alias (desktopFolder & "upVideo:archive:")
if volumeName is not in (paragraphs of (do shell script "/bin/ls /Volumes")) then
-- mount the shared drive
beep 2
set this_folder to (mount volume theVolumeURL) as alias
else
beep 4
set this_folder to volumeName as alias
end if
I’ve been working on this quite a bit since posting, and I believe I have it pretty well complete. I’d greatly appreciate a review, as I’m sure there are things I could improve. The only issue that I’m running into is that if a file on the local drive is not displaying an extension, it will transfer correctly, but the label is not applied to the local copy and the transfer is not included in transferCount. I haven’t fully explored this, but it does behave this way on an AppleScript file that is not showing its extension.
The whole script is triggered to run at a predetermined time by a launchd plist (included below).
property serverName : "wwwwwwwwww"
property domainName : "xxxxxxx.yyy.zzz.gov"
property volumeName : "ves"
property mountpointName : "videos"
set theVolumeURL to "smb://" & serverName & "." & domainName & "/" & volumeName & "/" & mountpointName
set desktopFolder to path to desktop folder as text
set work_folder to alias (desktopFolder & "upVideo:")
set archiveFolder to alias (desktopFolder & "upVideo:archive:")
set listTransfer to ""
global listTransfer
set counter to ""
global counter
--try
set transferCount to 0
global transferCount
if volumeName is not in (paragraphs of (do shell script "/bin/ls /Volumes")) then
-- mount the shared drive
--beep for testing purposes
--beep 2
set this_folder to (mount volume theVolumeURL) as alias
delay 2
else
--beep for testing purposes
--beep 4
set POSIXpath to "/Volumes/" & volumeName
set this_folder to (POSIX file POSIXpath) as alias
end if
tell application "Finder" to set theFiles to every file of work_folder as alias list --get all files of local folder as alias
tell application "Finder" to set theRemoteFiles to (every file of ((this_folder as string) as alias)) --get all files of remote folder to check count in checkNames
--process each local file
repeat with i from 1 to number of items in theFiles
set this_item to item i of theFiles #GET A LOCAL FILE
tell application "Finder" to set LabelIndex to label index of this_item
if LabelIndex is not 6 then
tell application "Finder" to set this_name to displayed name of this_item
tell application "Finder" to set this_extension to name extension of this_item
set realName to (do shell script "echo " & quoted form of this_name & "| awk -F '" & quoted form of this_extension & "' '{print $1}'") --get file name without extension
--test
--get last character of realName
set lastChar to character -1 of realName
--if last character is a period, delete it
if lastChar is "." then
set realName to (text 1 thru ((length of realName) - 1)) of realName
--else move on
end if
set counter to 1 --set incrementing number to add to file name if file name exists on remote folder
my checkName(this_name, realName, this_item, this_extension, theRemoteFiles, this_folder) --call to check name and copy files
end if
end repeat
--UNCOMMENT THIS
--tell application "Finder" to eject this_folder
--display dialog ((transferCount as string) & " files were successfully transferred" & return & listTransfer)
my sendMessage()
--subroutines
--subroutine call includes non-global variables that need to be passed on to it
on checkName(this_name, realName, this_item, this_extension, theRemoteFiles, this_folder)
-- condition #1 - if the number of theRemoteFiles is greater than 0 then files exist in the remote folder and may contain duplicate names as local files - process further
-- condition #2 - if the number of theRemoteFiles is not greater than 0 then files do not exist in the remote folder and the local ones can just be copied over
--condition #1
if (count of theRemoteFiles) is greater than 0 then
try
my copyOver(this_item, this_folder, this_name)
-- check for "not overwritten" in error message to trigger renaming the remote file name to include an incremented number
on error errMssg
--tell application "Finder" to set label index of this_item to 6
if errMssg contains "not overwritten" then
set this_name to (realName & "_" & counter & "." & this_extension)
--increment appending number by 1 and check again
set counter to counter + 1
-- check again with new appending number
my checkName(this_name, realName, this_item, this_extension, theRemoteFiles, this_folder)
end if
end try
--condition #2
else
my copyOver(this_item, this_folder, this_name)
end if
end checkName
on copyOver(this_item, this_folder, this_name)
# THE -n OPTION IN THE SHELL COMMAND TELLS CP NOT TO OVERWRITE EXISTING FILES. AN ERROR OCCURS IF THE FILE EXISTS.
# THE -p OPTION IN THE SHELL COMMAND TELLS CP TO PRESERVE THE FOLLOWING ATTRIBUTES OF EACH SOURCE FILE IN THE COPY:
# modification time, access time, file flags, file mode,
#user ID, and group ID, as allowed by permissions. Access Control
#Lists (ACLs) and Extended Attributes (EAs), including resource
#forks, will also be preserved.
# THE -v OPTION IN THE SHELL COMMAND TELLS CP TO USE VERBOS MODE. WHICH GIVES US A BETTER CLUE OF THE ERROR
set theResult to (do shell script "cp -npv " & quoted form of (POSIX path of this_item) & space & quoted form of (POSIX path of (this_folder as string) & this_name))
if theResult contains "->" then
tell application "Finder" to set label index of this_item to 6
set transferCount to transferCount + 1
set listTransfer to listTransfer & "<BR>" & return & this_name
else
tell application "Finder" to set label index of this_item to 7
end if
move this_item to archiveFolder
end copyOver
on sendMessage()
tell application "Microsoft Outlook"
set theDate to current date
set theSubject to "New video uploaded on " & theDate
set myEmail to email address of exchange account 1
set otherMail to "www@xxxxx.yyy.zzz.gov"
set theList to "this is temporary text"
set theContent to "The following files have been uploaded:" & listTransfer & "<br>" & "<br>" & "<br>"
set newMessage to make new outgoing message with properties {subject:theSubject, content:theContent}
make new recipient at newMessage with properties {email address:{address:myEmail}}
make new recipient at newMessage with properties {email address:{address:otherMail}}
#make new cc recipient at newMessage with properties {email address:{name:"Name", address:"test@example.com"}}
--open newMessage
send message id (id of newMessage)
end tell
end sendMessage