OSX 脚本编辑器与 AppleScript

huangapple go评论72阅读模式
英文:

OSX script editor with AppleScript

问题

我希望创建一个片段,在双击时运行一些bash命令。我希望找到一个Automator的解决方案,但我不明白它是如何工作的。

所以我尝试了使用脚本编辑器的AppleScript,但我遇到了一些问题。

这里是我的代码:

on run
    activate
    do shell script "/opt/homebrew/bin/npx igir report --dat DAT --input ROM > report.txt && find . -name .* -exec rm {} \\;"
end run

我收到这个消息:sh: report.txt: 只读文件系统

然后,我尝试了这个代码:

on run
    activate
    do shell script "/opt/homebrew/bin/npx igir report --dat DAT --input ROM > /Users/me/Downloads/ROM/report.txt && find . -name .* -exec rm {} \\;"
end run

现在,我得到了这个错误:env: node: 没有这个文件或目录

注意:我想在最后一个代码中将 "me" 更改为某个变量(比如:{user})。

请问你有什么想法吗?谢谢。

在Ap片段中调用一个bash脚本。

英文:

My wish is to create a snippet which launch some bash command when I double-click. I was hoping find one solution with Automator. But I do not understand how it works.

So I tried AppleScript with the script editor, but I got some issues.

Here I have this code:

on run
	activate
	do shell script "/opt/homebrew/bin/npx igir report --dat DAT --input ROM > report.txt && find . -name .* -exec rm {} \\;"
end run

I have this message: sh: report.txt: Read-only file system

Then, I tried this code:

on run
	activate
	do shell script "/opt/homebrew/bin/npx igir report --dat DAT --input ROM > /Users/me/Downloads/ROM/report.txt && find . -name .* -exec rm {} \\;"
end run

Now, I got this error: env: node: No such file or directory

NOTE: I would like change "me" on last code by some variable (like: {user}).

Please do you have some ideas? Thanks.

Call a bash script in a Ap snippet.

答案1

得分: 1

尝试将npx的输出重定向到"report.txt"——一个相对路径——这会依赖于应用程序的工作目录,通常是/。由于这在只读系统卷上,您会收到"只读文件系统错误"。

您的第二次尝试,将输出定向到您主目录中的绝对路径,这样做更好,但假定"Downloads"文件夹中有一个名为"ROM"的文件夹,所以您可能需要使用mkdir先创建它。

如果该文件夹已经存在,那么我不确定问题是什么,但值得注意的是"do shell script"不会读取您通常的shell启动文件,因此不会具有您可能在那里做的任何PATH自定义设置——npx可能会依赖于那个。

您还问到如何替代"/Users/me":您可以使用变量${HOME},或者因为这是一个shell,只需使用~;例如~/Downloads

这些和其他问题在TN2065: do shell script in AppleScript中有解答。

英文:

By trying to redirect the output of npx to "report.txt" — a relative path — you are relying on the working directory of the application, which is generally /. Since that is on the read-only system volume, you get a "Read-only file system error."

Your second attempt, directing the output to an absolute path in your home directory, is better, but assumes that there is a folder "ROM" in the Downloads folder, so you might have to make it first using mkdir.

If that folder already exists, then I am not sure what the issue is, but it is worth mentioning that "do shell script" does not read your usual shell startup files and therefore does not have any PATH customizations you may have made there — npx may be relying on that.

You also asked about substituting /Users/me: you can use the variable ${HOME}, or since this is a shell, just ~; for example ~/Downloads.

These and other questions are addressed in TN2065: do shell script in AppleScript.

huangapple
  • 本文由 发表于 2023年6月29日 02:50:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76575946.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定