Sunday, October 17, 2021

#1 2021-10-09 03:22:10 am

lotr
Member
Registered: 2011-08-22
Posts: 107
Website

Converting terminal command to AS command

Wanting to experiment with a clean zip file, but difficulty attempting at incorporating such a command into AS.

The needed command is
zip -r my_compressed_file.zip path/to/folder -x "*.DS_Store" -x "__MACOSX"

AS does not like the " after the  -x I've tried adding more " to the command but still no fix.
I'd normally expect the command to start like:         try
            do shell script "zip ...
Any advice appreciated.

Model: mp3,1
AppleScript: 2.8.1
Browser: Firefox 78.0
Operating System: macOS 10.11


The shy can light the dull dark room rich with life when their eyes mirror their inner sunrise

Offline

 

#2 2021-10-09 04:10:14 am

Mockman
Member
From:: Toronto
Registered: 2020-05-27
Posts: 128

Re: Converting terminal command to AS command

Try setting up your paths beforehand. If, for example, you had spaces anywhere then your script would fail just for that but you don't mention the error you're actually receiving.

Applescript:

-- path to directory to zip
set dir2zip to POSIX path of (path to desktop as text) & "application exists"
set qd to quoted form of dir2zip

-- path to resulting archive
set zippedFile to POSIX path of (path to desktop as text) & "myc.zip"
set qzf to quoted form of zippedFile

-- do shell script "zip -r " & qzf & space & qd
do shell script "zip -r " & qzf & space & qd & " -x '*.DS_Store' '__MACOSX'"

This is the command that the above sends to the shell:

"zip -r '/Users/kkeller/Desktop/myc.zip' '/Users/kkeller/Desktop/application exists' -x '*.DS_Store' '__MACOSX'"

Last edited by Mockman (2021-10-09 07:54:27 am)


Filed under: shell

Offline

 

#3 2021-10-09 07:04:11 am

Marc Anthony
Member
From:: Dallas, TX
Registered: 2006-04-27
Posts: 1043

Re: Converting terminal command to AS command

The man page suggests a necessity to use a backslash to avoid potential filename substitution, so quoting the excluded items might not be desirable; they can also be grouped in a "list."

Applescript:

do shell script "zip -r " & (((path to desktop)'s POSIX path) & "zip-a-dee-doo-dah.zip")'s quoted form & space & (choose folder)'s POSIX path's quoted form & " -x \\*.DS_Store \\__MACOSX "

Offline

 

#4 2021-10-09 07:38:19 am

Mockman
Member
From:: Toronto
Registered: 2020-05-27
Posts: 128

Re: Converting terminal command to AS command

The single quotes, i.e. quoted form, prevent the shell from interpreting anything within them.

    https://developer.apple.com/library/arc … ______ETC_

Out of curiosity, why escape the first underscore?

You're right about only requiring a single '-x'.

I find that either of these will work, which makes sense to me as they're both escaping the asterisk. Just having the asterisk also works but then you have the risk of something unforeseen happening.
   

    -x \\*.DS_Store
    -x '*.DS_Store'

This doesn't work but I'm not sure why.
   

    -x '.DS_Store'

Last edited by Mockman (2021-10-09 07:51:38 am)

Offline

 

#5 2021-10-09 09:46:11 am

Marc Anthony
Member
From:: Dallas, TX
Registered: 2006-04-27
Posts: 1043

Re: Converting terminal command to AS command

It does appear to work either way—single quoted or slash escaped. In regards to why the last example failed to work, it's because the whole path, rather than the name, is being evaluated; the asterisk is a wildcard for 'whatever' that precedes '.DS_Store.' I didn't have such a file for testing, but my script's second list item would actually need to be adjusted to also include an asterisk; e.g. "\\*_MACOSX."

Offline

 

#6 2021-10-09 07:17:02 pm

Mockman
Member
From:: Toronto
Registered: 2020-05-27
Posts: 128

Re: Converting terminal command to AS command

Okay that makes sense. I was thinking that having the filename would be sufficient because it was working the same folder but that's not the case. I tried working with just the top-level directory (i.e. no recursion). It added all the files but the exclusion failed without the asterisk, even when I ran the command from that directory. Something learned. Thanks.

I guess the last file falls into the same category — I also didn't have one of those to test.

Last edited by Mockman (2021-10-09 07:17:16 pm)

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)