How to .zip a file?

does anyone out there know how to zip a file using AS?

i know the “BOMArchiveHelper” application takes care of it.

Hi airbuff

this should see you right.

set _Choice to choose file without invisibles
set _Choice2 to quoted form of POSIX path of _Choice as string
set _zip to (_Choice & “.zip”) as string
set _zip to quoted form of POSIX path of _zip as string
do shell script "/usr/bin/ditto -c -k -rsrc " & _Choice2 & " " & _zip

Budgie

Hi,

I’m using this:

do shell script ("/usr/bin/ditto -c -k -rsrc --keepParent " & quoted form of  item_path & space" & quoted form of destination_path)

item_path and destinaton_path are POSIX paths.
item_path can be either folder or file
destination_path must be a path with .zip at the end

The best way to do it with AppleScript is using a shell script. Here is an example with a file/path hardwired. You can adapt it however you like into a choose file prompt or a droplet or whatever.

set PathToDesktop to path to the desktop as text

set thisPath to POSIX path of file (PathToDesktop & "TestFile.ai") as string
set ZipPath to POSIX path of file (PathToDesktop & "TestFile.ZIP") as string
set theShell to ("zip -j -m -T " & ZipPath & " " & thisPath)
try
	do shell script theShell
	display dialog the result giving up after 1
end try

You can checkout sites like this on:
http://linux.about.com/od/commands/l/blcmdl1_zip.htm
to figure out if you want other parameters.

Model: G5 Tower (not Intel) - Script Editor ver 2.1.1
AppleScript: Tiger
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

This works:

set thisPath to POSIX path of file (dieLogfileLocation & "ProblemTracksLog.txt" as text) as string
	set ZipPath to POSIX path of file (dieLogfileLocation & "ProblemTracksLog.ZIP" as text) as string
	set theShell to ("zip -j -m -T " & ZipPath & " " & thisPath)
	try
		do shell script theShell
		display dialog the result giving up after 5
	end try

Two questions:

  1. What is the result?
    “updating: ProblemTracksLog.txt (deflated 92%)
    test of /Users/pb/Desktop/ProblemTracksLog.ZIP OK”
  2. Why try?

Honestly, this is code that I copied from another post here a long time ago and I have always just used it as is.

  1. The “result” just gives you some feedback as to what happened. It is not necessary really since you are using -T which tests the integrity of the ZIP file.

  2. I’m not sure why it is in a try loop. I would guess so that if you gave it a bad path or non-existent file it wouldn’t crash the script, but you could certainly code the file choice so that wouldn’t be a problem. Again, I copied this and just used it as is. Feel free to experiment.

Model: G5 Tower (not Intel) - Script Editor ver 2.1.1
AppleScript: Tiger
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

A word of warning from MacOSXhints

See also: http://www.macosxhints.com/article.php?story=20031118111144197

Thanx MatBoy.

@AdamBell: I see your concerns, but as I´m a total newbie to AS, I did not get how to use:

$ /usr/bin/tar cjvf foo.tbz foo

And to restore it, one can do:

/usr/bin/tar xjvf foo.tbz

How would this look like in the script:

   set theShell to ("zip -j -m -T " & ZipPath & " " & thisPath)
   try
       do shell script theShell
       display dialog the result giving up after 5
   end try

To compress it:


set tFile to POSIX path of (choose file with prompt "Choose a file to compress (tar)")
set theShell to "/usr/bin/tar cjvf " & quoted form of (tFile & ".tbz") & space & quoted form of tFile
do shell script theShell

To get it back:


set tFile to (choose file with prompt "Choose a file with .tbz extension")
set tScript to "/usr/bin/tar xjvf " & quoted form of POSIX path of tFile
do shell script tScript

Change “choose a file” to “choose a folder” if you want to compress folders.

Adam, had a look at the links you posted and thought I would play with the disk image alternative. This works but I can’t work out how to pass the file back for finder to move to a mounted iDisk. Here is what I was playing with. BTW I can’t get folder to work with tar file is fine though.

FolderImage(choose folder without invisibles)
--
tell application "Finder"
	activate
	tell application "System Events"
		tell process "Finder"
			tell menu bar 1
				click menu item "Other User's Public Folder..." of menu "iDisk" of menu item "iDisk" of menu "Go"
			end tell
			delay 0.5
			keystroke "Blah_Blah"
			delay 0.5
			keystroke return
		end tell
	end tell
	delay 10
	repeat with i from 1 to 10
		if exists disk "Blah_Blah-Public" then
			-- move file dmgFile to disk "Blah_Blah-Public"
			exit repeat
		end if
		delay 2
	end repeat
	eject disk "Blah_Blah-Public"
end tell
--
on FolderImage(myFile)
	if (myFile as text) ends with ":" then set myFile to (text items 1 thru -2 of (myFile as text)) as text
	set dmgFile to (myFile & ".dmg") as text
	do shell script "hdiutil create -srcfolder " & (quoted form of (POSIX path of myFile)) & space & (quoted form of (POSIX path of dmgFile))
	return dmgFile
	delay 1
	display dialog "Your Disk Image is now ready." giving up after 2
end FolderImage

I agree that your .dmg converter works well, but I can’t test the iDisk part because I don’t have one. If unmounted, doe it show up in set D to do shell script “diskutil list” ?

if exists disk “Blah_Blah-Public” then – returns true. So that part is fine. Where I am sruggling to learn is with the handler. My error is “The variable dmgFile is not defined.” I can’t work out how to pass the path of the newly created file in the handler back into the script. I tried set dmgFile to dmgFile of me. Getting my head around handlers is driving me nuts.

Hi Mark,

I use this handler to mount my iDisk

on MountiDisk()
	tell application "Finder"
		activate
		set iDiskName to do shell script "defaults read .GlobalPreferences iToolsMember"
		if not (exists the disk iDiskName) then
			tell application "System Events"
				key down {shift, command}
				keystroke "I"
				key up {shift, command}
			end tell
			repeat until (exists the disk iDiskName)
				delay 0.5
			end repeat
			close window iDiskName -- omit this if you want the keep the iDisk window open
		end if
	end tell
	return iDiskName
end MountiDisk

StefanK, I don’t have my own account just yet but though backing up some disk images may be useful. I was just digging in the archives to work out how to do the repeat until disk is mounted thing so thanks much neater. I will try the handler when Im set up.

Here’s an example, Mark:


on FolderImage(myFile)
	if (myFile as text) ends with ":" then set myFile to (text items 1 thru -2 of (myFile as text)) as text
	set dmgFile to (myFile & ".dmg") as text
	do shell script "hdiutil create -srcfolder " & (quoted form of (POSIX path of myFile)) & space & (quoted form of (POSIX path of dmgFile))
	return dmgFile as alias -- I added "as alias"
end FolderImage

set IMG to FolderImage(choose folder)
set CkSm1 to last word of (do shell script "md5 " & POSIX path of IMG)

tell application "Finder"
	set Mvd to (move IMG to (choose folder) -- I chose a folder on another volume
-- when you move a file from one volume to another, the Finder copies it and leaves the original.
-- here, I test that the check sum of the copy matches the original and delete the original if it does.
	set CkSm2 to last word of (do shell script "md5 " & POSIX path of Mvd)
	if CkSm2 = CkSm1 then delete IMG
end tell

Took it out of handler and works fine moving image to local server. I can’t get any move commands to work with iDisk the one Im trialing shows as format WebDAV. Stefan can you copy files to yours with finder commands?

Did you try ‘duplicate’?

sorry. it appears to behaving now thanks all the same

Adam, thanks for the help it seems to be working quite nicely now. I just had to add a “quoted form of” to get the md5 shell to work. Here is what I now have for folder back-ups to my server.

set TodaysDate to do shell script "date \"+%d-%m-%Y\""
set CurrentUser to do shell script "whoami"
--
set myFile to choose folder without invisibles
--
if (myFile as text) ends with ":" then set myFile to (text items 1 thru -2 of (myFile as text)) as text
set dmgFile to (myFile & ".dmg") as text
do shell script "hdiutil create -srcfolder " & (quoted form of (POSIX path of myFile)) & space & (quoted form of (POSIX path of dmgFile))
set dmgFile to dmgFile as alias
delay 0.5
display dialog "Your Disk Image is now ready." giving up after 1
set Check1 to last word of (do shell script "md5 " & quoted form of POSIX path of dmgFile)
--
tell application "Finder"
	if (not (exists disk "Macraid")) then
		try
			set TheServer to mount volume "afp://192.168.0.248/macraid"
		end try
	else
		set TheServer to disk "Macraid"
	end if
	if (not (exists folder ((TheServer as string) & (CurrentUser as string)))) then
		set UserFolder to make new folder at TheServer with properties {name:CurrentUser}
	else
		set UserFolder to folder ((TheServer as string) & (CurrentUser as string)) as alias
	end if
	if (not (exists folder ((UserFolder as string) & (TodaysDate as string)))) then
		set TodaysFolder to make new folder at UserFolder with properties {name:TodaysDate}
	else
		set TodaysFolder to folder ((UserFolder as string) & (TodaysDate as string)) as alias
	end if
end tell
--
tell application "Finder"
	set MyDI to move file dmgFile to TodaysFolder with replacing
	set MyDI to MyDI as alias
end tell
--
set Check2 to last word of (do shell script "md5 " & quoted form of POSIX path of MyDI)
if Check2 = Check1 then
	do shell script "rm -rf " & quoted form of POSIX path of dmgFile
end if

Was checking the wrong alias now corrected. doh