why am I getting this error in AppleScript and how do I fix it

Hi im trying to automate some task using AppleScript and am trying to get the script to tell terminal to run the following script but keep getting this error "Expected end of line but found “{”. "

here is the code im trying to run

tell application "Terminal" do script "find . -maxdepth 1 -type d -exec sh -c 'echo "{}: $(ls -1 "{}" | wc -l) file(s)"' ;" end tell

any help would be appreciated

haven't tried much consulted chatgpt for the error which is where I got the code from in the first place and it runs manually in terminal but for some reason having a hard time with AppleScript

Upvotes: 0

Views: 600

Answers (1)

Mockman
Mockman

Reputation: 1205

Try this:

do shell script "find . -maxdepth 1 -type d -exec sh -c 'echo \"{}: $(ls -1 \"{}\" | wc -l) file(s)\"' \\;"

Three issues:

First, do script refers to other applescripts while do shell script refers to what you want to do.

Second, in applescript, when entering shell script commands, you need to escape quotes and backslashes. Your command has four interior quotes that require a backslash.

Third, the terminating semicolon needs to be escaped — probably. At least, it worked for me once I did so… and of course, the escaping backslash must be escaped.

When I run the above, it gives me a response like this (but the list goes on for a bit):

".:       19 file(s)
./.DocumentRevisions-V100:        0 file(s)
[…]"

You can get some background information on shell scripting in applescript in apple's Technical Note TN2065 - do shell script in AppleScript

Actually… one more thing: You don't need (or really want) the Terminal involved. You can execute your commands directly. The technical note covers some situations where this might not be the case but I don't believe this would be one.

Upvotes: 0

Related Questions