macos 如何从终端启动 applescript.scpt 文件并传递术语/变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8424930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 08:41:00  来源:igfitidea点击:

How can I launch an applescript.scpt file from the terminal and pass terms / variables?

macoscommand-lineterminalapplescript

提问by cwd

I have an applescript that does something along these lines:

我有一个applescript,可以按照以下方式做一些事情:

using terms from application "Quicksilver"
    on open theseitems
        repeat with aitem in theseitems
            display dialog aitem as text
        end repeat
    end open
end using terms from

But what I'd like to do is be able to start running a particular applescript.scptfile via the Terminal and pass in a variable, like a path to a file.

但是我想做的是能够applescript.scpt通过终端开始运行特定文件并传入一个变量,比如文件的路径。

osascript ~/applescript.scpt /path/to/my/file.txt

and then have the Applescript run with access to that parameter. In this case it would (hopefully) display a dialog with that path, /path/to/my/file.txt

然后让 Applescript 运行并访问该参数。在这种情况下,它会(希望)显示一个带有该路径的对话框,/path/to/my/file.txt

I know I could achieve that by doing something like

我知道我可以通过做类似的事情来实现这一点

osascript -e "display dialog "~/path/to/file.txt"

But the point is not to display a dialog with Applescript, rather it's more about knowing if I would be able to pass a variable in to a script file.

但重点不是用 Applescript 显示对话框,而是更多地了解我是否能够将变量传递给脚本文件。

回答by Kassym Dorsel

In the script you pass in the arguments with the on runlike this :

在脚本中,您on run像这样传入参数:

on run arg
  --do whatever you want with arg
end run

If more than one argument is specified the argvariable is a list. enter image description here

如果指定了多个参数,则arg变量是一个列表。 在此处输入图片说明

enter image description here

在此处输入图片说明