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