我有以下终端命令,我想使用 applescript 自动执行。我还想有一个命令将目录 cd 到我想首先应用命令的文件夹。在 cd 到包含 .pdf 文件的文件夹之后。此代码查看文件名的第一个字母,并根据该字母将文件分类到与文件的第一个字母对应的字母文件夹中。
for x in `ls -1 | sed -e 's/^\(.\).*/\1/' | sort -u`; do
mv -i ${x}?* $x
done
我该怎么做呢?我是这个的新手。任何见解将不胜感激。我愿意学习 Applescript,但是,我不确定从哪里开始实现它。
谢谢
最佳答案
试试这个:
choose folder with prompt "Select the original folder" default location (path to documents folder)
set folderPath to contents of result
set folderPath to POSIX path of folderPath
do shell script "for x in `ls -1 " & folderPath & " | sed -e 's/^\\(.\\).*/\\1/' | sort -u`; do destination=$(echo $x | tr '[:lower:]' '[:upper:]'); mkdir " & folderPath & "$destination; mv -i " & folderPath & "${x}?* " & folderPath & "$destination/; done"
忽略大写,即:每个以a
和A
开头的文件都移动到文件夹A/
编辑 1: 尝试创建目标文件夹,但如果它已经存在,则静默失败并将文件移动到它。
编辑 2: 默认打开 Dropbox 文件夹:
set dropboxFolder to (POSIX path of (path to home folder) & "Dropbox")
choose folder with prompt "Select the original folder" default location (POSIX file dropboxFolder as alias)
已编辑 3: 最终脚本:
set dropboxFolder to (POSIX path of (path to home folder) & "Dropbox")
choose folder with prompt "Select the original folder" default location (POSIX file dropboxFolder as alias)
set folderPath to contents of result
set folderPath to POSIX path of folderPath
do shell script "for x in `ls -1 " & folderPath & " | sed -e 's/^\\(.\\).*/\\1/' | sort -u`; do destination=$(echo $x | tr '[:lower:]' '[:upper:]'); mkdir " & folderPath & "$destination; mv -i " & folderPath & "${x}?* " & folderPath & "$destination/; done"
关于Applescript 中的终端命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35120896/