I’m trying to write a script that will allow me to individually zip a batch of files dropped onto it. So far I’ve come up with this however I’m getting problems in that the ZipIt application is trying to open the files thinking them to be .zip files.
Any help would be greatly appreciated.
Thanks in advance.
Nick
P.S.
Here’s the code:-
displayName(choose file with prompt “Select a file:”) --if double-clicked
return – not needed, but shows that the script stops here when “run”
on open of finderObjects – catches drag’n’drop launches
repeat with i in (finderObjects) – in case multiple objects dropped on applet
processName(i) – show file/folder’s info
end repeat
end open
on processName(theFile) – an example of doing something with each item
tell application “ZipIt”
activate
set file_name to theFile as string
set this_file to “MM_G4_SJ:Desktop Folder:” & file_name
save window 1 in file file_name
end tell
end processName
Next to ZipIt, you will find an item called “Drop files here to zip (OS X)”. I supposse you can use it easylly!
In case you don’t have such droplet, here is the code:
on deleteExtension(name)
if character ((number of characters in name) - 3) of name is "." then
return characters 1 through ((number of characters in name) - 4) of name as string
else
return name
end if
end deleteExtension
on open fileList
if fileList is {} then
display dialog "Drop files onto this application to zip them." buttons {"OK"}
else
-- the following code is just to figure out
-- what the name of the zip archive should be
copy name of (info for (item 1 of fileList)) to fileName
set fileName to deleteExtension(fileName)
copy (fileName & ".zip") to zipFileName
copy item 1 of fileList to firstFile
tell application "Finder"
set zipFolder to ((container of firstFile) as alias)
end tell
-- this is the part that actually adds the files and zips them
tell application "ZipIt"
activate
set the_window to (make new window at beginning)
repeat with file_to_zip in fileList
add file_to_zip to the_window
end repeat
compress the_window name zipFileName in zipFolder
quit
end tell
end if
end open
Thanks for the script, I’m having problems though! When I drop a file onto the script icon the ZipIt opens with the file in the window however I get the error:-
‘Can’t make <> of alias “MM_G$_SJ:Desktop Folder:test.jpg” into a alias’
Do you have any ideas as to why I’m getting that error?
tell application "Finder"
set zipFolder to ((container of firstFile) as alias)
end tell
Personally, I hate the Finder for most of operations… :evil:
You can try these lines instead:
set zipFolder to firstFile as text
set zipFolder to alias (text 1 thru (-(offset of ":" in (reverse of zipFolder's text items as string))) of zipFolder)
No! You are in the correct forum! I asked because maybe ZipIt’s dictionary was not the same for OS9 than for OSX, which is not very probable, though… :?
Here’s the code as it stands, when I drop a file on to the icon I get the error:-
Can’t make <> of application Finder into a alias.
The code:-
on deleteExtension(name)
if character ((number of characters in name) - 3) of name is “.” then
return characters 1 through ((number of characters in name) - 4) of name as string
else
return name
end if
end deleteExtension
on open fileList
if fileList is {} then
display dialog “Drop files onto this application to zip them.” buttons {“OK”}
else
– the following code is just to figure out
– what the name of the zip archive should be
copy name of (info for (item 1 of fileList)) to fileName
set fileName to deleteExtension(fileName)
copy (fileName & ".zip") to zipFileName
copy item 1 of fileList to firstFile
tell application "Finder"
set zipFolder to container of firstFile
set zipFolder to zipFolder as alias
end tell
-- this is the part that actually adds the files and zips them
tell application "ZipIt"
activate
set the_window to (make new window at beginning)
repeat with file_to_zip in fileList
add file_to_zip to the_window
end repeat
compress the_window name zipFileName in zipFolder
quit
end tell
end if
tell application "Finder"
set zipFolder to container of firstFile
set zipFolder to zipFolder as alias
end tell
And substitute with this:
set zipFolder to firstFile as string
set AppleScript's text item delimiters to ":"
set zipFolder to "" & zipFolder's text items 1 thru -2 & ":"
set AppleScript's text item delimiters to {""}
set zipFolder to zipFolder as alias
Thanks for the new code which I replaced. It works fine the only problem is all of the files dropped onto the icon are now zipped into the same archive, unfortunately I wanted to keep them as separate .zip files.
I’ll have to play around with the code to see if I can get it working.
I’ve had a plat around with the script and managed to get it to do what I wanted. For those who are interested here’s the revised script:-
on deleteExtension(name)
if character ((number of characters in name) - 3) of name is “.” then
return characters 1 through ((number of characters in name) - 4) of name as string
else
return name
end if
end deleteExtension
on open fileList
set i to 1
if fileList is {} then
display dialog "Drop files onto this application to zip them." buttons {"OK"}
else
-- the following code is just to figure out
-- what the name of the zip archive should be
repeat with file_to_zip in fileList
copy name of (info for (item i of fileList)) to fileName
set fileName to deleteExtension(fileName)
copy (fileName & ".zip") to zipFileName
copy item 1 of fileList to firstFile
set zipFolder to firstFile as string
set AppleScript's text item delimiters to ":"
set zipFolder to "" & zipFolder's text items 1 thru -2 & ":"
set AppleScript's text item delimiters to {""}
set zipFolder to zipFolder as alias
-- this is the part that actually adds the files and zips them
tell application "ZipIt"
activate
set the_window to (make new window at beginning)
add file_to_zip to the_window
compress the_window name zipFileName in zipFolder
save window 1
close window 1
end tell
set i to i + 1
end repeat
end if