How Do I Create Zip File in AppleScript and Retain Finder Tags?

I have been actively tagging (with Finder Tags) most of my script files for some time to aid me in finding scripts of interest. I have to say that the Finder Spotlight Search using tags is working quite well.

But I have recently learned that when sending files to others the file does NOT retain the Finder Tags unless it is zipped, or “compressed” as it is called in the Finder. When I right-click on a file in the Finder and select “compress” for a file, it keeps the Tags. But when I run an AppleScript / Shell Script using the zip command, the Tags are NOT retained. Here is my script:

tell application "Finder"
  set theItem to choose file --i had no problems when i changed this to choose folder
  set itemPath to quoted form of POSIX path of theItem
  set fileName to name of theItem
  set thefolder to container of theItem
  set zipFileHFS to (thefolder as text) & fileName & ".zip"
  set zipFile to quoted form of (POSIX path of zipFileHFS)
  
  set zipResults to do shell script "zip " & zipFile & " " & itemPath
  
  select zipFileHFS
  set itemList to selection
  set oZip to item 1 of itemList
  
end tell

I also just tested this in Terminal, but still no Tags:

zip -X- zip_from_term_X.zip 'test File with Tags.txt'

What am I doing wrong?
Should I be using ASObjC? If so, how?

I need to zip/compress by script because after I zip the file, I need to do some other stuff with the zipped file using the script.

I have done considerable research and studied the zip command options, but to no avail.

TIA.

Use ditto instead. I use:

do shell script "ditto -c -k --keepParent " & quoted form of sourcePosixPath & space & quoted form of destPosixPath

That should give the same result as the Finder.

2 Likes

Thanks, Shane. That works great! :+1:

I removed the --keepParent switch so that it would not put the zipped file in a folder:

tell application "Finder"
  
  set oItem to choose file
  set sourcePosixPath to POSIX path of oItem
  set destPosixPath to sourcePosixPath & ".zip"
  
  ## Removed the --keepParent switch
  
  set cmdStr to "ditto -c -k " & quoted form of sourcePosixPath & space & quoted form of destPosixPath
  do shell script cmdStr
  
  return destPosixPath
  
end tell

For the benefit of anyone else following this, here’s the proof:

image

As an alternative to tags and spotlight I suggest a look at Scriptlight from HAMsoft Engineering. It is a database for scripts that puts every script (you choose what kind) on your system at your fingertips.

Hey Shane,

Can you confirm that there’s not an AppleScriptObjC method to create a .zip archive?

TIA.


Take Care,
Chris

Yes, I can confirm that. There’s some stuff in 10.15 that might make it theoretically possible, but also fairly complex.

1 Like

I’ve been looking for an AppleScript that creates a zip archive exactly as this does. Under Monterey, I get an access violation error when I use the version with ditto - although the one that Jim posted at the top of the thread created a zip archive. Is there a way to use ditto without getting that access violation?

I have the same issue. I’m on Monterey 12.7.1 and FilemakerPro 20. Migrating from Catalina and the backup applescript using either of these options fails:

							# do shell script "ditto -ck " & quoted form of src_posix_path & space & quoted form of dest_posix_path
								do shell script "zip -r " & quoted form of dest_posix_path & space & quoted form of src_posix_path

However both work using Terminal.
Does this imply the applecscript needs some permission?
I welcome any feedback.

I’m using the following on macOS Monterey 12.7.1. It works without any problem and retains tags.

(do shell script "/usr/bin/ditto -c -k -rsrc " & (quoted form of filePath) & " " & (quoted form of zipPath))

@ionah - Excellent. Thank you. One detail: man ditto says: As of Mac OS X 10.4, --rsrc is default behavior.

@ionah thanks for your solution which worked. The zipping takes 2 and a bit minutes, and I must add a progress dialog.
I checked my original syntax which also worked?#$

ditto -ck
I think the update to Monterey had to “settle in”.