سؤال

I want to be sure that the USB stick at 'usbMountPoint' is also the one with the matching 'usbSerialNumber'

-- Get Mount Point of script's USB host
tell application "Finder"
   try
       set usbMountPoint to text 1 thru -2 of POSIX path of (disk of (path to me) as alias)
       log usbMountPoint --> Output: /Volumes/usb-drive-name
   on error
       set usbMountPoint to POSIX path of (disk of (path to me) as alias) --> if file is on desktop, path: '/', can't have ' ' hence no removing
       log usbMountPoint --> Output: /Volumes/usb-drive-name
   end try
end tell

-- Set Serial Num of USB Stick
set usbSerialNumber to "C86000BDB9F2BFA04A31E8A1"

-- Check if usbMountPoint and usbSerialNumber exist
set sysProfileUSB to (do shell script "system_profiler SPUSBDataType") as text
if sysProfileUSB contains "Serial Number: " & usbSerialNumber and sysProfileUSB contains "Mount Point: " & usbMountPoint then
   log "USB Stick Found!"
else
   log "USB Stick Not found."
end if

When validating to check if usbMountPoint and usbSerialNumber exist in system_profiler, the code should check if the USB device at 'usbMountPoint' has the serial number of 'usbSerialNumber'. Without this "matching/pairing", the user could simply have two USBs connected - one with the right serial number, the other with the script; while the code will return "Found" becuase 'contains' reads the entire system_profiler without validating that both variables come from the same device.

Any help would be greatly appreciated. Cheers.

هل كانت مفيدة؟

المحلول

Of course I do not know the total scope of what you're trying to accomplish however because as you presently have it coded, the script could be running from other then the target USB Drive. So I've written it a bit differently, however it does get the mount point based on the serial number if the target USB Drive is mounted. You can then branch accordingly to the conditions, i.e. whether the script is running from the target USB Drive or not and if the target USB Drive is mounted or not, etc. After getting the mount point in the code, I've included another block of code to test against if the target USB Drive is mounted and where the script was run from. This obviously is just one example of testing and you'll have to do as you need. The bottom line here is, how I coded getting the mount point of the target USB Drive based on the serial number and this I guess it what you're really looking for.

Here's my take on the code and it's output when run from both the Desktop and the target USB Drive:

  • Note: Before use, change the actual serial number below to the appropriate value.

AppleScript Code:

--    # Get the volume this script is located on.

tell application "Finder"
    try
        set theVolumeThisScriptIsLocatedOn to text 1 thru -2 of POSIX path of (disk of (path to me) as alias)
        log "The volume this script is located on is: " & quoted form of theVolumeThisScriptIsLocatedOn
    on error
        set theVolumeThisScriptIsLocatedOn to POSIX path of (disk of (path to me) as alias)
        log "The volume this script is located on is: " & quoted form of theVolumeThisScriptIsLocatedOn
    end try
end tell

--    # Set the Serial Number of the target USB Drive.

set usbSerialNumber to "200434112111BA425FA4"

--    # See if the USB Drive matching 'usbSerialNumber' is mounted and if so, get its mount point.
--    # The variable 'usbMountPoint' will contain either its mount point, e.g., '/Volumes/...', or '' as in nothing.

set usbMountPoint to (do shell script "system_profiler SPUSBDataType | awk '/" & usbSerialNumber & "/' RS= | awk -F': ' '/Mount Point: /{print $2}'") as text
log "The mount point is: " & quoted form of usbMountPoint
if usbMountPoint is equal to "" then
    log "The target USB Drive is not mounted!"
else
    log "The target USB Drive is mounted!"
end if

--    # See if the script is running from the target USB Drive or elsewhere.

if usbMountPoint is not equal to "" and theVolumeThisScriptIsLocatedOn is equal to usbMountPoint then
    log "This script is running from the target USB Drive!"
else
    log "This script is not running from the target USB Drive!"
end if

Replies when run from the target USB Drive:

tell current application
    path to current application
        --> alias "16GB-USB:USB Test.scpt"
end tell
tell application "Finder"
    get disk of alias "16GB-USB:USB Test.scpt"
        --> alias "16GB-USB:"
    (*The volume this script is located on is: '/Volumes/16GB-USB'*)
end tell
tell current application
    do shell script "system_profiler SPUSBDataType | awk '/200434112111BA425FA4/' RS= | awk -F': ' '/Mount Point: /{print $2}'"
        --> "/Volumes/16GB-USB"
    (*The mount point is: '/Volumes/16GB-USB'*)
    (*The target USB Drive is mounted!*)
    (*This script is running from the target USB Drive!*)
end tell

Replies when run from the Desktop with the target USB Drive connected:

tell current application
    path to current application
        --> alias "Macintosh HD:Users:me:Desktop:USB Test.scpt"
end tell
tell application "Finder"
    get disk of alias "Macintosh HD:Users:me:Desktop:USB Test.scpt"
        --> alias "Macintosh HD:"
end tell
tell current application
    path to current application
        --> alias "Macintosh HD:Users:me:Desktop:USB Test.scpt"
end tell
tell application "Finder"
    get disk of alias "Macintosh HD:Users:me:Desktop:USB Test.scpt"
        --> alias "Macintosh HD:"
    (*The volume this script is located on is: '/'*)
end tell
tell current application
    do shell script "system_profiler SPUSBDataType | awk '/200434112111BA425FA4/' RS= | awk -F': ' '/Mount Point: /{print $2}'"
        --> "/Volumes/16GB-USB"
    (*The mount point is: '/Volumes/16GB-USB'*)
    (*The target USB Drive is mounted!*)
    (*This script is not running from the target USB Drive!*)
end tell

Replies when run from the Desktop without the target USB Drive connected:

tell application "Finder"
    get disk of alias "Macintosh HD:Users:me:Desktop:USB Test.scpt"
        --> alias "Macintosh HD:"
end tell
tell current application
    path to current application
        --> alias "Macintosh HD:Users:me:Desktop:USB Test.scpt"
end tell
tell application "Finder"
    get disk of alias "Macintosh HD:Users:me:Desktop:USB Test.scpt"
        --> alias "Macintosh HD:"
    (*The volume this script is located on is: '/'*)
end tell
tell current application
    do shell script "system_profiler SPUSBDataType | awk '/200434112111BA425FA4/' RS= | awk -F': ' '/Mount Point: /{print $2}'"
        --> ""
    (*The mount point is: ''*)
    (*The target USB Drive is not mounted!*)
    (*This script is not running from the target USB Drive!*)
end tell

Understanding what the set usbMountPoint to (do shell script "...") as text command line is doing to set the value of the usbMountPoint variable.

  • system_profiler SPUSBDataType | outputs all information related to the USB hardware and then gets piped to the first occurrence of awk in the command line.
  • awk '/" & usbSerialNumber & "/' RS= | looks for the serial number and outputs everything from the first blank line before the serial number and the first blank line after the serial number and its output then gets piped to the second occurrence of awk in the command line. This ensures the only line containing '/Mount Point:' in the output is associated with the serial number (if the target USB Drive is mounted).
  • awk -F': ' '/Mount Point: /{print $2}' finds the line containing 'Mount Point: ' then uses ': ' as the field separator and prints the second field which will either be the POSIX pathname of the mount point of the target USB Drive, or '' as in nothing (because it wasn't mounted).
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى apple.stackexchange
scroll top