Applescript 中的终端命令
Terminal Command into Applescript
我想使用 applescript 自动执行以下终端命令。我还想有一个命令将目录 cd 到我想首先应用命令的文件夹。在 cd 到包含 .pdf 文件的文件夹之后。此代码查看文件名的第一个字母,并根据该字母将文件分类到与文件的第一个字母对应的字母文件夹中。
for x in `ls -1 | sed -e 's/^\(.\).*//' | 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 自动执行以下终端命令。我还想有一个命令将目录 cd 到我想首先应用命令的文件夹。在 cd 到包含 .pdf 文件的文件夹之后。此代码查看文件名的第一个字母,并根据该字母将文件分类到与文件的第一个字母对应的字母文件夹中。
for x in `ls -1 | sed -e 's/^\(.\).*//' | 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"