functionM.debounce(func, delay) local timer = nil returnfunction(...) local args = {...} if timer then timer:stop() end timer = hs.timer.doAfter(delay, function() func(table.unpack(args)) end) end end
functionM.filterHiddenFiles(files) local filteredFiles = {} for _, file inipairs(files) do local fileName = file:match("([^/]+)$") if fileName andnot fileName:match("^%.") then table.insert(filteredFiles, file) end end return filteredFiles end
functionM.runRclone() local localPath = "/Users/XXX/Library/Mobile\\ Documents/iCloud~md~obsidian/Documents/Afanty" local remotePath = "webdav:/Afanty" -- 从本地同步到远程,包括删除操作 local syncToRemoteCommand = string.format("rclone sync %s %s --delete-during", localPath, remotePath) -- 从远程同步到本地,包括删除操作 local syncToLocalCommand = string.format("rclone sync %s %s --delete-during", remotePath, localPath) -- 执行从本地到远程的同步 hs.task.new("/bin/bash", function(exitCode, stdOut, stdErr) if exitCode == 0then print("本地到远程同步成功完成") -- 执行从远程到本地的同步 hs.task.new("/bin/bash", function(exitCode2, stdOut2, stdErr2) if exitCode2 == 0then print("远程到本地同步成功完成") print("双向同步全部完成") else print("远程到本地同步失败") print("错误:", stdErr2) end end, {"-c", syncToLocalCommand}):start() else print("本地到远程同步失败") print("错误:", stdErr) end end, {"-c", syncToRemoteCommand}):start() end
localfunctiondebouncedRunRclone() utils.debounce(function() print("执行rclone同步") M.runRclone() end, 5) end
functionM.watchFolder() local folderPath = "/Users/XXX/Library/Mobile Documents/iCloud~md~obsidian/Documents/Afanty" if watcher then watcher:stop() end watcher = hs.pathwatcher.new(folderPath, function(files) local changedFiles = utils.filterHiddenFiles(files) if #changedFiles > 0then print("检测到文件变化") for _, file inipairs(changedFiles) do print("变化的文件:", file) end debouncedRunRclone() end end) watcher:start() print("开始监控文件夹:", folderPath) end