Escaping characters inside a "do shell script" command

I’m writing a script to inject the names of the files inside a folder into a custom metadata field of the tarball of that folder. I’m trying to filter out the hidden files in that list of file names. In terminal I can issue this command

But when I try to write that into an applescript I get errors with the ‘/.’ part of the command. I’m not sure how to handle this escaped character. Tried a few ways and can’t seem to crack it. My eventual script should like something like this.

do shell script "output='$(find " & (theFolderPath) & (quoted form of (name of theFolder as text)) & " -not -path '*/\.*' -type f -exec basename {} ';')' && xattr -w com.apple.metadata:Filenames '${output}' " & nameofTarbal

Any thoughts?

For Applescript, anything inside quotes needs quotation marks escaped as:
"
and backslash escaped with a preceding backslash:
\

so this is working for me:

set shellOutput to do shell script "(find ~/Desktop -not -path '*/\\.*' -type f -exec basename {} ';')"

Worked like a charm, thank you!

You’re welcome.

Or if you want to be “Applescript-y” about it:

tell application "System Events" to set fileNames to the name of every file of (path to desktop folder) whose visible is true

I get a run time of 0.08 seconds on the terminal command, but only 0.001 on the Applescript.

Problem is that files can be invisible even when they don’t start with a period so the System Events solution isn’t exactly the same. Think about folder like /Volumes, /usr and /etc you won’t see on your boot disk.

For all I know AppleScript doesn’t have an single command ready for this. AppleScript Toolbox does however:

AST list folder "/" matching regex "^[^.]" 

What for:

tell application "System Events" to set fileNames to the name of every disk,item of (folder "Macintosh HD") whose (name does not start with ".")
--> {"Applications", "assignIcon.py", "bin", "bin @", "cores", "delete_image_file_resource_fork.pl", "dev", "Developer", "Documents", "etc", "etc @", "get archives AppleScript - stripped.scpt", "home", "Important1.zip", "joinPDF.py", "Library", "mach_kernel", "Macintosh .2.0", "net", "Network", "opt", "opt @", "private", "private @", "sbin", "sbin @", "System", "tmp", "tmp @", "traces.log", "Users", "usr", "usr @", "var", "Volumes"}

The items whose name ends with " @" are visible aliases to invisible items.

Yvan KOENIG running Sierra 10.12.3 in French (VALLAURIS, France) jeudi 16 mars 2017 15:43:48

That’s true, but the original post did say:

So I’m guessing that, to the extent the results would not be the same, it’s actually the results of my System Events line that he would want.

It was not clear : I responded to DJ Bazzie Wazzie’s proposal.

Yvan KOENIG running Sierra 10.12.3 in French (VALLAURIS, France) jeudi 16 mars 2017 19:51:20

Hmmm, I’ve got your first method working. But, now I’ll have to try your system events method, t.spoon! Thank you!