applescript 中的文件名增量
Filename increment in applescript
我正在使用轻量级自动化进程 (applescript) 来检测像 "file_v01"、"document_v03" 这样的文件名,这些文件名都以“_vXX”结尾并递增(到 "file_v02" 并且有令人惊讶的是,这方面的内容很少。
我曾尝试简单地检测并删除文件名中的最后两个字符,但无济于事,如果有任何想法就好了。不需要任何花哨的东西,只是 _v02 变成 _v03。
任何帮助都会很棒!!
这是您想要的脚本。
您需要先设置文件名和计数器之间的分隔符类型。然后您需要定义您的计数器应该有多少位。
我假设您的文件名中的计数器后面可能有一些文本(例如文件扩展名!):
set Separator to "_v" -- separator between name and counter
set DigitsCount to 2 -- assuming only 2 digits for counter like 01, 02, ...99
set myName to "testxxx_v07_Copy.txt" -- example for the test
set PosSep to offset of Separator in myName
if PosSep is 0 then
-- no separator, then no counter..do what's required !
else
set BaseName to text 1 thru (PosSep - 1) of myName
set myCount to (text from (PosSep + (length of Separator)) to (PosSep + (length of Separator) + DigitsCount - 1) of myName) as integer
set PostName to text from (PosSep + (length of Separator) + DigitsCount) to -1 of myName
end if
-- convert the counter to +1 in string with '0' before
set newCount to "00000" & (myCount + 1) as string
set newCount to text from ((length of newCount) - DigitsCount + 1) to -1 of newCount
set NextName to BaseName & Separator & newCount & PostName
-- BaseName now contains the basic name before the counter
-- newCount contains the new counter value (+1) as string formatted with x digits
-- PostName contains the current value of myName after the counter
-- NextName contains your new file name with new counter and extension if any
这是 Automator
的 AppleScript,它递增文件名
的最后两个字符
on run {input, parameters}
tell application "System Events"
repeat with thisFile in input
set {tName, nameExt} to {name, name extension} of thisFile
if nameExt is not "" then set nameExt to "." & nameExt
set newName to my incrementNumber(tName, nameExt)
if newName is not "" then -- the last two characters is a number
set name of thisFile to newName
end if
end repeat
end tell
return input
end run
on incrementNumber(n, e) -- name and name extension
set tBase to text 1 thru ((length of n) - (length of e)) of n -- get the name without extension
try
set thisNumber to (text -2 thru -1 of tBase) + 1 -- get the last two characters and increment
if thisNumber < 10 then set thisNumber to "0" & thisNumber -- zero padding
set tBase to text 1 thru -3 of tBase -- remove the last two characters
return tBase & thisNumber & e -- the updated name (the basename without the last two characters + the number + the extension)
end try
return "" -- the last two characters is not a number
end incrementNumber
我正在使用轻量级自动化进程 (applescript) 来检测像 "file_v01"、"document_v03" 这样的文件名,这些文件名都以“_vXX”结尾并递增(到 "file_v02" 并且有令人惊讶的是,这方面的内容很少。
我曾尝试简单地检测并删除文件名中的最后两个字符,但无济于事,如果有任何想法就好了。不需要任何花哨的东西,只是 _v02 变成 _v03。
任何帮助都会很棒!!
这是您想要的脚本。
您需要先设置文件名和计数器之间的分隔符类型。然后您需要定义您的计数器应该有多少位。
我假设您的文件名中的计数器后面可能有一些文本(例如文件扩展名!):
set Separator to "_v" -- separator between name and counter
set DigitsCount to 2 -- assuming only 2 digits for counter like 01, 02, ...99
set myName to "testxxx_v07_Copy.txt" -- example for the test
set PosSep to offset of Separator in myName
if PosSep is 0 then
-- no separator, then no counter..do what's required !
else
set BaseName to text 1 thru (PosSep - 1) of myName
set myCount to (text from (PosSep + (length of Separator)) to (PosSep + (length of Separator) + DigitsCount - 1) of myName) as integer
set PostName to text from (PosSep + (length of Separator) + DigitsCount) to -1 of myName
end if
-- convert the counter to +1 in string with '0' before
set newCount to "00000" & (myCount + 1) as string
set newCount to text from ((length of newCount) - DigitsCount + 1) to -1 of newCount
set NextName to BaseName & Separator & newCount & PostName
-- BaseName now contains the basic name before the counter
-- newCount contains the new counter value (+1) as string formatted with x digits
-- PostName contains the current value of myName after the counter
-- NextName contains your new file name with new counter and extension if any
这是 Automator
的 AppleScript,它递增文件名
on run {input, parameters}
tell application "System Events"
repeat with thisFile in input
set {tName, nameExt} to {name, name extension} of thisFile
if nameExt is not "" then set nameExt to "." & nameExt
set newName to my incrementNumber(tName, nameExt)
if newName is not "" then -- the last two characters is a number
set name of thisFile to newName
end if
end repeat
end tell
return input
end run
on incrementNumber(n, e) -- name and name extension
set tBase to text 1 thru ((length of n) - (length of e)) of n -- get the name without extension
try
set thisNumber to (text -2 thru -1 of tBase) + 1 -- get the last two characters and increment
if thisNumber < 10 then set thisNumber to "0" & thisNumber -- zero padding
set tBase to text 1 thru -3 of tBase -- remove the last two characters
return tBase & thisNumber & e -- the updated name (the basename without the last two characters + the number + the extension)
end try
return "" -- the last two characters is not a number
end incrementNumber