How to do shell script with " in the code

I’m such a newb but I completely forgot how to

do shell script

command that contains

"

in the code

do shell script date | awk '{ print $2 " " $3 " " $6 " " $4 " " $5}' | tr -s [:space:] ' ' | tr " " _ | sed 's/_$//'

Every time I try to run the following I’m getting the error

Expected "," but found "'".

Can you please help me run the above command

The command takes the bash command date which normally returns
Mon Nov 24 16:03:20 PST 2014

but returns
Nov_24_2014_16:03:20_PST

you need to escape a " when you want to use it in a string:

"\""

Your example script will look like:

do shell script "date | awk '{ print $2 \" \" $3 \" \" $6 \" \" $4 \" \" $5}' | tr -s [:space:] ' ' | tr \" \" _ | sed 's/_$//'"

Hi. I’m not sure if there’s a legitimate reason for all the double quotes”you might try just using commas. A variant:

do shell script "date | awk '{print $2, $3, $6, $4, $5}' | awk '{gsub(/[ ]+/, \"_\");print}'"

And if your using Yosemite and using that code regularly, say for logging, you could use this instead:

use framework "Foundation"

set theFormatter to current application's NSDateFormatter's alloc()'s init()
theFormatter's setDateFormat:"MMM_dd_yyyy_HH:mm:ss_z" -- Nov_24_2014_16:03:20_PST 
set theDate to current application's NSDate's |date|()
(theFormatter's stringFromDate:theDate) as text

Just because it runs about 15-20 times faster, or more than 50 times faster if you create the formatter once.

Hi Shane! What is this? It’s not pure AppleScript, is it? I’ve already seen that “new” language and I wonder what it is and how to learn it. It sounds to be much faster and somehow easier though it requires clearly some learning.

It’s AppleScriptObjC – basically calling Cocoa/Objective-C via AppleScript. As of Yosemite you can mix it with any other AS code (in Mavericks you had to store it in scripting libraries, and before that you had to write Xcode projects). So I guess it’s pseudo-vanilla.

It is faster for some things, although not all things by any means. And I wouldn’t call it easier (unless the alternative is sed or awk;)), although it’s also not rocket science. But it does give you access to lots of powerful stuff.

It sounds like a plug, but the main resource I can point you to is my book:

www.macosxautomation.com/applescript/apps/everyday_book.html

There are also quite a few samples in various threads around here.

Thanks for the input. I shall take time to read deeper later on, when I get some “free” time. Just to understand the thing: does it allow the scripter to reach some Objective-C components of applications that are not pure AS scriptable? Or it has nothing to do with the app code? I mean where does, for instance, the current application’s NSDateFormatter’s alloc()'s init() come from? Do I need to learn Obj-C prior to go further? Sorry for hijacking the original post.

It gets at the components apps get at, but not within other apps. It’s more about bypassing apps, in a way, or doing things apps can’t do. A bit like do shell script in that respect.

NSDateFormatter is a Cocoa class – essentially part of the OS. So that line is allocating and initializing an instance of the NSDateFormatter class.

No, but you need to understand a bit about how it works, and the concepts of classes and instances.

Looking at the desired results, instead of the question asked by the TS you won’t need AWK at all.

do shell script "date +%b_%d_%Y_%H:%M:%S_%Z"

Thank you, Shane! I’ve searched the Net for more info and details about all that. I found a picture of you and it’s nice to see how people look like in real :slight_smile: I’ve also read you’re a real specialist of AS and ASOC. Which was not really a discovery to me :slight_smile:

I’ll really try to find time to dig into ASOC. I’m still on ML and am not always keen on upgrading rapidly to the newest version of Mac OS (or whatever application). Each time I upgrade sth, some of scripts are just ruined for one reason or another. One cannot be on every front. Thanks again for the info.

You all are the best, thanks so much for all the great feedback. I will try all and see what works best.

Thanks again