applescript - 如何在 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 获取完整路径吗?
解决方案
不完全确定您在这里的全部意图,但假设您希望在 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'"
推荐阅读
- excel - 表格视图中的 Excel 查找
- javascript - 简单的node.js修改,如何判断一个变量是递增还是递减?
- java - 调用第二种方法时出现空指针异常
- wordpress - 为所有用户类型启用查询监视器插件 - 无管理员
- android - 每次更改时,Android Studio 设计屏幕都会滚动到顶部
- python - 如何在 python Turtle 图形中检测 X(关闭)按钮?
- jquery - Search any content from div tag and show the searched div at top of all divs
- qt - How to make an item fill all available vertical space in QML?
- python - Xor multiple arrays in Python
- java - 插入后 SQL-Sever 表不更新