如何使用 applescript 提示用户输入并将该值放在终端命令中?

How to prompt for user input and place that value in a terminal command using applescript?

我正在尝试创建一个脚本,询问用户他们想要通过 AppleScript / 脚本编辑器使用终端拆分什么 .mp3。这是我目前所拥有的。

我正在尝试让输入显示在这一行中:

do script "spleeter separate -i (text returned of input).mp3 -p spleeter:2stems -o output" 

在终端中,它只是说没有找到(输入返回的文本)的匹配项。

这是我的代码:


set input to display dialog "What would you like to convert?" default answer "" with icon note buttons {"Continue", "Cancel"} default button "Continue"

tell application "Terminal"
    do script "cd Desktop/spleetr"
    do script "spleeter separate -i (text returned of input).mp3 -p spleeter:2stems -o output"
    
end tell

您在字符串中放置了一个命令,所以它没有被评估。解决方案是按所需顺序连接命令的结果和字符串部分。

使用终端时,除非另有说明,每个 do script 命令在其自己的 window/tab 中是 运行。如果您不一定需要终端 window,可以使用 do shell script 命令代替(请注意它使用默认的 shell,因此您应该使用完整路径),但要使用多个命令与任何一个你需要将各种 shell 命令组合成一个语句,否则它们将 运行 在单独的 shells.

我没有要测试的实用程序,但在下面的脚本中,我在对话框语句中获取文本,并引用 shell 脚本的结果以防它包含空格等:

set input to text returned of (display dialog "What would you like to convert?" default answer "" with icon note buttons {"Continue", "Cancel"} default button "Continue")

-- to run in a new Terminal window: --

tell application "Terminal"
   do script "cd Desktop/spleetr; spleeter separate -i " & quoted form of (input & ".mp3") & " -p spleeter:2stems -o output"
end tell

-- or if Terminal is not needed: --

do shell script "cd Desktop/spleetr; spleeter separate -i " & quoted form of (input & ".mp3") & " -p spleeter:2stems -o output"