Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Use an AppleScript droplet for easier compression' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Use an AppleScript droplet for easier compression
Authored by: Graff on Sep 28, '04 11:45:46AM

Here's a script which will combine multiple files into one archive. It also will attempt to get a unique name for the final archive in case there is already a file with the chosen archive name.

on open these_items
  tell application "Finder"
    set tempFolderPath to path to "temp" as string
    set tempFolderName to my UniqueName(tempFolderPath, "Archive")
    
    if (tempFolderName is not anything) then
      try
        set theFolder to choose folder with prompt "Choose where the archive should be placed:"
        
        display dialog "Enter a name to use for the archive:" default answer "Archive.zip"
        set theName to text returned of the result
        set archiveName to my UniqueName(theFolder, theName)
        
        if (archiveName is not anything) then
          set tempFolder to make new folder at tempFolderPath with properties {name:tempFolderName}
          repeat with i from 1 to the count of these_items
            set this_item to item i of these_items
            duplicate this_item to tempFolder
          end repeat
          
          set theSource to quoted form of (POSIX path of (tempFolder as alias))
          set theDest to quoted form of ((POSIX path of theFolder) & archiveName)
          do shell script "/usr/bin/ditto -c -k --sequesterRsrc " & theSource & " " & theDest
        else
          display dialog "The archive can not be created: too many archive files with similar names."
        end if
        delete tempFolder
      end try
    else
      display dialog "The archive can not be created: too many temp files with similar names."
    end if
  end tell
end open

on UniqueName(baseFolder, itemName)
  tell application "Finder"
    set basePath to baseFolder as string
    set savedDelims to AppleScript's text item delimiters
    set AppleScript's text item delimiters to "."
    set theTextItems to text items of itemName
    if ((count of theTextItems) > 1) then
      set baseName to (items 1 thru -2 of theTextItems) as text
      set theExt to "." & (item -1 of theTextItems)
    else
      set baseName to itemName
      set theExt to ""
    end if
    set AppleScript's text item delimiters to savedDelims
    
    set tempName to itemName
    set i to 0
    repeat while ((folder (basePath & tempName) exists) and (i < 1000))
      set i to i + 1
      set tempName to baseName & i & theExt
    end repeat
    if (i < 1000) then
      return tempName
    else
      return anything
    end if
  end tell
end UniqueName



[ Reply to This | # ]