0

希望可以有人帮帮我,

首先,我的 AppleScript 技能几乎不存在。

我得到了一些用于归档目的的文件,格式如下,需要排序。格式首先是设备名称,然后是运行编号和样品编号,然后是一些文本。

示例文件:“UZLA 55879 [05x13] september cal run.cdf”(文件格式不同)需要移动到文件夹中:~/UZLA 55879 (LCMS)/Run 5/

  • 设备名称是相当随机的,有时只是一个数字,有时是整个官方命名。
  • 主文件夹在设备名称后的方括号中有一个辅助项,该辅助项不在要移动的文件名中。"[" 和 "(" 之前的字符串匹配,在主名称之后它是不同的。
  • 子文件夹并不总是存在,当开始新的运行时,文件夹 /Run 6/ 例如可能不存在。他们对数字没有 0 填充
  • 这些文件都到达同一个文件夹中,它们不应该是位于该文件夹中的其他文件。

为了解决这个问题,我们喜欢在主驱动器上的单个文件夹中创建一个别名(文件被移动到外部系统)以便直接访问,这很容易快速查找,但对于整个系统来说完全不方便(旧的别名是被其他脚本从中删除)。

谢谢。


好的,所以这是令人讨厌的关闭,外观,因为它只帮助我,而不是来自未来的随机人。对我来说获得帮助有点意思。这就是我们所处的位置,感谢 adayzdone:

set myFolder to (choose folder)
tell application "System Events" to set folderName to myFolder's name
set {TID, text item delimiters} to {text item delimiters, {"(", ")"}}
set myText to text item 2 of folderName
set text item delimiters to TID

set myFiles to every paragraph of (do shell script "find " & quoted form of (POSIX path of myFolder) & " \\! -name \".*\" -type f")
repeat with aFile in myFiles
    set aFile to aFile as text
    tell application "Finder" to set fileName to name of (POSIX file aFile as alias)
    set newPath to "~/" & quoted form of (do shell script "echo " & quoted form of fileName & " | sed -e 's/\\[0/\\[/' -e 's/\\([^\\[]*\\)\\[\\([0-9]*\\)x[0-9]*\\].*/\\1(" & myText & ")\\/Run \\2\\//'") as text
    do shell script "mkdir -p " & newPath & " ; mv " & quoted form of aFile & space & newPath
end repeat

这会产生以下错误:错误“无法获取 \"testFolder\" 的文本项 2。” “testFolder”的文本项 2 中的数字 -1728


让我澄清一下:我在一个名为 /testFolder/ 的文件夹中有一堆文件(总是这样命名,所有文件都在这里输入)我想要的是根据文件名将文件移动到给定格式的文件夹和子文件夹中。

示例:文件:/UZLA 55879 [01x05] XXX.cdf

  • 基本名称“UZLA 55879”,文件夹 /UZLA 55879 (LCMS)/ 存在于目的地。(LCMS)与移动无关,它只是文件夹名称上的额外垃圾,脚本应该检测到该文件夹​​存在(尽管 ()" 之间有什么垃圾并将其用作目标。如果没有具有该基础的文件夹名称存在它可能只是弹出一个错误或使整个脚本崩溃,这并不是真正的问题,因为很少创建新的基本名称并且无论如何都是手动命名的(而且是随机命名的)。
  • 名称的第二部分是 [01x05] 的第一部分,“01”被检测到(从其填充零中删除)并移动到子文件夹 /Run 1/ (如果它是“[05x07] 进入 /Run 7/等文件名/扩展名的其余部分与移动无关。

当前问题:脚本现在尝试从起始文件夹中提取信息以选择目标文件夹,起始文件夹(未)命名为 /UZLA 55879 (LCMS)/ 它使用该“LCMS”来创建目标文件夹。(它不能因为起始文件夹是 1)没有命名和 2))每个目标文件夹在这些括号之间都有一个不同的项目(尽管有些是相同的),所以像这样命名起始文件夹是没有用的。该脚本为此使用“ & myText & ”,该字符串必须是由目标文件夹而不是起始文件夹定义的随机字符串。

4

1 回答 1

0

除非用户付出了一些努力,否则我通常不会回答这样的问题,但我是正则表达式的傻瓜。

set sourceFolder to "/Users/You/Desktop/testFolder"
set destFolder to "/Users/You/Desktop/testFolder2"

set myFiles to every paragraph of (do shell script "find " & quoted form of sourceFolder & " \\! -name \".*\" -type f")

repeat with aFile in myFiles
    set aFile to aFile as text
    tell application "Finder" to set fileName to name of (POSIX file aFile as alias)

    -- Find name of parent folder
    set parentName to do shell script "echo " & quoted form of fileName & " | sed 's/ \\[.*//'"
    tell application "System Events" to set parentFolder to POSIX path of (first folder of folder destFolder whose name starts with parentName)

    -- Extract the text between ( and ) 
    set myText to do shell script "echo " & quoted form of parentFolder & " | sed 's/.*(\\(.*\\)).*/\\1/'"

    set newPath to quoted form of (destFolder & "/" & (do shell script "echo " & quoted form of fileName & " | sed -e 's/\\[0/\\[/' -e 's/\\([^\\[]*\\)\\[\\([0-9]*\\)x[0-9]*\\].*/\\1(" & myText & ")\\/Run \\2\\//'") as text)

    do shell script "mkdir -p " & newPath & " ; mv " & quoted form of aFile & space & newPath
end repeat
于 2012-10-04T22:13:30.513 回答