2

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

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

8 Comments

Hello mklement0, many thanks for your answer. I am using a 10.8.4 Mac so I guess I am using AppleScript version 2.2.4
I copied your script and made the necessary changes - I must confess, my script was overly bloated. When I tried to run it I get a "The was a problem connecting to the server" error "An error of type -5016 has occurred". I can fix this by removing the "& destShareName" on the Mount Volume command. It then ran a treat. Many thanks. I about to schedule it.
@JohnHarris: Glad to hear it; did the scheduling work?
Hello mklement0, thank you for your message. I am having real problems with launchd. I used Lingon to create a job with the full path to both osascript and the script itself. I also added the "Keep it running" (= <key>KeepAlive</key> <true/> and a specific time. However, it starts on system startup (despite not ticking "Run it when it is loaded") and it fails (Exited with a code 1) then Throttles a respawn every 10 seconds ad infinitum (probably due to the KeepAlive). I tried editing the launchd plist and put in StandardErrorPath and StandardOutPath but it simply never runs. Very annoying. ATB
@JohnHarris: Sorry to hear it. In Lingon 3, if you have At login and load checked, the script should run immediately when you click the Save & Load button - if you try that, does the script run correctly then?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.