Question

I have an AppleScript that mounts a network smb share, creates destination folders (if they don't exist) then copy files to the destination folders with replacing. It then unmounts the smb share.

I have heavily edited to remove sensitive information but the gist is:

-- AppleScript to backup crucial files from server without TimeMachine
-- J Harris 20-12-13
-- v1 created alias and used this for folder creation 26-12-13
-- v2 added share4 folder 03-01-14
-- Mount the destination and create an alias
mount volume "smb://<username>:<password>@server.domain.com/sharename"
set server to result as alias
-- do the copy
tell application "Finder"
    -- Create destination folder & copy the * files. This will overwrite all files
    if not (exists POSIX file "/Volumes/sharename/share1") then make new folder with properties {name:"share1"} at server
    duplicate items of folder "Macintosh HD:<location to share1>" to POSIX file "/Volumes/sharename/share1" with replacing
    --Create destination folder and copy the ** files. This will overwrite all files
    if not (exists POSIX file "/Volumes/sharename/share2") then make new folder with properties {name:"share2"} at server
    duplicate items of folder "Macintosh HD:<location to share2>" to POSIX file "/Volumes/sharename/share2" with replacing
    --Create destination folder and copy *** files. This will overwrite all files
    if not (exists POSIX file "/Volumes/sharename/share3") then make new folder with properties {name:"share3"} at server
    duplicate items of folder "Macintosh HD:<location to share3>" to POSIX file "/Volumes/sharename/share3" with replacing
    --Create destination folder and copy all local **** files. This will overwrite all files
    if not (exists POSIX file "/Volumes/sharename/share4") then make new folder with properties {name:"share4"} at server
    duplicate items of folder "Macintosh HD:<location to share4>" to POSIX file "/Volumes/sharename/share4" with replacing
    -- Unmount the destination
    eject server
end tell

I saved it as an app and it works beautifully.

I then tried to schedule the task using Lingon to edit launchd. I have read that you can't point launchd to the application itself, you have to point it to the app/Contents/MacOS/Applet

When it runs, I get an AppleScript Editor error "Folder some object not found".

Anybody got any ideas?

Thanks in advance

John

Was it helpful?

Solution

I have no specific explanation for your symptoms, but a few observations:

  • Your script as written doesn't work on my OS X 10.8 and 10.9 machines - find a streamlined version below. What version are you using?
  • Given the nature of your script there's no strict reason to save it as an application - you could instead save it to a regular *.scpt file and tell Lingon to execute the following (note the need to use full paths both for osascript and your *.scpt file):
    /usr/bin/osascript /path/to/your/script.scpt

The problems I encountered on OS X 10.8 and 10.9:

  • The mount volume command neither directly returns a value nor sets the global result variable - my streamlined version simply tries instead to access the share as a mounted disk.
  • The variable name server resulted in strange errors; changing the name made them go away.

Streamlined version:

# Mount the share.
# Note that at least on OS X 10.8 and 10.9 this command:
#  - is a no-op if the share is already mounted
#  - in either case has NO return value and does NOT set the 
# global `result` variable.
# The share will be mounted as "disk" /Volumes/{shareName} (POSIX) / 
# {shareName}: (HFS)
# Note that the `mount volume` is defined in the Standard Additions dictionary, 
# not the Finder's.
set destShareName to "sharename"
mount volume "smb://<username>:<password>@server.domain.com/" & destShareName

# Getting here without error means that the share was just mounted
# or was previously mounted.

tell application "Finder"

    # Get a reference to the mounted share.
    # !! It seems that at least on OSX 10.9 it can take a few seconds 
    # !! before a just-mounted
    # !! share becomes accessible.
    # !! We try for a while, but eventually give up.
    set numTries to 5 # How many seconds (at least) to keep trying.
    set numTry to 1
    repeat
        try
            set destShare to disk destShareName
            exit repeat
        on error errMsg number errNo
            set numTry to numTry + 1
            if numTry > numTries then error errMsg & " - giving up after " & numTries & "+ seconds." number errNo
            delay 1 # Sleep for 1 second, then try again.
        end try
    end repeat

    # Define the SOURCE *HFS PATHS*.
    set sourceFolderPaths to {"Macintosh HD:<location to share1>", "Macintosh HD:<location to share2>", "Macintosh HD:<location to share3>", "Macintosh HD:<location to share4>"}
    # Define the corresponding DESTINATION *FOLDER NAMES*
    set destFolderNames to {"share1", "share2", "share3", "share4"}

    # Process all folders.  
    repeat with i from 1 to length of sourceFolderPaths
        set sourceFolderPath to item i of sourceFolderPaths
        set destFolderName to item i of destFolderNames
        # Get a reference to the destination folder or create it, if it doesn't yet exist.
        try
            set destFolder to folder destFolderName of destShare
        on error # folder access failed -> assume it must be created
            set destFolder to make new folder with properties {name:destFolderName} at destShare
        end try
        # Copy the files. This will overwrite all files.
        duplicate items of folder sourceFolderPath to destFolder with replacing
    end repeat

    # Unmount the share.
    eject destShare

end tell
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top