AppleScript Help

Discussion in 'Mac Programming' started by gpchess2k, Apr 13, 2017.

  1. gpchess2k macrumors newbie


    Oct 12, 2015

    I am trying to create an IF statement in Applescript but running into a couple problems.

    1. The first do shell script fails b/c of the space after the backslash (\ and the single double quotes after awk -F '"'.

    2. The Proper syntax for running the second do shell script. I put 'is equal to or greater than' which i know is not correct but can't figure out how to convert it.

    tell application "Finder"
        set exitstatus to do shell script "mdls /Applications/Microsoft\ -name kMDItemVersion | awk -F'"' '{print $2}'"
        if exitstatus is equal to or greater than "15.32" then
        do shell script ¬
        "installer -allowUntrusted -pkg " & myFolder & ¬
        "word.pkg -target /" with administrator privileges
        end if
    end tell
  2. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    AppleScript uses backslash as its escape character within strings. Therefore, you must escape backslashes. Google search terms:
    applescript backslash in string​

    Your alternative to escaping backslashes is to learn how shell quoting works. Refer to the bash man page, heading QUOTING.

    Double-quotes must be escaped within AppleScript strings. Single-quotes are not.

    Regarding question #2, any decent AppleScript tutorial or reference should cover this. If you don't have one, or don't know of one, Apple's is titled "AppleScript Language Guide".
  3. Mark FX macrumors regular

    Mark FX

    Nov 18, 2011
    West Sussex, UK
    I don't have Word installed, but I do have an old version of Pages, located in a sub folder of the Applications folder.
    With a space character in the path "/Applications/iWork '09/", so posted code tested with that path.

    To use AppleScript strings within shell script commands, it's best to use the AppleScript command "quoted form of".

    Like This.

    set OldPagesAppPath to quoted form of "/Applications/iWork '09/" as text
    set OldPagesAppVersion to do shell script ("mdls -name kMDItemVersion -raw " & OldPagesAppPath) as text
    Or like this.

    set OldPagesAppVersion to do shell script ("mdls -name kMDItemVersion -raw " & quoted form of "/Applications/iWork '09/") as text
    Both work, so which ever suits you.

    Secondly, you can use the short version of AppleScript's comparison operators like this.

    if OldPagesAppVersion ≥ "4.3" then
       -- Version is equal to or greater than "4.3"
       -- Version is less than "4.3"
    end if
    But do bare in mind that you are comparing strings in mine and your own posted code.
    Sometimes you may find it better to coerce numbered text into actual numbers before comparison.

    There are several short comparison operators in AppleScript such as.
    "≥" equal to or greater than
    "≤" equal to or less than
    "≠" Not equal to

    Lastly, I don't think you need to have the Finder tell block with your posted code, as your not actually telling Finder to do anything.
    Unless there is other code you are going to add.

    Hope this helps

    Regards Mark
  4. gpchess2k thread starter macrumors newbie


    Oct 12, 2015
    Setting as text was the right method!! I though I had tried it but your code helped a ton. Here is what I did and is working perfectly:

    set pathwithSpaces to "/Applications/Microsoft"
    set WordAppPath to quoted form of pathwithSpaces as text
    set WordAppVersion to do shell script ("mdls -name kMDItemVersion -raw " & WordAppPath) as text
    if WordAppVersion ≥ "15.33" then
        display dialog "Skipping!"
        display dialog "Installing!!"
    end if

Share This Page