理想情况下,我有以下输入:
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您知道如何使用args运行activate,或者如何从appId获取完整路径吗?
发布于 2021-06-24 16:00:38
在这里不能完全确定您的全部意图,但假设您希望在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 (很容易找到)。我应该补充说,activate命令不是此过程的一部分。该命令将使活动应用程序发生差异合并(取决于…可能导致窗口打开)。
更新:
path to appID触发了应用程序的启动,我不知道有任何方法可以抑制这一点。
可以拼凑到应用程序的命令行界面工具的路径,但它不是通用的-特别是在/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'"https://stackoverflow.com/questions/68110470
复制相似问题