终端命令进入Applescript

时间:2016-01-31 23:37:51

标签: terminal applescript

我有以下终端命令,我想使用applescript自动执行。我也希望有一个命令将cd目录放到我想先应用命令的文件夹中。 cd到保存.pdf文件的文件夹之后。此代码查看文件名的第一个字母,并根据该文件将文件排序到与文件的第一个字母对应的字母文件夹中。

for x in `ls -1 | sed -e 's/^\(.\).*/\1/' | sort -u`; do
mv -i ${x}?* $x
done

我该怎么做呢?我是新手。任何见解将不胜感激。我愿意学习Applescript,但是,我不知道从哪里开始实现它。

由于

1 个答案:

答案 0 :(得分:1)

试试这个:

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"

忽略大写,即:以aA开头的每个文件都会移至文件夹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)

EDITRED 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"