2021年07月14日
拡張子なしのファイル名を基準にzipファイルにまとめる
拡張子なしのファイル名を基準にzipファイルにまとめるスクリプトです
処理したいファイルを選択して実行すると、同階層に“zips”フォルダが生成され、その中にzipファイルが出来上がります。
A.jpeg A.png → A.zip
C.2.jpeg C.2.png → C.2.zip
ちょっとだけコードを変更すると、Automatorでサービス(クイックアクション)を作成でき、右クリックからの実行ができるようになります。
処理したいファイルを選択して実行すると、同階層に“zips”フォルダが生成され、その中にzipファイルが出来上がります。
A.jpeg A.png → A.zip
C.2.jpeg C.2.png → C.2.zip
ちょっとだけコードを変更すると、Automatorでサービス(クイックアクション)を作成でき、右クリックからの実行ができるようになります。
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
tell application "Finder"
set aSel to the selection as alias list
if aSel = {} then return
set aParent to (container of (item 1 of aSel)) as alias
set POSIXaParent to POSIX path of aParent
set aList to {}
repeat with i in aSel
set aFile to contents of i
set the end of aList to (name of aFile)
end repeat
copy aList to cList
set cList to sort1DNumList(aList, true) of cocoaSortKit
set eList to {}
repeat with j in cList
set pathString to (current application's NSString's stringWithString:(contents of j))
set newPath to pathString's stringByDeletingPathExtension() as string
set aExt to pathString's pathExtension() as string
if (length of cList) > 0 then
set the end of eList to merge(cList, newPath, aExt) of me
end if
end repeat
set fList to uniquifyList(eList) of me
zips(fList, (POSIXaParent)) of me
end tell
--ファイル名を基準にリストにまとめる
on merge(bbList as list, aName, aExt)
set aLen to (length of aName)
set bList to {}
repeat with i from 1 to (length of bbList)
set pathString to (current application's NSString's stringWithString:((item i of bbList)))
set newPath to (pathString's stringByDeletingPathExtension() as string)
if (item i of bbList) begins with aName and (length of aName) = (length of newPath) then
set the end of bList to (item i of bbList)
end if
end repeat
return bList
end merge
--zipファイルに圧縮
on zips(ccList, POSIXaParent)
tell application "Finder"
set parentFolder to (POSIXaParent) as POSIX file as alias
tell folder parentFolder
if not (exists folder "zips") then
make new folder at parentFolder with properties {name:"zips"}
end if
end tell
end tell
set outputDir to POSIXaParent & "zips/"
repeat with i in ccList
set pathString to (current application's NSString's stringWithString:(item 1 of (contents of i)))
set aName to pathString's stringByDeletingPathExtension() as string
set curDelim to AppleScript's text item delimiters
set AppleScript's text item delimiters to ","
set AppleScript's text item delimiters to "' '"
set aStr to (contents of i) as string
set AppleScript's text item delimiters to curDelim
set aStr to "'" & aStr & "'"
try
do shell script "cd " & quoted form of (POSIXaParent) & "; zip " & quoted form of (outputDir & aName & ".zip") & " " & aStr
on error msg
display alert msg
end try
end repeat
end zips
--ユニーク化
on uniquifyList(aaList as list)
set aArray to current application's NSArray's arrayWithArray:aaList
set bArray to aArray's valueForKeyPath:"@distinctUnionOfObjects.self"
return bArray as list
end uniquifyList
--ソート
script cocoaSortKit
use scripting additions
use framework "Foundation"
use framework "AppKit"
property parent : AppleScript
--1D List(数値)をsort / ascOrderがtrueだと昇順ソート、falseだと降順ソート
on sort1DNumList(theList as list, aBool as boolean)
tell current application's NSSet to set theSet to setWithArray_(theList)
tell current application's NSSortDescriptor to set theDescriptor to sortDescriptorWithKey_ascending_(missing value, aBool)
set sortedList to theSet's sortedArrayUsingDescriptors:{theDescriptor}
return (sortedList) as list
end sort1DNumList
end script
更新履歴
- 2021年07月14日新規掲載ラベル
- 20xx年xx月xx日更新情報が入りますラベル
- 20xx年xx月xx日更新情報が入りますラベル
- 20xx年xx月xx日更新情報が入りますラベル
- 20xx年xx月xx日更新情報が入りますラベル