我是AppleScript的新手,但我认为它可以自动解决我有时会遇到的轻微烦恼。
让我说我写道:"我喜欢披萨,把培根放在上面!" 但我决定将其分成两句话:"我喜欢披萨。把培根放进去!"
我希望能够选择字符串",p" 使用键盘快捷键,让脚本删除任何标点符号,添加句点,并大写第一个字母。
我想出了如何设置系统偏好设置>键盘运行自动播放器服务,运行AppleScript,但我不能谷歌足够的信息从头开始创建脚本。
任何帮助或方向都会很棒!
答案 0 :(得分:0)
如果你:
selected text
any application
Output replaces selected text
Run AppleScript
操作,代码如下它应该做你想要的。
on run {input, parameters}
# Initialize return variable.
set res to ""
# Split the selection into clauses by ", ".
set {orgTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {", "}} #'
set clauses to text items of item 1 of input
set AppleScript's text item delimiters to orgTIDs #'
# Join the clauses with ". ", capitalizing the first letter of all clauses but the first.
repeat with clause in clauses
if res = "" then
set res to clause
else
set res to res & ". " & my capitalizeFirstChar(clause)
end if
end repeat
# Return the result
return res
end run
# Returns the specified string with its first character capitalized.
on capitalizeFirstChar(s)
set {orgTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {""}} #'
set res to my toUpper((text item 1 of s) as text) & text items 2 thru -1 of s as text
set AppleScript's text item delimiters to orgTIDs #'
return res
end capitalizeFirstChar
# Converts the specified string (as a whole) to uppercase.
on toUpper(s)
tell AppleScript to return do shell script "export LANG='" & user locale of (system info) & ".UTF-8'; tr [:lower:] [:upper:] <<< " & quoted form of s
end toUpper
答案 1 :(得分:0)
替代:由于Automator也支持其他解释器,因此不需要使用AppleScript。
以下是使用Run Shell Script
操作将bash
与awk
结合使用的替代解决方案,这样可以减少代码:
selected text
any application
Output replaces selected text
Run Shell Script
操作,代码如下awk -F ', ' '{
printf $1 # print the first "field"; lines without ", " only have 1 field
for (i=2; i<=NF; ++i) { # all other fields, i.e., all ", "-separated clauses.
# Join the remaining clauses with ". " with their 1st char. capitalized.
printf ". " toupper(substr($i,1,1)) substr($i,2)
}
printf "\n" # terminate the output with a newline
}'
警告:令人难以置信的是,awk
(从OS X 10.9.2开始)不能正确处理外来字符 - 通过未经修改的作品传递它们,但是toupper()
和{ {1}}不认为它们是字母。因此,此解决方案将无法正确用于外部字符的输入。必须是大写的;例如:tolower()
不会将"I like pizza, ṕut bacon on it!"
转换为ṕ
。