首页 > 解决方案 > 如何在 applescript 中使用参数运行 DiffMerge?

问题描述

理想情况下,我有这些输入:

set appId to "com.sourcegear.DiffMerge"
set path1 to "/tmp/file1.txt"
set path2 to "/tmp/file2.txt"

并且需要使用带有给定参数的 appId 运行应用程序。

我知道如何使用 appId 运行应用程序,但这没有通过参数。

tell application id appId to activate

或者我可以运行应用程序并传递参数,但我不知道如何从 appId 获取路径

set diff_path to "/Applications/pp/dev/DiffMerge.app/Contents/MacOS/DiffMerge"
set cmd to diff_path & " '" & path1 & "' '" & path2 & "'"
do shell script cmd

您知道如何activate使用 args 运行或如何从 appId 获取完整路径吗?

标签: applescriptautomator

解决方案


不完全确定您在这里的全部意图,但假设您希望在 applescript 中使用 diffmerge 的命令行功能,请尝试以下操作:

set appID to "com.sourcegear.DiffMerge"
set appIDO to application id "com.sourcegear.DiffMerge"
set diff_path to quoted form of POSIX path of (path to appIDO) & "Contents/MacOS/DiffMerge"
set path1 to quoted form of "/tmp/file1.txt"
set path2 to quoted form of "/tmp/file2.txt"
set cmd to diff_path & space & path1 & space & path2
do shell script cmd

它将运行这个 shell 命令:

"'/Applications/pp/dev/DiffMerge.app/Contents/MacOS/DiffMerge' '/tmp/file1.txt' '/tmp/file2.txt'"

quoted form代码将文本用单引号括起来,这样 shell 就不会对其进行进一步的解释。对于您的特定示例,这可能不是问题,但如果任何路径或文件名有空格,它不应该导致错误。做的space很明显。如果你愿意,你可以将上面的内容压缩成更少的行。

有关 applescript 如何处理“do shell”的更多信息,请查看苹果技术说明 TN2065(很容易找到)。我应该补充一点,激活命令不是这个过程的一部分。该命令将使活动应用程序 diffmerge (并且取决于...可能会导致打开一个窗口)。

更新:

path to appID触发应用程序的启动,我不知道有任何抑制它的方法。

可以将应用程序的 CLI 工具的路径拼凑在一起,但它并不通用——特别是在 /Applications 中的嵌套文件夹周围。

set sName to "DiffMerge"
set cName to "DiffMerge"

set aPath to path to (applications folder) as text
set sFol to ":Contents:MacOS:"
--> ":Contents:MacOS:"

set gApp to aPath & sName & ".app"
--> "MacHD:Applications:DiffMerge.app"

set cTool to aPath & gApp & sFol & cName
--> "MacHD:Applications:DiffMerge.app:Contents:MacOS:DiffMerge"

set cmdPath to quoted form of POSIX path of cTool
--> "'/Applications/DiffMerge.app/Contents/MacOS/DiffMerge'"


set path1 to quoted form of "/tmp/file1.txt"
set path2 to quoted form of "/tmp/file2.txt"

cmdPath & space & path1 & space & path2
--> "'/Applications/MacHD/Applications/DiffMerge.app/Contents/MacOS/DiffMerge' '/tmp/file1.txt' '/tmp/file2.txt'"

推荐阅读