AppleScript Help

gpchess2k

macrumors newbie
Original poster
Oct 12, 2015
29
0
Hello,

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 (\ Word.app) 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.

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

chown33

Moderator
Staff member
Aug 9, 2009
8,350
4,318
Coruscant antipodal seas
1. The first do shell script fails b/c of the space after the backslash (\ Word.app) and the single double quotes after awk -F '"'.
[/CODE]
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".
 

Mark FX

macrumors regular
Nov 18, 2011
119
8
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/Pages.app", 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.

Code:
set OldPagesAppPath to quoted form of "/Applications/iWork '09/Pages.app" as text

set OldPagesAppVersion to do shell script ("mdls -name kMDItemVersion -raw " & OldPagesAppPath) as text
Or like this.

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

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

Code:
if OldPagesAppVersion ≥ "4.3" then
   -- Version is equal to or greater than "4.3"
else
   -- 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
 
  • Like
Reactions: gpchess2k

gpchess2k

macrumors newbie
Original poster
Oct 12, 2015
29
0
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:

Code:
set pathwithSpaces to "/Applications/Microsoft Word.app"

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!"
else
    display dialog "Installing!!"
end if