使用AppleScript将文件名中的无效字符替换为破折号 我的目标是使用Apple脚本或JavaScript在AutoScript中创建服务,它替换了选定文件名()\ \ \(*)*> + < < /代码>和空格的空格( ->代码>),并使文件名小写。 use AppleScript version "2.4" -- Yosemite (10.10) or later use scripting additions use framework "Foundation" set fileName to "New(Foo)*aBcd<B|r.ext" set nsFileName to current application's NSString's stringWithString:fileName set nsLowerCaseFileName to nsFileName's lowercaseString() set trimmedFileName to (nsLowerCaseFileName's stringByReplacingOccurrencesOfString:"[()[\\/:\"*?<>|]+_]" withString:"-" options:(current application's NSRegularExpressionSearch) range:{location:0, |length|:nsLowerCaseFileName's |length|()}) as text display dialog trimmedFileName 使用AppleScript版本“2.4”--约塞米蒂(10.10)或更高版本 使用脚本添加 使用框架“基础” 将文件名设置为“新建(FoO)*ABCD),在正则表达式的帮助下,将基础框架连接到Apple SpRepButtoJC.非常容易。 use AppleScript version "2.4" -- Yosemite (10.10) or later use scripting additions use framework "Foundation" set fileName to "New(Foo)*aBcd<B|r.ext" set nsFileName to current application's NSString's stringWithString:fileName set nsLowerCaseFileName to nsFileName's lowercaseString() set trimmedFileName to (nsLowerCaseFileName's stringByReplacingOccurrencesOfString:"[()[\\/:\"*?<>|]+_]" withString:"-" options:(current application's NSRegularExpressionSearch) range:{location:0, |length|:nsLowerCaseFileName's |length|()}) as text display dialog trimmedFileName 使用AppleScript版本“2.4”--约塞米蒂(10.10)或更高版本 使用脚本添加 使用框架“基础” 将fileName设置为“New(Foo)*xiao xiao
替换文件/文件夹名称中不允许的字符可以通过在Automator服务中使用Bash Shell脚本来实现 以下步骤描述了如何实现这一点: 配置自动机使用AppleScript将文件名中的无效字符替换为破折号 我的目标是使用Apple脚本或JavaScript在AutoScript中创建服务,它替换了选定文件名()\ \ \(*)*> + < < /代码>和空格的空格( ->代码>),并使文件名小写。 use AppleScript version "2.4" -- Yosemite (10.10) or later use scripting additions use framework "Foundation" set fileName to "New(Foo)*aBcd<B|r.ext" set nsFileName to current application's NSString's stringWithString:fileName set nsLowerCaseFileName to nsFileName's lowercaseString() set trimmedFileName to (nsLowerCaseFileName's stringByReplacingOccurrencesOfString:"[()[\\/:\"*?<>|]+_]" withString:"-" options:(current application's NSRegularExpressionSearch) range:{location:0, |length|:nsLowerCaseFileName's |length|()}) as text display dialog trimmedFileName 使用AppleScript版本“2.4”--约塞米蒂(10.10)或更高版本 使用脚本添加 使用框架“基础” 将文件名设置为“新建(FoO)*ABCD),在正则表达式的帮助下,将基础框架连接到Apple SpRepButtoJC.非常容易。 use AppleScript version "2.4" -- Yosemite (10.10) or later use scripting additions use framework "Foundation" set fileName to "New(Foo)*aBcd<B|r.ext" set nsFileName to current application's NSString's stringWithString:fileName set nsLowerCaseFileName to nsFileName's lowercaseString() set trimmedFileName to (nsLowerCaseFileName's stringByReplacingOccurrencesOfString:"[()[\\/:\"*?<>|]+_]" withString:"-" options:(current application's NSRegularExpressionSearch) range:{location:0, |length|:nsLowerCaseFileName's |length|()}) as text display dialog trimmedFileName 使用AppleScript版本“2.4”--约塞米蒂(10.10)或更高版本 使用脚本添加 使用框架“基础” 将fileName设置为“New(Foo)*xiao xiao,javascript,regex,applescript,automator,Javascript,Regex,Applescript,Automator,替换文件/文件夹名称中不允许的字符可以通过在Automator服务中使用Bash Shell脚本来实现 以下步骤描述了如何实现这一点: 配置自动机 发射自动机 类型⌘N、 或者从菜单栏中选择FileNew 选择服务并单击选择 在画布区域的顶部,按如下方式配置其设置: 选择左侧面板/列顶部的Library: 在搜索字段中键入:Get Select Finder items并将Get Select Finder items操作拖动到画布区域 在搜索字段中键入:运行Shell并将运行Shell脚本
File
New
服务
并单击选择
Library
:
- 在搜索字段中键入:Get Select Finder items并将
操作拖动到画布区域Get Select Finder items
- 在搜索字段中键入:运行Shell并将
操作拖动到画布区域运行Shell脚本
运行Shell脚本
操作的顶部:
运行shell脚本
操作的主区域:
!/usr/bin/env bash
#基本名称中不允许使用以下字符:
#
#-左方括号:[
#-右方括号:]
#-左括号:(
#-反向索利多金币:\
#-冒号::
#-引号“
#-单引号'
#-星号*
#-问号?
#-小于符号<
#-大于号>
#-垂直线|
#-加号+
#-空格字符
#-下划线_
#
#1.Sed用于字符替换,因此列出了字符
#在括号中,必须根据需要转义表达式[…]。
#2.默认情况下,basename中的任何正斜杠“/”都将替换为
#冒号“:”在shell级别-因此没有必要搜索它们。
#
declare-r unpermissible_CHARS=“[[][()\\:\”'*?|+\”
声明-r替换_字符串=“-”
#获取“Finder”中每个选定项的POSIX路径。输入必须
#通过前面的“获取选定查找项”操作传递到此脚本
#在Automator服务工作流中。
声明所选项目=(“$@”)
declare-a排序的路径
declare-a编号的路径
#在POSIX路径深度级别之前加上前缀,以帮助排序。
对于((i=0;i<“${{所选项目[@]}”;i++),请执行以下操作
编号的_路径+=(“$(echo“${selected_items[$i]}”)|\
awk-F“/”{print NF-1,$0}'))
完成
#按深度的降序对数组中的每个POSIX路径进行排序。
#这确保较深路径在较浅路径之前重命名。
IFS=$'\n'read-rd'-排序的路径=
为255个字符。当然,只有将替换字符串(在Bash/Shell脚本中)的值更改为多个字符,而不仅仅是一个连字符-
时,才会出现这种情况替换文件/文件夹名称中不允许的字符可以通过在Automator服务中使用Bash Shell脚本来实现 以下步骤描述了如何实现这一点: 配置自动机
File
New
服务
并单击选择
Library
:
- 在搜索字段中键入:Get Select Finder items并将
操作拖动到画布区域Get Select Finder items
- 在搜索字段中键入:运行Shell并将
操作拖动到画布区域运行Shell脚本
运行Shell脚本
操作的顶部:
运行shell脚本
操作的主区域:
!/usr/bin/env bash
#基本名称中不允许使用以下字符:
#
#-左方括号:[
#-右方括号:]
#-左括号:(
#-反向索利多金币:\
#-冒号::
#-引号“
#-单引号'
#-星号*
#-问号?
#-小于符号<
#-大于号>
#-垂直线|
#-加号+
#-空格字符
#-下划线_
#
#1.Sed用于字符替换,因此列出了字符
#在括号中,必须根据需要转义表达式[…]。
#2.默认情况下,basename中的任何正斜杠“/”都将替换为
#冒号“:”在shell级别-因此没有必要搜索它们。
#
declare-r unpermissible_CHARS=“[[][()\\:\”'*?|+\”
声明-r替换_字符串=“-”
#获取“Finder”中每个选定项的POSIX路径。输入必须
#通过前面的“获取选定查找项”操作传递到此脚本
#在Automator服务工作流中。
声明所选项目=(“$@”)
declare-a排序的路径
declare-a编号的路径
#前缀
property alphabet : "abcdefghijklmnopqrstuvwxyz-ABCDEFGHIJKLMNOPQRSTUVWXYZ"
on run {input, parameters}
tell application "Finder" to repeat with f in input
set the name of f to (rename from f)
end repeat
end run
to rename from ...
.
.
end rename
to makeLowercase(str as text)
.
.
end makeLowercase