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”.
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.
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”}
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.
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.
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