讓我們打破你的問題分解成步驟。
首先,您要從取景器中檢索文件。現在,讓我們假設你選擇了一個文件夾,並且希望將該腳本應用於其封裝的文件。
tell application "Finder"
set theFolder to the selection
set theFiles to every file of item 1 of theFolder
當你搶Finder的選擇你的列表,因此第1項這也讓你有機會通過擴大它,比如,選擇幾個文件夾,並使用重複循環通過它們進行迭代。
接下來,我們希望每一個文件走了過來,讓我們建立一個循環調用一個函數,並將其傳遞我們正在尋找一個字符串當前文件的文件名:
repeat with aFile in theFiles
set originalName to the name of aFile
set newName to my threeDigitPrefix(originalName)
的子程序我們稱之爲是一個非常簡單的一個,即通過除了打破了文件名字符串,並將其存儲在列表中開始:
set AppleScript's text item delimiters to " "
set splitName to (every text item of originalName) as list
然後我們會檢查文件名開始與一個數字,打破了功能如果不是。
try
first item of splitName as number
on error
return "FAILED" -- originalName does not start with a number
end try
現在我們現有的前綴分配給一個變量,並檢查它的長度,以確定我們需要多少個零添加到文件名:
set thePrefix to the first item of splitName
if the length of thePrefix is 1 then
set thePrefix to "00" & thePrefix
else if the length of thePrefix is 2 then
set thePrefix to "0" & thePrefix
end if
然後我們把前綴回包含列表我們破碎的文件名,並重組,並將其返回到調用它的循環:
set the first item of splitName to thePrefix
return splitName as string
最後,我們檢查功能並沒有失敗,該文件與ST重命名戒指,我們剛剛從功能中獲得:
if newName is not "FAILED" then
set the name of aFile to newName
end if
我們完成了。把它放在一起,你結束了這一點:
tell application "Finder"
set theFolder to the selection
set theFiles to every file of item 1 of theFolder
repeat with aFile in theFiles
set originalName to the name of aFile
set newName to my threeDigitPrefix(originalName)
if newName is not "FAILED" then
set the name of aFile to newName
end if
end repeat
end tell
on threeDigitPrefix(originalName)
set AppleScript's text item delimiters to " "
set splitName to (every text item of originalName) as list
try
first item of splitName as number
on error
return "FAILED" -- originalName does not start with a number
end try
set thePrefix to the first item of splitName
if the length of thePrefix is 1 then
set thePrefix to "00" & thePrefix
else if the length of thePrefix is 2 then
set thePrefix to "0" & thePrefix
end if
set the first item of splitName to thePrefix
return splitName as string
end threeDigitPrefix
這是我需要讓我的頭圍繞這件事的教訓。 Applescript不像我以前用過的任何其他東西,所以非常感謝你對這種真正發生的事情有這樣的瞭解。你先生......太棒了! – Nordanfors 2013-02-26 14:28:11