Applescript 中的终端命令

标签 terminal applescript

我有以下终端命令,我想使用 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"

忽略大写,即:每个以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)

已编辑 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/

相关文章:

objective-c - 如何在 Mac OS X 中为特定文件类型设置默认应用程序?

Applescript 在 do shell 脚本上返回 "The command exited with a non-zero status"

arrays - Julia - 如何在 Linux 终端中很好地格式化数组

Vim 在写入文件时似乎卡住了

python - 如何在 python 中创建一个显示所有可用命令的命令?就像终端中的帮助一样

applescript - 如何使用 AppleScript 将窗口移动到某个桌面?

shell - applescript 'Can’ t 将当前应用程序转换为字符串类型'

ruby-on-rails - Gemfile 语法错误 : <<<<<<< HEAD when trying to start localhost

python - 从 Python 调用命令行实用程序

macos - 如何以编程方式设置 Mac OS X 10.6 终端选项卡标题?