有没有办法告诉Applescript找到文件名与此类条件匹配的文件:
set foundFile to file whose name starts with
[字符0-9] ,然后是 [字符0-9] ,然后是 下划线
答案 0 :(得分:1)
我想不出用Finder做条款声明的方法。但是grep具有正则表达式功能,所以试试这个......
set theFolder to choose folder
set foundFileNames to paragraphs of (do shell script "ls " & quoted form of POSIX path of theFolder & " | grep '^[0-9][0-9]_'")
set firstFoundFile to (theFolder as text) & item 1 of foundFileNames
答案 1 :(得分:1)
这是一个使用所有Applescript并且没有shell脚本的处理程序。
-- get your folder however and feed the folder to the handler getMatchingFiles(*your folder*)
on getMatchingFiles(theFolder)
tell application "Finder"
set theFiles to every file of theFolder
set allDigits to "0123456789"
set matchingFiles to {}
repeat with aFile in theFiles
set fileName to the name of aFile
if ((the first character of fileName is in allDigits) and (the second character of fileName is in allDigits) and (the third character of fileName is "_")) then copy aFile to the end of matchingFiles
end repeat
end tell
return matchingFiles
end getMatchingFiles
答案 2 :(得分:0)
regulus6633's answer很有希望,但没有必要诉诸外部实用程序ls
和grep
;使用模式[0-9][0-9]_*
,shell自己的路径名扩展(globbing)就足够了:
# Pick a folder.
set targetFolder to choose folder
# Get matching files.
# `shopt -s nullglob` instructs the shell to return an empty string, if no files match.
set foundFiles to paragraphs of (do shell script "shopt -s nullglob; printf '%s
' " & quoted form of POSIX path of targetFolder & "[0-9][0-9]_*")
# Extract the 1st matching file.
set foundFile to item 1 of foundFiles