2

I'm using a MacBook Pro (Retina, Mid-2014) running El Capitan. I'm trying to create a script that checks if my computer is open or not, and I'm using this:

ioreg -r -k AppleClamshellState | grep '"AppleClamshellState"' | cut -f2 -d"=""

I tested this in Terminal and it works fine, so I add do shell script in front of it to run it in AppleScript. But when I do, it gives me this:

Expected end of line, etc. but found identifier.

Please help!

  • I updated my answer to use grep and cut however I'd stick with awk! – user3439894 Mar 3 '16 at 3:05
  • See the Note at the bottom of my answer to shed some light on the quoting issues surrounding complex commands used with do shell script. – user3439894 Mar 3 '16 at 3:21
0

Instead of piping to grep and cut, just use awk.

do shell script "ioreg -r -k AppleClamshellState | awk -F \" = \" '/AppleClamshellState/ {print $2}'"
  • -F \" = \" makes the separator: = (space = space)
  • /AppleClamshellState/ Search for: AppleClamshellState
  • {print $2} Prints Yes or No depending on what's returned for:AppleClamshellState
  • E.g "AppleClamshellState" = No prints No

enter image description here

You can also use:

do shell script "ioreg -r -k AppleClamshellState | grep 'AppleClamshellState' | cut -d'=' -f2"

However there will be a leading space in front of Yes or No as cut only handles a single character for the deliminator.

enter image description here

Note: The command text portion of do shell script, e.g. do shell script "uptime" must be double-quoted. So when the command text string itself is using double-quotes within the string, one should substitute a single-quote ' wherever possible and if double-quotes must be used within the string then escape them with a backslash \.

  • I get the same error. – atirit Mar 3 '16 at 2:26
  • Works fine for me! – user3439894 Mar 3 '16 at 2:35
  • OK, now I have set myVar to "do shell script "ioreg -r -k AppleClamshellState | awk -F \" = \" '/AppleClamshellState/ {print $2}'"", and it gives me A identifier can’t go after this “"”. higlighting ioreg. – atirit Mar 3 '16 at 2:51
  • Your command is malformed! There should be no " in front of do and you have an extra " at the end. Copy and paste my code exactly as is and it should work! – user3439894 Mar 3 '16 at 2:58
  • Just realised that right before you commented. Thanks for your help! :-) – atirit Mar 3 '16 at 2:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .