启动AppleScript错误“找不到某个对象的文件夹”

时间:2014-01-24 16:30:23

标签: applescript launchd

我有一个AppleScript,它安装网络smb共享,创建目标文件夹(如果它们不存在),然后将文件复制到目标文件夹并进行替换。然后卸载smb共享。

我进行了大量编辑以删除敏感信息,但要点是:

-- 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

我将它保存为应用程序,它的工作非常精彩。

然后我尝试使用Lingon来安排任务来编辑launchd。我已经读过你不能将launchd指向应用程序本身,你必须将它指向app / Contents / MacOS / Applet

运行时,我收到AppleScript编辑器错误“找不到某个对象的文件夹”。

有人有任何想法吗?

提前致谢

约翰

1 个答案:

答案 0 :(得分:1)

我对您的症状没有具体解释,但有一些观察结果:

  • 您编写的脚本在我的OS X 10.8和10.9机器上不起作用 - 请在下面找到简化版本。你在用什么版本?
  • 鉴于您的脚本的性质没有严格的理由将其保存为应用程序 - 您可以将其保存到常规*.scpt文件并告诉Lingon执行以下操作(注意需要使用osascript*.scpt文件的完整路径:
    /usr/bin/osascript /path/to/your/script.scpt

我在OS X 10.8和10.9上遇到的问题:

  • mount volume命令既不直接返回值也不设置全局result变量 - 我的简化版本只是尝试将共享作为已挂载的磁盘来访问。
  • 变量名server导致奇怪的错误;更改名称使他们离开。

简化版:

# 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