Zip file passed from Automator action.

Hello.

I’m working with Automator and just beginning to tap into Applescript. I have yet to find a simple applescript that takes in a specified file passed into it from an automator action, and zips it using the same filename. So my file would end up as Whatevername.zip within it’s original location. I’ve been trying to piece one together but so far I’ve been able to make it work sometimes, othertimes I get a “Can’t make {} into type alias”.

Any help is appreciated greatly.

This little sample might help. It relies on the fact that if you append a file or folder name with “.zip”, the Finder will zip it.

tell application "Finder"
	set f to selection -- or otherwise get it
	set n to name of (info for f as alias) -- doesn't need the alias if already in that form
	set n to n & ".zip" -- concatinate the name with ".zip"
	set name of (f as alias) to n -- give the object its new name.
end tell

Obviously this could be compressed, but I’m not sure what you are going to hand it.

It’s giving me a “Can’t make {} into type alias” Applescript error message. And I’m not sure I understand why or what that means in this case.

In that case you’ve passed it a list instead of a file or folder. Show us what it’s input was.

Ah I see. The action before it is a “Get Specified Finder Item” automator action.
It looks like this:

Get Specified Finder Item: Delivery.dmg
Run Applescript: Your code.

When I use the Automator’s “View Results” action after “Get Specified Finder Items” I see it’s passing: {alias “Macintosh HD:Applications:Make a Delivery:Resources: Delivery.dmg”}

So all you do is cast that “as string”.

set b to {alias "Macintosh HD:Applications:Make a Delivery:Resources: Delivery.dmg"} as string

SECOND Thought:

set b to item 1 of {alias "Macintosh HD:Applications:Make a Delivery:Resources: Delivery.dmg"}

Otherwise you’ll have to set it back as an alias. → string will drop the alias

I got it. The problem now is that the .zip file it makes is unable to unzip.

Error 1 Operation not permitted.

I ran into this problem trying to use Automator’s “Rename Finder Items” action in this way. And it still occasionally returns the “Can’t make {} into type alias”.

To recap what I need:

Applescript which will run in automator, to archive a specified file within the same location, without asking me to name it. I just want it to make Delivery.dmg into Delivery.dmg.zip when run. The .zip file needs to be able to unzip when double clicked like any .zip file does.

Any other ideas are greatly appreciated.

I’m not sure what your problem is. When I run the script I posted above in the Script Editor, I get a zipped file. When I double click it I get the file back (and the zipped file remains). For the moment, just run my script in your Script Editor (just click on the link above it). Does it work? First select anything in a finder window

tell application "Finder"
	set f to selection -- or otherwise get it
	set n to name of (info for f as alias) -- doesn't need the alias if already in that form
	set n to n & ".zip" -- concatinate the name with ".zip"
	set name of (f as alias) to n -- give the object its new name.
end tell

Archive Utility
Unable to unarchive “Testfile.rtf.zip” into “Desktop”.
(Error 1 - Operation not permitted.)

What I’m doing. Opened your script in Script Editor by clicking the link. Made a new file on the desktop. Select it, run your script. Double click the resulting zip file. Get the error message.

I am on OS 10.4.6 and I get the same result if i simply rename a file adding .zip. They don’t unzip. I think you need extra software such as Zipit or Stuffit framework in order to zip files simply by renaming them.

My apologies, audioW;

I do have an old copy of Stuffit on my machine - didn’t think of that. :rolleyes:

I did find this, however, in our Code Exchange by Bruce Phillips on using Zip.

Oh my gosh, I don’t understand any of that script.

Using your code to set the alias to a string, and then using the scrip below, I got it to create a .zip file once, and was able to unzip it, but after that it seems that the script to set as a string doesn’t work, and so I get the “Can’t make {} into type alias”. Any idea why that might occur?

on run {input, parameters}

tell application "Finder"
	
	set theItem to selection as alias
	set itemPath to quoted form of POSIX path of theItem
	set fileName to name of theItem
	set theFolder to POSIX path of (container of theItem as alias)
	set zipFile to quoted form of (theFolder & fileName & ".zip")
	do shell script "zip -r -j " & zipFile & " " & itemPath
end tell

return input

end run