Applescript - 获取POSIX路径的一部分并创建具有该名称的文件夹

时间:2014-04-11 21:30:34

标签: applescript

我在工作文件夹中有一些文件包含jobnames_jobnumber /work/jobnameA_001/outbox/filename.mp4,/work/jobnameB_002/outbox/flamename.mp4 e.t.c

我想让一个AppleScript获取完整的POSIX路径,然后只提取路径的jobnameA_001部分,然后用jobnames_jobnumber_date创建一个新文件夹

请注意,作业名是客户名称,因此它们不是固定长度。

请注意我是一个完整的新手,我不确定要研究什么来找到答案。但我会试试。

谢天谢地,

1 个答案:

答案 0 :(得分:1)

这是执行您想要的代码:

# Extract all <jobName>_<jobNumber> tokens from matching paths.
set jobNamesWithNumbers to paragraphs of ¬
  (do shell script "for p in /work/*/outbox/*.mp4; do echo \"$p\"; done | 
                    awk -F/ '{ print $2 }'")

# Get the date in format YYYY-MM-DD, e.g., "2014-04-13".
set dateString to do shell script "date +'%Y-%m-%d'"

# Specify the target folder in which to create the new folders:
set targetFolder to POSIX path of (path to desktop)

# Build the absolute paths of the folders to create.
set quotedFolderPathList to ""
repeat with itm in jobNamesWithNumbers
    set folderPath to (targetFolder & "/" & itm & "_" & dateString)
    set quotedFolderPathList to quotedFolderPathList & " " & ¬
                                  quoted form of folderPath
end repeat

# Finally, create the new folders.
do shell script "mkdir -p" & quotedFolderPathList

请注意,这很大程度上依赖于使用bash调用的shell(do shell script)命令,这样可以缩短代码。

然而,这是以必须理解两种语言为代价的。 鉴于AppleScript的局限性,从长远来看,值得进行这种学习投资。