Thursday, March 30, 2017

#1 2017-03-15 02:06:20 pm

dne202
Member
Registered: 2016-07-28
Posts: 14

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

output="$(find ~/Desktop -not -path '*/\.*' -type f -exec basename {} ';')"

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.

Applescript:

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?

Offline

 

#2 2017-03-15 02:26:31 pm

t.spoon
Member
From: BFE, Massachusetts
Registered: 2013-01-12
Posts: 103

Re: Escaping characters inside a "do shell script" command

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

so this is working for me:

Applescript:

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

Offline

 

#3 2017-03-15 04:10:11 pm

dne202
Member
Registered: 2016-07-28
Posts: 14

Re: Escaping characters inside a "do shell script" command

Worked like a charm, thank you!

Offline

 

#4 2017-03-15 04:42:30 pm

t.spoon
Member
From: BFE, Massachusetts
Registered: 2013-01-12
Posts: 103

Re: Escaping characters inside a "do shell script" command

You're welcome.

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

Applescript:

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.

Last edited by t.spoon (2017-03-15 04:43:58 pm)

Offline

 

#5 2017-03-16 05:45:42 am

DJ Bazzie Wazzie
Member
From: the Netherlands
Registered: 2004-10-20
Posts: 2594

Re: Escaping characters inside a "do shell script" command

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:

Applescript:

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

Last edited by DJ Bazzie Wazzie (2017-03-16 05:46:02 am)

Offline

 

#6 2017-03-16 09:43:52 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 2885

Re: Escaping characters inside a "do shell script" command

What for:

Applescript:

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

Last edited by Yvan Koenig (2017-03-16 09:50:09 am)

Offline

 

#7 2017-03-16 11:14:17 am

t.spoon
Member
From: BFE, Massachusetts
Registered: 2013-01-12
Posts: 103

Re: Escaping characters inside a "do shell script" command

the System Events solution isn't exactly the same

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

I'm trying to filter out the hidden files

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.

Last edited by t.spoon (2017-03-16 11:14:38 am)

Offline

 

#8 2017-03-16 01:51:25 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 2885

Re: Escaping characters inside a "do shell script" command

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

Offline

 

#9 2017-03-22 05:55:50 pm

dne202
Member
Registered: 2016-07-28
Posts: 14

Re: Escaping characters inside a "do shell script" command

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

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.035 seconds, 8 queries executed ]

RSS (new topics) RSS (active topics)