【问题标题】:launchd AppleScript error "Folder some object not found"启动 AppleScript 错误“未找到文件夹”
【发布时间】:2023-03-24 01:07:01
【问题描述】:

我有一个 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 编辑器错误“未找到文件夹”。

有人有什么想法吗?

提前致谢

约翰

【问题讨论】:

    标签: applescript launchd


    【解决方案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
    

    【讨论】:

    • 您好 mklement0,非常感谢您的回答。我使用的是 10.8.4 Mac,所以我想我使用的是 AppleScript 2.2.4 版
    • 我复制了您的脚本并进行了必要的更改 - 我必须承认,我的脚本过于臃肿。当我尝试运行它时,我收到“连接到服务器时出现问题”错误“发生了 -5016 类型的错误”。我可以通过删除 Mount Volume 命令上的“& destShareName”来解决此问题。然后它运行了一个款待。非常感谢。我正要安排它。
    • @JohnHarris:很高兴听到这个消息;日程安排成功了吗?
    • 您好 mklement0,感谢您的留言。我在使用launchd时遇到了真正的问题。我使用 Lingon 创建了一个包含 osascript 和脚本本身的完整路径的作业。我还添加了“保持运行”(= KeepAlive 和特定时间。但是,它在系统启动时启动(尽管没有勾选“加载时运行”)并且它失败(以代码 1 退出)然后限制每 10 秒无限次重生(可能是由于 KeepAlive)。我尝试编辑启动的 plist 并放入 StandardErrorPath 和 StandardOutPath 但它根本不会运行。非常烦人。ATB
    • @JohnHarris:很抱歉听到这个消息。在 Lingon 3 中,如果您选中了 At login and load,则当您单击 Save &amp; Load 按钮时,脚本应该立即运行 - 如果您尝试这样做,那么脚本是否运行正确?
    猜你喜欢
    • 1970-01-01
    • 2020-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-05
    相关资源
    最近更新 更多