首页 > 解决方案 > 使用苹果脚本复制然后执行终端命令

问题描述

这是我第一次创建苹果脚本。

我正在尝试在我的 Mac 上创建一个脚本,以便它复制一个名为“1-Client Template”的文件夹,然后我希望终端问我要将其重命名为什么,然后最后运行一些终端命令。

这是我希望它运行的终端命令

cd ~/Documents/Clients/TEMPLATE/Projects/Website/5-Website && git init && git add . && git config --global user.email 'name@email.com' && git config --global user.name 'Full Name' && git commit -m 'initial commit, added all website files' && git status

我知道要求输入部分肯定是错误的,但我无法弄清楚这一点。到目前为止,这是我的脚本...

tell application "Finder"
    if exists Finder window 1 then
        set ~/documents/Clients/1-Client\ Template to ~/documents/Clients/ask for input
    else
        return
    end if
end tell
tell application "Terminal"
    do shell script "cd ~/Documents/Clients/TEMPLATE/Projects/Website/5-Website && git init && git add . && git config --global user.email 'name@email.com' && git config --global user.name 'Full Name' && git commit -m 'initial commit, added all website files' && git status
"
    activate
end tell

我在查看其他在线示例时制作了这段代码,但我不太明白。

我收到以下错误

Expected expression but found unknown token.

标签: macosterminalapplescript

解决方案


do shell script不需要tell块,甚至不需要终端运行,因为它直接调用shell。

如果您需要询问用户输入(即新文件名),并且您正在使用 AppleScript,有一些方法可以提示输入字符串,例如(text returned of (display dialog "foo" default answer "bar")),或者如果您需要选择一个文件夹,您可以使用(quoted form of (POSIX path of (choose folder))),类似地您可能会使用(quoted form of (POSIX path of (choose file)))这两个文件,这将为您提供从 Mac GUI 派生的对 shell 友好的字符串路径。

要进行实际的重命名,您要么必须使用 Finder(这比您想象的要难,因为它具有完全不同的文件引用方式)do shell script使用 (eg)精心构造的调用mv,这可能更好,因为您将使用 POSIX 路径而不是 Finder 文件引用来执行此操作,并且很容易执行 git 内容或之后的任何操作。


推荐阅读