我有一个Automator服务,用于上传图片和将URL返回到文本编辑器。 它运作得很好,但我想改进一些事情:
当工作流程启动时,Automator将获取文件夹中的所有图像并将其上传到FTP服务器。 然后它返回如下结果:
("http://url/0530/pic01/pic-01.jpg",
"http://url/0530/pic01/pic-02.jpg",
"http://url/0530/pic01/pic-03.jpg",
"http://url/0530/pic01/pic-04.jpg",
"http://url/0530/pic02/pic-01.jpg",
"http://url/0530/pic02/pic-02.jpg",
"http://url/0530/pic02/pic-03.jpg",
"http://url/0530/pic02/pic-04.jpg",
"http://url/0530/pic03/pic-01.jpg",
"http://url/0530/pic03/pic-02.jpg",
"http://url/0530/pic03/pic-03.jpg",
"http://url/0530/pic03/pic-04.jpg")
工作流程获取这些字符串并将它们放入新的文本文档中 最后,这是文本编辑器中的结果:
http://url/0530/pic01/pic-01.jpg
http://url/0530/pic01/pic-02.jpg
http://url/0530/pic01/pic-03.jpg
http://url/0530/pic01/pic-04.jpg
http://url/0530/pic02/pic-01.jpg
http://url/0530/pic02/pic-02.jpg
http://url/0530/pic02/pic-03.jpg
http://url/0530/pic02/pic-04.jpg
http://url/0530/pic03/pic-01.jpg
http://url/0530/pic03/pic-02.jpg
http://url/0530/pic03/pic-03.jpg
http://url/0530/pic03/pic-04.jpg
但我希望它是:
http://url/0530/pic01/pic-01.jpg
http://url/0530/pic01/pic-02.jpg
http://url/0530/pic01/pic-03.jpg
http://url/0530/pic01/pic-04.jpg
http://url/0530/pic02/pic-01.jpg
http://url/0530/pic02/pic-02.jpg
http://url/0530/pic02/pic-03.jpg
http://url/0530/pic02/pic-04.jpg
http://url/0530/pic03/pic-01.jpg
http://url/0530/pic03/pic-02.jpg
http://url/0530/pic03/pic-03.jpg
http://url/0530/pic03/pic-04.jpg
我想我可以在Automator中使用AppleScript在结果发送到文本编辑器之前执行此操作。 你有任何编码建议吗?
=============================================== ====
非常感谢@ user309603和@ mklement0 !!
我选择纯Applecript方法。 这是我在automator中的代码,它在工作流程中运行applescript:
on run {input, list}
set txtG to first item of input
repeat with txtLine in items 2 thru -1 of input
if txtLine contains "-01" then set txtG to txtG & linefeed
set txtG to txtG & linefeed & txtLine
end repeat
return txtG
end run
答案 0 :(得分:2)
这是纯粹的AppleScript解决方案。 我更喜欢mklement0的awk解决方案。如果您有长URL列表,速度会快得多。
set txt to "http://url/0530/pic01/pic-01.jpg
http://url/0530/pic01/pic-02.jpg
http://url/0530/pic01/pic-03.jpg
http://url/0530/pic02/pic-01.jpg
http://url/0530/pic03/pic-01.jpg
http://url/0530/pic03/pic-02.jpg
http://url/0530/pic03/pic-03.jpg
http://url/0530/pic03/pic-04.jpg
http://url/0530/pic04/pic-01.jpg"
set txtG to first paragraph of txt
repeat with txtLine in paragraphs 2 thru -1 of txt
if txtLine contains "-01" then set txtG to txtG & linefeed
set txtG to txtG & linefeed & txtLine
end repeat
return txtG
答案 1 :(得分:1)
以下AppleScript代码段演示了原则上可以使用的方法 - 我不确定具体情况:
如果您必须处理原始结果:
# Sample input text.
set txt to "(\"http://url/0530/pic01/pic-01.jpg\",
\"http://url/0530/pic01/pic-02.jpg\",
\"http://url/0530/pic01/pic-03.jpg\",
\"http://url/0530/pic01/pic-04.jpg\",
\"http://url/0530/pic02/pic-01.jpg\",
\"http://url/0530/pic02/pic-02.jpg\",
\"http://url/0530/pic02/pic-03.jpg\",
\"http://url/0530/pic02/pic-04.jpg\",
\"http://url/0530/pic03/pic-01.jpg\",
\"http://url/0530/pic03/pic-02.jpg\",
\"http://url/0530/pic03/pic-03.jpg\",
\"http://url/0530/pic03/pic-04.jpg\")"
# Group (break into blocks) by files containing "-01."
set txtGrouped to do shell script ¬
"printf %s " & quoted form of txt & " | tr -d '()\"'" & ¬
" | awk -F, 'NR>1 && /-01\\./ { print \"\" } { print $1 }'" ¬
without altering line endings
如果已经有清理一组仅限URL的行:
# Sample input text.
set txt to "http://url/0530/pic01/pic-01.jpg
http://url/0530/pic01/pic-02.jpg
http://url/0530/pic01/pic-03.jpg
http://url/0530/pic01/pic-04.jpg
http://url/0530/pic02/pic-01.jpg
http://url/0530/pic02/pic-02.jpg
http://url/0530/pic02/pic-03.jpg
http://url/0530/pic02/pic-04.jpg
http://url/0530/pic03/pic-01.jpg
http://url/0530/pic03/pic-02.jpg
http://url/0530/pic03/pic-03.jpg
http://url/0530/pic03/pic-04.jpg"
# Group (break into blocks) by files containing "-01."
set txtGrouped to do shell script ¬
"printf %s " & quoted form of txt & ¬
" | awk 'NR>1 && /-01\\./ { print \"\" } { print }'" ¬
without altering line endings
使用do shell script
让awk
执行所需的分组。
注意:
without altering line endings
需要阻止AppleScript使用Mac式\n
行结尾替换输出中的\r
个字符。结果将有一个尾随\n
字符,即使输入没有。要解决此问题,请使用:
set txtGrouped to text 1 thru ((length of txtGrouped) - 1) of txtGrouped
答案 2 :(得分:0)
如果目标是始终将列表项划分为按四个分组的段落,则可以在自动化操作中添加一个AppleScript:
set l to input as list
set r to ""
repeat with n from 1 to (count l) by 4
set r to r & item n of l & return & item (n + 1) of l & return & item (n + 2) of l & return & item (n + 3) of l & return & return
end repeat
return r
澄清你是否需要将一张图片的所有版本组合在一起,但它不会一直是4,并且脚本需要检查文件名以确定。
答案 3 :(得分:0)
这是一个解决方案:此脚本检查每个文件的父文件夹的名称,当文件夹名称不相同时,它会添加一个空行。
将Run Shell Script
操作附加到您的工作流程中,选择" / bin / bash " shell并选择" 到stdin "
将此脚本放入Run Shell Script
操作:
awk -F/ '{f=$(NF - 1); if (NR==1) {fName=f} else if (f != fName) {print ""; fName=f} print}' < "/dev/stdin"