首先上实现效果的视频:https://v.qq.com/x/page/x3021yp3u3a.html
MessageTemp目录下,存在很多子文件夹的
步骤1:创建类型为工作流的Automator
步骤2:具体实现Automator 工作流
流程2对应的AppleScript脚本如下:
on run {input, parameters}
-- 获取过滤规则
set rule to item -1 of input
--将input设置为待处理的目录
set input to item 1 of input
set folderPath1 to (input as text) & "OpenData:"
set folderPath2 to (input as text) & "File:"
tell application "Finder"
try
set files1 to get document files of folder folderPath1
set files2 to get document files of folder folderPath2
set allFiles to files2
--过滤后的文件
set filterFiles to {}
set theList to {}
repeat with aFile in allFiles
--文件扩展名
set extensionName to the name extension of aFile
--文件名称
set fileName to the name of aFile
try
set SuffixRules to suffix of rule
set stopStartRule to false
--创建时间
set theFileDate to (creation date of aFile as string)
set oneminutes to (current date) - 1 * minutes
if theFileDate > oneminutes then
--是否在扩展名规则里,如果在直接 放入filterFiles 不进行后续规则过滤
repeat with suffix in SuffixRules
if (extensionName as text) ends with suffix then
set the end of filterFiles to aFile
set stopStartRule to true
exit repeat
end if
end repeat
-- 以什么开头规则 和 不以什么结尾规则是 且的关系
if stopStartRule is false then
-- 以什么开头规则 和 不能以什么结尾
set startRules to start of rule
repeat with startRule in startRules
if fileName starts with startRule then
set notEndRules to notEnd of rule
set needAdd to true
repeat with notEndRule in notEndRules
if extensionName = notEndRule then
needAdd without to
exit repeat
end if
end repeat
if needAdd is true then
set the end of filterFiles to aFile
exit repeat
end if
end if
end repeat
end if
end if
on error the error_message number the error_number
display dialog "Error: " & the error_number & "." & the error_message buttons {"OK"}
end try
end repeat
return filterFiles
on error the error_message number the error_number
display dialog "Error: " & the error_number & "." & the error_message buttons {"OK"}
end try
end tell
end run
流程3对应的AppleScript脚本如下:
on run {input, parameters}
if (count of input) ≤ 0 then
-- 没有需要移动的文件
display notification "没有需要移动的文件"
return {}
else
local params
set params to {}
repeat with aFile in input
set curPath to aFile as text
set params to params & curPath & "|"
end repeat
return params
end if
end run
on theSplit(theString, theDelimiter)
-- 保存分隔符来恢复旧的设置
set oldDelimiters to AppleScript's text item delimiters
-- 设置分隔符分隔符使用
set AppleScript's text item delimiters to theDelimiter
-- 创建数组 并且 数组theArray为文本类型
set theArray to text items of theString
--恢复旧的设置
set AppleScript's text item delimiters to oldDelimiters
return theArray
end theSplit
流程5对应的AppleScript脚本如下:
on run {input, parameters}
-- 获取下拉框选项值
set chooseFromList to item -1 of input
set chooseFromList to theSplit(chooseFromList, "|")
--将变量参数剔除
set input to items 1 through -2 of input
if input is {} then
return {}
else
set flodPath to choose from list chooseFromList & "选择文件夹"
--display dialog ((flodPath) as text)
if flodPath is false then
return
end if
set re to item -1 of flodPath
if re = "选择文件夹" then
set folderSelected to choose folder "Select a folder"
if folderSelected is false then return
set re to POSIX path of folderSelected
end if
set input to input & re
return input
end if
(* Your script goes here *)
return input
end run
on theSplit(theString, theDelimiter)
-- 保存分隔符来恢复旧的设置
set oldDelimiters to AppleScript's text item delimiters
-- 设置分隔符分隔符使用
set AppleScript's text item delimiters to theDelimiter
-- 创建数组 并且 数组theArray为文本类型
set theArray to text items of theString
--恢复旧的设置
set AppleScript's text item delimiters to oldDelimiters
return theArray
end theSplit
流程6对应的AppleScript脚本如下:
on run {input, parameters}
if the length of input < 2 then
return
end if
--获取目标路劲
set targetPath to item -1 of input
--获取文件路劲
set filesPaths to items 1 through -2 of input
repeat with aFile in filesPaths
try
set theLength to the length of ((aFile) as text)
if (theLength > 0 and isDirectory(aFile) is false) then
set filePath to POSIX path of aFile as text
tell application "Finder" to set fileExists to exists my POSIX file filePath
if fileExists is true then
set fArray to my theSplit((filePath) as string, "/")
set fname to item -1 of fArray
--判断目标路劲是否存在同名文件
set targetPathFile to targetPath & fname
set file1 to POSIX file filePath
set relTargetPath to POSIX file targetPath as text
tell application "Finder" to set targetPathFileExists to exists my POSIX file targetPathFile
if targetPathFileExists is true then
set tempVar to display dialog "文件 [" & fname & "] 已存在,是否覆盖" buttons {"是", "否"}
set theButtonPressed to button returned of tempVar
if theButtonPressed = "是" then
tell application "Finder"
--覆盖 复制
duplicate file1 to relTargetPath with replacing
display notification "成功移动到:" & targetPath with title ("文件: " & fname as text)
end tell
else
--exit repeat
end if
else
tell application "Finder"
--复制
duplicate file1 to relTargetPath
display notification "成功移动到:" & targetPath with title ("文件: " & fname as text)
end tell
end if
end if
end if
on error the error_message number the error_number
display dialog "Error: " & the error_number & "." & the error_message buttons {"OK"}
end try
end repeat
end run
on theSplit(theString, theDelimiter)
--display dialog ((theString) as text)
-- 保存分隔符来恢复旧的设置
set oldDelimiters to AppleScript's text item delimiters
-- 设置分隔符分隔符使用
set AppleScript's text item delimiters to theDelimiter
-- 创建数组 并且 数组theArray为文本类型
set theArray to text items of theString
--恢复旧的设置
set AppleScript's text item delimiters to oldDelimiters
-- return the result
return theArray
end theSplit
on isDirectory(someItem)
set filePosixPath to quoted form of (POSIX path of someItem)
set fileType to (do shell script "file -b " & filePosixPath)
if fileType ends with "directory" then return true
return false
end isDirectory
以上就是实现该功能的所有步骤,有需要的也可在文章后面的附件自行下载,实现过程历经艰辛,因为自己之前也没接触过AppleScript语言。
Automator CICD Consul Consul-template docker docker-compose elasticsearch gitlab gitlab-runner harbor Hazel hybris java kubernetes mac macx高效率 nginx rancher spring boot spring cloud swarm 分布式 序列号 有规则