Home > Net >  How to combine 2 apple-scripts using "sw_vers"? one script for sierra and other script for
How to combine 2 apple-scripts using "sw_vers"? one script for sierra and other script for

Time:12-04

script for sierra :

    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

script for other macos :

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

CodePudding user response:

There is a native way to get the system version

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

CodePudding user response:

The distinction between your two scripts is slightly more significant than their contents insinuates. For one, they do different things: the first (were it not missing its initial try block opener) sets the doNotShowSplashScreen flag for VoiceOver to true, then launches VoiceOver (i.e. turns it on); the second launches VoiceOver (turns it on) if it's not active, and quits VoiceOver (turns it off) if it is.

Moreover, do shell script was introduced in Mac OS X 10.1. Meanwhile, System Events wasn't introduced until Mac OS X 10.6.3. Therefore, the second script would execute just fine in Sierra, but not in anything pre-Snow Leopard. The first script ought to execute fine in versions since Puma.

I'd recommend considering the relevance of testing the system version and, if you really do need to, whether the distinctions between your scripts truly reflect this relevance.


@user3439894 helpfully pointed you to the use of system version of (system info). AppleScript allows numerical comparisons of version strings like so:

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

or, equivalently:

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

If you work through the logic of your first script, it's clear the end result in any situation is to execute do shell script "/System/Library/CoreServices/VoiceOver.app/Contents/MacOS/VoiceOverStarter", i.e.. to turn VoiceOver on. Slightly less obvious is the that, by the end of the script, the doNotShowSplashScreen flag for VoiceOver will always be true regardless of its initial state. Therefore:

  1. The first observation highlights the discrepancy between your two scripts' functionality that I mentioned above, because the System Events command will toggle VoiceOver. This can be rectified like so:

    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. The second observation means there's no point reading the value of the doNotShowSplashScreen flag at the start of your "Sierra" script, so you can take out the entire try block altogether, and forego the if...then...else condition testing as well. This allows your first script to be reduced simply to:

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

    Note that's a single string that contains a line-break, which is perfectly fine

I'll leave you to determine whether or not you actually need both of these scripts. They both to me look as though they'd run on any system version after 10.6.3, and since the only functional difference between them now is the writing of the defaults key/value pair, I'd be tempted to stick with the single call to do shell script and expunge everything else.

  • Related