パスワード付きzip圧縮を行うAppleScript

パスワード付きzip圧縮を行うAppleScriptです。
こちらを参考に、少し書き換えました→ パスワード付きzip圧縮を行うAppleScript
自身の環境では、Automatorでサービスを作成し、右クリックからも実行できるようにしています。
スクリプトの基本的な動作は、ファイルを選択し実行するとパスワードの入力を求められますので、
確認用とあわせて2度入力すると、デスクトップにzipファイルが作成されます。

macOS Big Sur 11.6(20G165)、iMac (Retina 5K, 27-inch, 2019) で動作を確認しています。
tell application "Finder"
set tgtFiles to the selection as alias list
if tgtFiles is {} then return

--渡された項目がフォルダの場合、内容をチェックしファイルが含まれていたら次の処理へ渡す
set aList to {}
repeat with k in tgtFiles
set aProp to properties of (contents of k)
if (class of aProp) is folder then
try
every file of entire contents of (contents of k)
set end of aList to (contents of k)
end try
else
set end of aList to (contents of k)
end if
end repeat
if aList is {} then return

--選択したアイテムが2つ以上ならアーカイブという名称にする
if (length of aList) < 2 then
set aFile to (item 1 of aList)
set aName to name of aFile
if exists file (aName & ".zip") of (path to desktop) then
repeat with i from 2 to 100000
if not (exists file (aName & " " & i & ".zip") of (path to desktop)) then
set bName to aName & " " & i & ".zip"
exit repeat
end if
end repeat
else
set bName to aName & ".zip"
end if
else
if exists file "アーカイブ.zip" of (path to desktop) then
repeat with j from 2 to 100000
if not (exists file ("アーカイブ " & j & ".zip") of (path to desktop)) then
set bName to "アーカイブ " & j & ".zip"
exit repeat
end if
end repeat
else
set bName to "アーカイブ.zip"
end if
end if

set scptList to {"cd ~/Desktop/" & linefeed, "zip", "-rj", "-P"}
set psw to display dialog "設定するパスワードを入力" default button 2 default answer "" with hidden answer
if (button returned of psw is not "OK") or (text returned of psw is "") then
error number -128 --スクリプトの終了
end if
set psw2 to display dialog "パスワードの確認" default button 2 default answer "" with hidden answer
if pswpsw2 then
beep
display alert "パスワードが一致しません"
return
end if
set end of scptList to (text returned of psw)

set end of scptList to quoted form of bName

repeat with i in aList
set end of scptList to quoted form of (POSIX path of (i as string))
end repeat
set end of scptList to "-x \"*.DS_Store\""

-- リストをテキストに変換
set defaultDelimit to AppleScript's text item delimiters
set AppleScript's text item delimiters to " "
set scptBody to scptList as text
set AppleScript's text item delimiters to defaultDelimit

do shell script scptBody
end tell

動画は、上記スクリプトをスクリプトエディタで実行した時の動作です。

更新履歴

inserted by FC2 system