如何通过AppleScript判断MacOS应用是否为Universal版本?
判断Mac应用架构的两种实现方案
方案1:AppleScript调用Shell命令
Mac系统自带的lipo -info命令可以直接获取二进制文件的架构信息,结合AppleScript封装成可接收路径参数的脚本:
on run {appPath} set archInfo to do shell script "lipo -info " & quoted form of appPath if archInfo contains "x86_64" and archInfo contains "arm64" then return "Universal (x86_64 arm64)" else if archInfo contains "x86_64" then return "Intel (x86_64)" else return "Unknown architecture" end if end run
使用方式:调用时传入应用可执行文件的完整路径(比如/Applications/xxx.app/Contents/MacOS/xxx),脚本会返回对应的架构类型。
方案2:Mac版VBA直接运行Shell脚本
Mac版VBA可以通过Shell命令执行系统脚本,由于VBA的Shell函数默认不返回执行结果,需要借助临时文件获取输出,实现代码如下:
Function GetAppArchitecture(appPath As String) As String Dim tempFile As String Dim shellCmd As String Dim fileNum As Integer Dim archInfo As String ' 生成临时文件路径 tempFile = Environ("TMPDIR") & "arch_check.txt" ' 构造lipo命令,将输出写入临时文件 shellCmd = "lipo -info " & Chr(34) & appPath & Chr(34) & " > " & Chr(34) & tempFile & Chr(34) ' 执行Shell命令 Shell shellCmd, vbNormalFocus ' 读取临时文件内容 fileNum = FreeFile() Open tempFile For Input As #fileNum archInfo = Input$(LOF(fileNum), fileNum) Close #fileNum ' 清理临时文件 Kill tempFile ' 判断并返回架构类型 If InStr(archInfo, "x86_64") > 0 And InStr(archInfo, "arm64") > 0 Then GetAppArchitecture = "Universal (x86_64 arm64)" ElseIf InStr(archInfo, "x86_64") > 0 Then GetAppArchitecture = "Intel (x86_64)" Else GetAppArchitecture = "Unknown architecture" End If End Function
使用方式:在VBA中调用这个函数,传入应用可执行文件的完整路径即可得到架构类型。代码中的Chr(34)会自动为路径添加引号,避免路径含空格时命令执行出错。
内容的提问来源于stack exchange,提问作者Sankalp
相关产品推荐
相关产品推荐

