【问题标题】:How to combine 2 apple-scripts using "sw_vers"? one script for sierra and other script for other mac os's?如何使用“sw_vers”组合 2 个苹果脚本?一个用于 sierra 的脚本和另一个用于其他 mac os 的脚本?
【发布时间】:2022-01-10 01:59:16
【问题描述】:

山脉脚本:

    set doNotShowSplashScreen to (do shell script "defaults read com.apple.VoiceOverTraining doNotShowSplashScreen") as integer as boolean
on error
    set doNotShowSplashScreen to false
end try
if doNotShowSplashScreen then
    do shell script "/System/Library/CoreServices/VoiceOver.app/Contents/MacOS/VoiceOverStarter"
else
    do shell script "defaults write com.apple.VoiceOverTraining doNotShowSplashScreen -bool true && /System/Library/CoreServices/VoiceOver.app/Contents/MacOS/VoiceOverStarter"
end if

其他 macOS 的脚本:

tell application id "com.apple.systemevents" to key code 96 using command down

【问题讨论】:

  • 您检查您在哪个版本的 macOS 下运行,然后使用 if statement 运行 代码 基于版本。看看if Starements。您还可以使用 AppleScript 的例如system version of (system info) 而不是 shell 命令 sw_vers 来获取版本。

标签: macos applescript


【解决方案1】:

有获取系统版本的原生方式

set systemVersion to system version of (system info)
if systemVersion starts with "10.12" then
    try
        set doNotShowSplashScreen to (do shell script "defaults read com.apple.VoiceOverTraining doNotShowSplashScreen") as integer as boolean
    on error
        set doNotShowSplashScreen to false
    end try
    if doNotShowSplashScreen then
        do shell script "/System/Library/CoreServices/VoiceOver.app/Contents/MacOS/VoiceOverStarter"
    else
        do shell script "defaults write com.apple.VoiceOverTraining doNotShowSplashScreen -bool true && /System/Library/CoreServices/VoiceOver.app/Contents/MacOS/VoiceOverStarter"
    end if
else
    tell application id "com.apple.systemevents" to key code 96 using command down
end if

【讨论】:

    【解决方案2】:

    您的两个脚本之间的区别比它们的内容所暗示的要重要一些。一方面,他们做了不同的事情:第一个(如果它没有错过它最初的 try 块开启器)将 VoiceOver 的 doNotShowSplashScreen 标志设置为true,然后启动 VoiceOver(即打开它在);第二个启动 VoiceOver(打开它)如果它不活动,如果是则退出 VoiceOver(关闭它)。

    此外,do shell script 是在 Mac OS X 10.1 中引入的。同时,直到 Mac OS X 10.6.3 才引入系统事件。因此,第二个脚本在 Sierra 中执行得很好,但在 Snow Leopard 之前的任何东西中都不会执行。自 Puma 以来的版本中,第一个脚本应该可以正常执行。

    我建议考虑测试系统版本的相关性,如果您确实需要,您的脚本之间的区别是否真正反映了这种相关性。


    @user3439894 很有帮助地向您指出了 system version of (system info) 的用法。 AppleScript 允许对版本字符串进行数值比较,如下所示:

    set os_sevs_available to true
    
    considering numeric strings
        if "10.6.3" > (system info)'s ¬
            system version then set ¬
            os_sevs_available to false
    end considering
    

    或者,等效地:

    considering numeric strings
        set os_sevs_available to (system info)'s system version ≥ "10.6.3"
    end considering
    

    如果您完成第一个脚本的逻辑,很明显在任何情况下最终结果都是执行 do shell script "/System/Library/CoreServices/VoiceOver.app/Contents/MacOS/VoiceOverStarter",即打开 VoiceOver。稍微不太明显的是,在脚本结束时,VoiceOver 的 doNotShowSplashScreen 标志将始终true,无论其初始状态如何。因此:

    1. 第一个观察结果突出了我上面提到的两个脚本功能之间的差异,因为系统事件命令将切换 VoiceOver。这可以像这样纠正:

      if os_sevs_available then
          if application id "com.apple.VoiceOver" is running then return
          tell application id "com.apple.systemevents" to key code 96 using command down
      end if
      
    2. 第二个观察意味着在“Sierra”脚本开头读取 doNotShowSplashScreen 标志的值是没有意义的,因此您可以完全取出整个 try 块,并且也放弃了if...then...else 条件测试。这使您的第一个脚本可以简化为:

      do shell script "defaults write com.apple.VoiceOverTraining doNotShowSplashScreen -bool true;
                       /System/Library/CoreServices/VoiceOver.app/Contents/MacOS/VoiceOverStarter"
      

      请注意,这是一个包含换行符的单个字符串,非常好

    我会让你确定你是否真的需要这两个脚本。在我看来,它们都可以在 10.6.3 之后的任何系统版本上运行,并且由于它们之间唯一的功能区别是 defaults 键/值对的编写,我很想坚持使用单次调用 do shell script 并删除其他所有内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-22
      • 2017-04-15
      • 1970-01-01
      相关资源
      最近更新 更多