在 applescript 中重命名一堆快速时间
Renameing a bunch of quicktimes in applescript
我的文件夹中有一堆文件,名称如下:
123456_this_is_a_fun_test_v01.mov
685954_this_more_is_a_fun_test_v01_clean.mov
它们都有一个 6 位数的开头和某个地方的版本号。我需要做的是删除版本号并将前 6 位数字移动到末尾,在扩展名称之前,如下所示:
this_is_a_fun_test_123456.mov
this_more_is_a_fun_test_clean_685954.mov
一直在 automator 和一些简单的 Applescripting 中尝试一些东西,但没有任何运气。我的脚本编写水平不高,只有 "Hobby" 水平。有人有什么建议吗?
tell application "Finder"
--grab the selected files and put them into a variable
set F to selection
end tell
-- This will be the character used to rejoin pieces
-- of the filename after breaking them apart
set the text item delimiters to "_"
repeat with g in F -- Loop through the file list
-- Get the filename
tell application "System Events" to get name of g
-- Eliminate the version number component of the filename
set r to do shell script ¬
"echo " & quoted form of result & ¬
" | egrep -o '[^_\.]+'" & ¬
" | egrep -iv 'v\d+'"
-- Assemble the other components in the new order
get {paragraphs 2 thru -2, paragraph 1} of r
get (result as text) & ".mov"
-- Rename the file to the new name
tell application "System Events" to set name of g to result
end repeat
我已经向这个 AppleScript 添加了注释,解释了脚本每个部分的作用。目前,它被设计为从 Script Editor 内部 运行,但它可以很容易地调整为 Automator 工作流程的一部分.
将脚本复制并粘贴到 脚本编辑器。将 /Path/To/Folder 替换为 .mov 文件所在文件夹的路径(保留引号)。使用完整路径,即 /Users/CK/Movies 而不是 ~/Movies。按Cmd+K编译脚本,检查pre-运行语法错误等。当字体改变并且脚本看起来很漂亮时,然后点击 Cmd+R 来执行它。
tell application "System Events" to get the files in folder ¬
"/Path/To/Folder" whose name extension is "mov"
set F to the result -- The list of .mov files that need renaming
-- This will be the character used to rejoin pieces
-- of the filename after breaking them apart
set the text item delimiters to "_"
repeat with g in F -- Loop through the file list
-- Get the filename
tell application "System Events" to get name of g
-- Eliminate the version number component of the filename
set r to do shell script ¬
"echo " & quoted form of result & ¬
" | egrep -o '[^_\.]+'" & ¬
" | egrep -iv 'v\d+'"
-- Assemble the other components in the new order
get {paragraphs 2 thru -2, paragraph 1} of r
get (result as text) & ".mov"
-- Rename the file to the new name
tell application "System Events" to set name of g to result
end repeat
通过一些 google Fu 和大量查看您的代码,我让它可以处理选定的文件。感谢帮助
tell application "Finder"
set F to selection
end tell
set the text item delimiters to "_"
repeat with g in F -- Loop through the file list
-- Get the filename
tell application "Finder" to get name of g
-- Eliminate the version number component of the filename
set r to do shell script ¬
"echo " & quoted form of result & ¬
" | egrep -o '[^_\.]+'" & ¬
" | egrep -iv 'v\d+'"
-- Assemble the other components in the new order
get {paragraphs 2 thru -2, paragraph 1} of r
get (result as text) & ".mov"
-- Rename the file to the new name
tell application "Finder" to set name of g to result
end repeat
tell application "System Events"
activate
display dialog "Selected files have been renamed!"
end tell
我的文件夹中有一堆文件,名称如下:
123456_this_is_a_fun_test_v01.mov
685954_this_more_is_a_fun_test_v01_clean.mov
它们都有一个 6 位数的开头和某个地方的版本号。我需要做的是删除版本号并将前 6 位数字移动到末尾,在扩展名称之前,如下所示:
this_is_a_fun_test_123456.mov
this_more_is_a_fun_test_clean_685954.mov
一直在 automator 和一些简单的 Applescripting 中尝试一些东西,但没有任何运气。我的脚本编写水平不高,只有 "Hobby" 水平。有人有什么建议吗?
tell application "Finder"
--grab the selected files and put them into a variable
set F to selection
end tell
-- This will be the character used to rejoin pieces
-- of the filename after breaking them apart
set the text item delimiters to "_"
repeat with g in F -- Loop through the file list
-- Get the filename
tell application "System Events" to get name of g
-- Eliminate the version number component of the filename
set r to do shell script ¬
"echo " & quoted form of result & ¬
" | egrep -o '[^_\.]+'" & ¬
" | egrep -iv 'v\d+'"
-- Assemble the other components in the new order
get {paragraphs 2 thru -2, paragraph 1} of r
get (result as text) & ".mov"
-- Rename the file to the new name
tell application "System Events" to set name of g to result
end repeat
我已经向这个 AppleScript 添加了注释,解释了脚本每个部分的作用。目前,它被设计为从 Script Editor 内部 运行,但它可以很容易地调整为 Automator 工作流程的一部分.
将脚本复制并粘贴到 脚本编辑器。将 /Path/To/Folder 替换为 .mov 文件所在文件夹的路径(保留引号)。使用完整路径,即 /Users/CK/Movies 而不是 ~/Movies。按Cmd+K编译脚本,检查pre-运行语法错误等。当字体改变并且脚本看起来很漂亮时,然后点击 Cmd+R 来执行它。
tell application "System Events" to get the files in folder ¬
"/Path/To/Folder" whose name extension is "mov"
set F to the result -- The list of .mov files that need renaming
-- This will be the character used to rejoin pieces
-- of the filename after breaking them apart
set the text item delimiters to "_"
repeat with g in F -- Loop through the file list
-- Get the filename
tell application "System Events" to get name of g
-- Eliminate the version number component of the filename
set r to do shell script ¬
"echo " & quoted form of result & ¬
" | egrep -o '[^_\.]+'" & ¬
" | egrep -iv 'v\d+'"
-- Assemble the other components in the new order
get {paragraphs 2 thru -2, paragraph 1} of r
get (result as text) & ".mov"
-- Rename the file to the new name
tell application "System Events" to set name of g to result
end repeat
通过一些 google Fu 和大量查看您的代码,我让它可以处理选定的文件。感谢帮助
tell application "Finder"
set F to selection
end tell
set the text item delimiters to "_"
repeat with g in F -- Loop through the file list
-- Get the filename
tell application "Finder" to get name of g
-- Eliminate the version number component of the filename
set r to do shell script ¬
"echo " & quoted form of result & ¬
" | egrep -o '[^_\.]+'" & ¬
" | egrep -iv 'v\d+'"
-- Assemble the other components in the new order
get {paragraphs 2 thru -2, paragraph 1} of r
get (result as text) & ".mov"
-- Rename the file to the new name
tell application "Finder" to set name of g to result
end repeat
tell application "System Events"
activate
display dialog "Selected files have been renamed!"
end tell