Skapa separata zip-arkiv av många objekt

Tråden skapades och har fått 5 svar. Det senaste inlägget skrevs .
1
  • Medlem
  • 2012-12-18 15:01

Här tipsas det om hur man skapar separata zip-arkiv med en kombo av applescript och Automator. Eftersom jag inte kommer överens med Automator gjorde jag en ren applescript/bash-lösning. Fler kanske är intresserade:
https://discussions.apple.com/message/7071592#7071592

set B to button returned of (display dialog "Filer eller mappar?" buttons {"Avbryt", "Filer", "Mappar"})
if B is "Filer" then
    set theObjects to (choose file with multiple selections allowed)
else if B is "Mappar" then
    set theObjects to (choose folder with multiple selections allowed)
end if


set output to {}
set SkippedItems to {} -- this will be a list of skipped items
set DestinationFolder to missing value -- a Finder path to a destination folder if different


repeat with SomeItem in the theObjects -- step through each item in the theObjects
    try
        set SomeItem to SomeItem as text
        if the last character of SomeItem is in {":", "/"} then set SomeItem to text 1 thru -2 of SomeItem
        set ArchiveSource to POSIX path of SomeItem
        if DestinationFolder is missing value then -- save the archive to the same location
            set ArchiveName to ArchiveSource & ".zip"
        else -- save the archive to the specified folder
            set TheName to name of (info for SomeItem as alias)
            set ArchiveName to (POSIX path of DestinationFolder) & TheName & ".zip"
        end if
        do shell script "ditto -ck " & (quoted form of ArchiveSource) & space & (quoted form of ArchiveName)
        set the end of the output to (POSIX file ArchiveName) as alias -- success
    on error ErrorMessage number ErrorNumber
        log ErrorMessage
        set the end of SkippedItems to SomeItem
    end try
end repeat


if SkippedItems is not {} then -- handle skipped items
    set TheCount to (count SkippedItems) as text
    display alert "Error with Archive action" message TheCount & " items(s) were skipped."
    (*
choose from list SkippedItems with title "Error with Archive action" with prompt "The following items were skipped:" with empty selection allowed
if result is false then error number -128 -- user cancelled
*)
end if


Kör det på egen risk.

Senast redigerat 2012-12-18 22:04
  • Medlem
  • Neverland
  • 2012-12-18 21:52

Kalasbra!

En liten fix till rad 4 som verkar behöva en liten if:

else if B is "Mappar" then
  • Medlem
  • 2012-12-18 22:02

Se där, så går det när man kodar utan att kompilera.

  • Medlem
  • Neverland
  • 2012-12-18 22:15

That's hardcore. Jag kompilerar efter minsta kommatecken.

Nu återstår att bygga ut skriptet med önskvärda funktioner så att t.o.m. projektledare kan använda det.

(Typ ersätta ordmellanrum med "_" och åäö med "aao".)

  • Medlem
  • 2012-12-19 12:03

Nu raderar den originalet om man vill.
OBS! Otestad!

property myVersion : "1.1"


set myChoice to (choose from list {"Filer, behåll källa", "Filer, radera källa", "Mappar, behåll källa", "Mappar, radera källa"} with prompt "Zippa " & myVersion & return & return & "Vad ska packas?")
if class of myChoice is boolean then
	return
else
	set B to item 1 of myChoice
end if
if "Filer" is in B then
	set theObjects to (choose file with multiple selections allowed)
else
	set theObjects to (choose folder with multiple selections allowed)
end if
if "radera" is in B then
	set myDeleteSource to true
else
	set myDeleteSource to false
end if


set output to {}
set SkippedItems to {} -- this will be a list of skipped items
set DestinationFolder to missing value -- a Finder path to a destination folder if different


repeat with SomeItem in the theObjects -- step through each item in the theObjects
	try
		set SomeItem to SomeItem as text
		if the last character of SomeItem is in {":", "/"} then set SomeItem to text 1 thru -2 of SomeItem
		set ArchiveSource to POSIX path of SomeItem
		if DestinationFolder is missing value then -- save the archive to the same location
			set ArchiveName to ArchiveSource & ".zip"
		else -- save the archive to the specified folder
			set TheName to name of (info for SomeItem as alias)
			set ArchiveName to (POSIX path of DestinationFolder) & TheName & ".zip"
		end if
		do shell script "ditto -ck " & (quoted form of ArchiveSource) & space & (quoted form of ArchiveName)
		if myDeleteSource is true then do shell script "rm -r " & (quoted form of ArchiveSource)
		set the end of the output to (POSIX file ArchiveName) as alias -- success
	on error ErrorMessage number ErrorNumber
		log ErrorMessage
		set the end of SkippedItems to SomeItem
	end try
end repeat


if SkippedItems is not {} then -- handle skipped items
	set TheCount to (count SkippedItems) as text
	display alert "Error with Archive action" message TheCount & " items(s) were skipped."
	(*
choose from list SkippedItems with title "Error with Archive action" with prompt "The following items were skipped:" with empty selection allowed
if result is false then error number -128 -- user cancelled
*)
end if


return the output -- pass the result(s) to the next action
-- end run
[I]

[/I]

  • Medlem
  • 2013-01-17 15:48

Nu även som droplet. Fördelen med det är att den kan packa både filer och mappar samtidigt.

property myVersion : "1.2"
property myButton : 3


on run
    activate
    set myDialog to display dialog "Zippa, version " & myVersion buttons {"Avbryt", "Dra och släpp", "Lista"} default button 3 cancel button 1 with icon 1
    if button returned of myDialog is "Lista" then
        set myChoice to (choose from list {"Filer, behåll källa", "Filer, radera källa", "Mappar, behåll källa", "Mappar, radera källa"} with prompt "Vad ska packas?")
        if class of myChoice is boolean then
            return
        end if
        if "filer" is in item 1 of myChoice then
            set theObjects to (choose file with multiple selections allowed)
        else
            set theObjects to (choose folder with multiple selections allowed)
        end if
        my doScript(theObjects, item 1 of myChoice)
    end if
end run


on open (theObjects)
    activate
    set myChoice to display dialog "Zippa, version " & myVersion & return & return & "Ska källan behållas eller raderas?" buttons {"Avbryt", "Radera", "Behåll"} default button myButton with icon 1 giving up after 2
    if gave up of myChoice is true then
        return
    else
        set myButton to button returned of myChoice
        set B to button returned of myChoice
    end if
    my doScript(theObjects, B)
end open


on doScript(theObjects, myChoice)
    if "radera" is in myChoice then
        set myDeleteSource to true
    else
        set myDeleteSource to false
    end if
    
    set output to {}
    set SkippedItems to {} -- this will be a list of skipped items
    set DestinationFolder to missing value -- a Finder path to a destination folder if different
    
    repeat with SomeItem in the theObjects -- step through each item in the theObjects
        try
            set SomeItem to SomeItem as text
            if the last character of SomeItem is in {":", "/"} then set SomeItem to text 1 thru -2 of SomeItem
            set ArchiveSource to POSIX path of SomeItem
            if DestinationFolder is missing value then -- save the archive to the same location
                set ArchiveName to ArchiveSource & ".zip"
            else -- save the archive to the specified folder
                set TheName to name of (info for SomeItem as alias)
                set ArchiveName to (POSIX path of DestinationFolder) & TheName & ".zip"
            end if
            do shell script "ditto -ck " & (quoted form of ArchiveSource) & space & (quoted form of ArchiveName)
            if myDeleteSource is true then do shell script "rm -r " & (quoted form of ArchiveSource)
            set the end of the output to (POSIX file ArchiveName) as alias -- success
        on error ErrorMessage number ErrorNumber
            log ErrorMessage
            set the end of SkippedItems to SomeItem
        end try
    end repeat
    
    if SkippedItems is not {} then -- handle skipped items
        set TheCount to (count SkippedItems) as text
        display alert "Error with Archive action" message TheCount & " items(s) were skipped."
        (*
choose from list SkippedItems with title "Error with Archive action" with prompt "The following items were skipped:" with empty selection allowed
if result is false then error number -128 -- user cancelled
*)
    end if
    
    return the output -- pass the result(s) to the next action
    -- end run
end doScript
1
Bevaka tråden