Want to highlight a helpful answer? Upvote!

Did someone help you, or did an answer or User Tip resolve your issue? Upvote by selecting the upvote arrow. Your feedback helps others! Learn more about when to upvote >

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

AppleScript Help w/ GREP

I'm trying to create a script that will open terminal and run a specific command that I created to get info from the system profiler. When I manually run the command in terminal it works perfectly; however when I paste it into the AppleScript editor it no longer works.


The command I want to run is:


system_profiler SPHardwareDataType SPPowerDataType SPStorageDataType SPDisplaysDataType SPMemoryDataType SPDiscBurningDataType SPCameraDataType | grep 'Cycle\|Processor\|Serial Number (system)\|Model\|Capacity\|Media\|Chipset Model\|VRAM\|Resolution\|Memory\|FaceTime\|Total Number of Cores\|Medium Type\|Type\|Speed\|Cycle\|CD-Write\|DVD-Write\|Camera’


In AppleScript Editor I have this:


tell application "Terminal"

set currentTab to do script "system_profiler SPHardwareDataType SPPowerDataType SPStorageDataType SPDisplaysDataType SPMemoryDataType SPDiscBurningDataType SPCameraDataType | grep 'Cycle\|Processor\|Serial Number (system)\|Model\|Capacity\|Media\|Chipset Model\|VRAM\|Resolution\|Memory\|FaceTime\|Total Number of Cores\|Medium Type\|Type\|Speed\|Cycle\|CD-Write\|DVD-Write\|Camera’"

activate

end tell


The error I get in AppleScript Editor is: Syntax Error: Expected “"” but found unknown token. If I run the same command without the grep portion it works OK.


Any ideas how to make this work?


Also, I want to the run: sysctl machdep.cpu.brand_string in the same script to get the full CPU info is that possible to run both one right after the other?

Macbook, Mac OS X (10.5.5), null

Posted on Aug 31, 2015 12:40 PM

Reply
9 replies

Aug 31, 2015 1:56 PM in response to Computer Overhauls

The following code will open a new Terminal window, and run the script, producing the appropriate output in that window. Notice I inserted a newline (which disappears upon compile) prior to the sysctl command.


tell application "Terminal"


activate


do script "system_profiler SPHardwareDataType SPPowerDataType SPStorageDataType SPDisplaysDataType SPMemoryDataType SPDiscBurningDataType SPCameraDataType | grep \"Cycle|Processor|Serial Number (system)|Model|Capacity|Media|Chipset Model|VRAM|Resolution|Memory|FaceTime|Total Number of Cores|Medium Type|Type|Speed|Cycle|CD-Write|DVD-Write|Camera\"

sysctl machdep.cpu.brand_string | sed -En 's/.+: (.+)$/\\1/p'"


end tell

Aug 31, 2015 2:29 PM in response to VikingOSX

Hi,


Thanks for your replies. I tried it both with the new line and with just a semi colon but didn't seem to get the correct output. The script runs but terminal only shows the output from the sysctl machdep.cpu.brand_string command. None of the output from the system profiler shows.


User uploaded file


The output from the system_profiler command should show this type of info:

User uploaded file

Aug 31, 2015 2:49 PM in response to Computer Overhauls

This works for me. There are no hard returns in the do script syntax.


tell application "Terminal"


activate


do script "system_profiler SPHardwareDataType SPPowerDataType SPStorageDataType SPDisplaysDataType SPMemoryDataType SPDiscBurningDataType SPCameraDataType | grep \"Cycle|Processor|Serial Number (system)|Model|Capacity|Media|Chipset Model|VRAM|Resolution|Memory|FaceTime|Total Number of Cores|Medium Type|Type|Speed|Cycle|CD-Write|DVD-Write|Camera\";sysctl machdep.cpu.brand_string | sed -En 's/.+: (.+)$/\\1/p'" in front window


end tell

Sep 1, 2015 7:22 AM in response to Computer Overhauls

Hi,


If a terminal command contains a backslash, you need to escape each backslash in an AppleScript

So use \\| instead of \|

tell application "Terminal"
      set currentTab to do script "system_profiler SPHardwareDataType SPPowerDataType SPStorageDataType SPDisplaysDataType SPMemoryDataType SPDiscBurningDataType SPCameraDataType|grep 'Cycle\\|Processor\\|Serial Number (system)\\|Model\\|Capacity\\|Media\\|Chipset Model\\|VRAM\\|Resolution\\|Memory\\|FaceTime\\|Total Number of Cores\\|Medium Type\\|Type\\|Speed\\|Cycle\\|CD-Write\\|DVD-Write\\|Camera'"
      activate
end tell

Sep 1, 2015 7:34 AM in response to VikingOSX

I've isolated the issue to something within the GREP portion.

If I have just a SINGLE word in GREP then it works but as soon as I can two or more words it does work.

With this script I'm search for just "Model Name" within SPHardwareDataType and it works.

tell application "Terminal"

activate

do script "system_profiler SPHardwareDataType | grep \"Model Name\";sysctl machdep.cpu.brand_string | sed -En 's/.+: (.+)$/\\1/p'" in front window

end tell

Terminal Output:

Owners-MacBook-Pro:~ owner$ system_profiler SPHardwareDataType | grep "Model Name";sysctl machdep.cpu.brand_string | sed -En 's/.+: (.+)$/\1/p'

Model Name: MacBook Pro

Intel(R) Core(TM) i5-4278U CPU @ 2.60GHz



However, If I try the same script but search for Model Name AND Model Identifier it only shows the sysctl command; nothing from the GREP.

tell application "Terminal"

activate

do script "system_profiler SPHardwareDataType | grep \"Model Name|Model Identifier\";sysctl machdep.cpu.brand_string | sed -En 's/.+: (.+)$/\\1/p'" in front window

end tell

Terminal Output

Owners-MacBook-Pro:~ owner$ system_profiler SPHardwareDataType | grep "Model Name|Model Identifier";sysctl machdep.cpu.brand_string | sed -En 's/.+: (.+)$/\1/p'

Intel(R) Core(TM) i5-4278U CPU @ 2.60GHz


Is there a different way I should be entering multiple GREP words to be searching by? It seem that this: grep \"Model Name|Model Identifier\" isn't being read properly. I tried it on a few different Macs but laptops and desktops but always had the same results...

Sep 1, 2015 7:39 AM in response to Computer Overhauls

That was it!


The GREP needed two backslashes like this \\| between each search term:


grep \"Cycle\\|Processor\\|Serial Number (system)\\|Model\\|Capacity\\|Media\\|Chipset Model\\|VRAM\\|Resolution\\|Memory\\|FaceTime\\|Total Number of Cores\\|Medium Type\\|Type\\|Speed\\|Cycle\\|CD-Write\\|DVD-Write\\|Camera\"

Now it works perfectly!

Thanks!!

Sep 1, 2015 8:17 AM in response to VikingOSX

The reason this AppleScript as written in my previous post works perfectly for me is that I have enabled GREP_OPTIONS in my .bashrc to include --extended-regexp. Thus every time I use grep, it is really invoking extended grep (egrep) and I do not need the double-backslashes before the | operators. Plain grep requires these in AppleScript, or you only get the sysctl output. You could also invoke grep as grep -E and my syntax would work equally well.


In the future, I will no longer assume everyone else is using extended grep.

AppleScript Help w/ GREP

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.