applescript working with mounted shares

Hey-o,


I am putting together an applescript that will mount a share, work with files on that share, then unmount it when done.


Here is some sanitized code...


set LCD to (mount volume "smb://servername/sharename")

( process files)

tell application "Finder"


eject disk LCD

end tell

This works perfectly when the share is not already mounted when the script runs. If the share is already mounted, it complains about LCD being undefined when the script hits the eject disk command. The language guide says mount volume returns nothing, but ejecting LCD works. I would prefer not to use eject disk "sharename" because there is the possibility of a share with the same name being mounted from a different server.

I tried putting the mount volume into a TRY block, but apparently having the volume already mounted isn't an error condition.

Ideally, what I would like is for LCD to get a disk handle (or whatever applescript calls it) from the new mount OR from the existing mount, which I can then eject.

Is this possible?

Thank you.

Paul Baughman

Posted on Dec 20, 2016 12:23 PM

Reply
10 replies

Dec 20, 2016 3:14 PM in response to Paul Baughman

Here is AppleScript that gets a list of ejectable, mounted drives, and uses that list to create an interactive selection of drives from which you choose one to eject.


tell application "Finder"

get name of (every disk whose ejectable is true and its name is not in {"Time Machine", "TIME MACHINE"}) as list

set VolumeName to (choose from list (result) with title "Mounted Volumes" default items "" without multiple selections allowed and empty selection allowed) as «class utf8»

if VolumeName is false then return -- user pressed cancel button

end if


-- now you can eject the VolumeName

Dec 21, 2016 8:12 AM in response to VikingOSX

If I execute your script it throws an error:

User uploaded file


Oddly enough, if I change the ejectable to FALSE, it works just fine, giving a list of all the disks mounted. I can also use "local volume is false" and that seems to work too.


I see in the dictionary for Finder something called "Internet Location File". Would that give me the location of the fileserver and/or path? I can't see how to use it.


Paul.

Dec 21, 2016 9:55 AM in response to Paul Baughman

Paul,


The lessons of copy/paste from existing code that works, but not entirely in context .... The following code works as is, and if there is an ejectable volume mounted, it will appear in a list prompt.


tell application "Finder"

get name of (every disk whose ejectable is true and its name is not in {"Time Machine", "TIME MACHINE"}) as list

set VolumeName to (choose from list (result) with title "Mounted Volumes" default items "" without multiple selections allowed and empty selection allowed) as «class utf8»

end tell

if VolumeName is false then return -- user pressed cancel button

return

As far as ejectable as false category, that will show the boot drive, home, net, and network mounted drives. I have a video folder on my Desktop that is mounted from my Synology DiskStation via AppleShare. It shows as one of these ejectable is false disks.

The Internet location file takes an existing file (e.g. csf.docx), and creates the following on my Desktop:

User uploaded file


This is not what you thought it was, and is the file counterpart to a webloc document. If I double-click on this icon, it will open the document csf.docx in its default opening application (e.g. Pages v5.6.2). The following code is lifted from StefanK's example at MacScripter here.


set f to choose file

set myName to text returned of (display dialog "Enter Filename" default answer name of (info for f))

tell application "Finder"

set file_url to URL of f

makenewinternet location fileatdesktoptofile_urlwith properties {name:myName}

end tell

return

And the fileloc content is the following (note the file:/// URI).

<plist version="1.0">

<dict>

<key>URL</key>

<string>file:///Users/viking/Desktop/csf.docx</string>

</dict>

</plist>

Dec 21, 2016 2:24 PM in response to Paul Baughman

Hello


You may use mount(8) to query SMB share address and its mount point.


#!/bin/bash mount | grep smbfs



If the SMB share address is listed with a mount point, e.g., /Volumes/SHARE, use the mount point. Otherwise use mount_smbfs(8) to mount the SMB address to whatever mount point you define and use it. Finally you may use umount(8) on the mount point to unmount the share.


Regards,

H

Dec 21, 2016 7:08 PM in response to Paul Baughman

A regular user can issue mount | grep smbfs. These are examples. I'm not using the mount command in the examples.

(*

It is easier to diagnose problems with debug information. I suggest adding
log statements to your script to see what is going on.  Here is an example.


    Author: rccharles
  
    For testing, run in the Script Editor.
      1) Click on the Event Log tab to see the output from the log
          statement
      2) Click on Run
    
  For running shell commands see:
  http://developer.apple.com/mac/library/technotes/tn2002/tn2065.html
  


*)


on run
    -- Write a message into the event log.
    log "  --- Starting on " & ((current date) as string) & " --- "
    --  debug lines
    set desktopPath to (path to desktop) as string
    log "desktopPath = " & desktopPath
  
    set unixDesktopPath to POSIX path of desktopPath
    log "unixDesktopPath = " & unixDesktopPath
  
    set quotedUnixDesktopPath to quoted form of unixDesktopPath
    log "quoted form is " & quotedUnixDesktopPath
  
    try
        set fromUnix to do shell script "ls -l  " & quotedUnixDesktopPath
        display dialog "ls -l of " & quotedUnixDesktopPath & return & fromUnix
    on error errMsg
        log "ls -l error..." & errMsg
    end try
  
end run


to mount a volume, you need to be administrator.


(*

It is easier to diagnose problems with debug information. I suggest adding log statements to your script to see what is going on.  Here is an example.


    Author: rccharles
  
    For testing, run in the Script Editor.
      1) Click on the Event Log tab to see the output from the log statement
      2) Click on Run
    
    For running shell commands see:
    http://developer.apple.com/mac/library/technotes/tn2002/tn2065.html
  


*)


on run
    -- Write a message into the event log.
    log "  --- Starting on " & ((current date) as string) & " --- "
    --  debug lines
  
  
    set unixDesktopPath to POSIX path of "/System/Library/User Template/"
    log "unixDesktopPath = " & unixDesktopPath
  
    set quotedUnixDesktopPath to quoted form of unixDesktopPath
    log "quoted form is " & quotedUnixDesktopPath
  
    try
        set fromUnix to do shell script "sudo ls -l  " & quotedUnixDesktopPath with administrator privileges
        display dialog "ls -l of " & quotedUnixDesktopPath & return & fromUnix
    on error errMsg
        log "ls -l error..." & errMsg
    end try
  
end run

Dec 22, 2016 3:21 PM in response to Paul Baughman

VikingOSX,

Your code, exactly as copy/pasted, does not work on my system. The screencap I posted is the result of my trying to get it to work by cutting out stuff. the full code throws the same error as my cut down version. Could it be OS version?


The ejectable property, when true throws the error, but when false does not, in either your full code or my cut down version.


I guess the 'internet location file' isn't what I was hoping for.


I'll use one of the smb commands listed later in this thread to find the info I need.


Thanks all.


Paul.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

applescript working with mounted shares

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.