我需要列出AppleScript特定文件夹中的所有唯一文件名。
唯一的问题是文件名全部采用这种格式-L58478_A_2803
在第一个下划线之后,我需要能够忽略所有内容。
答案 0 :(得分:0)
您必须告诉我这是否是您所追求的。给定文件夹"~/Example"
中的以下文件:
L58478_B_2803.txt
L58666_B_2803.txt
L58478_A_2804.txt
L58666_B_2804.txt
L58478_A_2803.txt
L58666_A_2803.txt
该脚本标识文件名的开头部分,并为这些文件组选择唯一的标识符。在这种情况下,结果将是{"L58478", "L58666"}
。
use application "System Events"
property text item delimiters : "_"
script filenames
property list : name of files in folder "~/Example" whose visible = true
end script
tell (a reference to the list of filenames)
repeat with f in it
set f's contents to text item 1 of f
end repeat
repeat with i from 1 to (its length) - 1
set [x, x_] to its [item i, items (i + 1) thru -1]
if x is in x_ then set its item i to null
end repeat
strings
end tell
答案 1 :(得分:0)
我们假设文件夹中的文件是.. {“ L58666_B_2803.txt”,“ L58666_B_2804.txt”,“ L58666_B_2805.txt”,“ L58666_B_2806.txt”,“ L58667_B_2807.txt”,“ L58668_B_2806 .txt”}
以下脚本
tell application "Finder"
activate
set fileNames to name of files of (choose folder)
end tell
set partialNames to {}
repeat with i from 1 to count of fileNames
set thisItem to item i of fileNames
set theOffset to offset of "_" in thisItem
set newName to text 1 thru (theOffset - 1) of thisItem
if partialNames does not contain newName then
set end of partialNames to newName
end if
end repeat
会返回... {“ L58666”,“ L58667”,“ L58668”}如果您不想包括第一个“ _”
但是,如果您要包含第一个“ _”
更改
set newName to text 1 thru (theOffset - 1) of thisItem
收件人
set newName to text 1 thru theOffset of thisItem