« [Acrobat]見開きページを分割して別ページにする(その3) | トップページ | [Quartz]見開きページを分割して別ページにする »

[SIPS]見開き画像を左右別ページにCROPする

下記の図のように
見開きになっている画像データを左右別ファイルにします
20220713104036_1584x1528x1440


#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions

property objMe : a reference to current application
property objNSString : a reference to objMe's NSString
property objNSURL : a reference to objMe's NSURL
property objNSArray : a reference to objMe's NSArray
set objFileManager to objMe's NSFileManager's defaultManager()
######ログ表示
doLogView()
###################
###設定項目
###中央部オフセット
set numCenterOffset to 140 as integer
###サイド部オフセット
set numSideOffset to 400 as integer

###################
###フォルダ選択

tell application "Finder"
set aliasDefaultLocation to (path to desktop folder from user domain) as alias
end tell
try
set aliasFolderResponse to (choose folder "画像が入っているフォルダを選んでください" with prompt "フォルダを選択してください" default location aliasDefaultLocation without multiple selections allowed, invisibles and showing package contents)
on error
log "エラーしました"
return
end try

###フォルダのパス
set strDirPath to POSIX path of aliasFolderResponse as text

###NSStringテキスト
set objDirPath to objNSString's stringWithString:strDirPath

##########################################
######ファイルを左右用に複製する
##########################################
###ディレクトリのコンテンツ一覧
set arrayFilesList to objFileManager's contentsOfDirectoryAtPath:objDirPath |error|:(missing value)
###並び替え
set ocidDescriptor to objMe's NSSortDescriptor's sortDescriptorWithKey:"self" ascending:true selector:"compare:"
set ocidSortedList to (arrayFilesList's sortedArrayUsingDescriptors:{ocidDescriptor}) as list
####リストにしておく
set lisFileNames to ocidSortedList as list

set numCntNo to 1 as number

repeat with objFileNames in lisFileNames
set strFileNames to objFileNames as text
if strFileNames is not ".DS_Store" then
####ファイル番号のゼロサプレス処理
if numCntNo < 100 then
####右ページ用番号
set strCntNo to numCntNo as text
set strZeroAdd to ("000" & strCntNo) as text
set strRightNo to text -3 through -1 of strZeroAdd as text
set strRightFileName to (strRightNo & "_R_" & strFileNames) as text
####左ページ用番号
set numCntNo to numCntNo + 1 as number
set strCntNo to numCntNo as text
set strZeroAdd to ("000" & strCntNo) as text
set strLeftNo to text -3 through -1 of strZeroAdd as text
set strLeftFileName to (strLeftNo & "_L_" & strFileNames) as text
set numCntNo to numCntNo + 1 as number
else
####右ページ用番号
set strRightNo to numCntNo as text
set strRightFileName to (strRightNo & "_R_" & strFileNames) as text
####左ページ用番号
set numCntNo to numCntNo + 1 as number
set strLeftNo to numCntNo as text
set strLeftFileName to (strLeftNo & "_L_" & strFileNames) as text
set numCntNo to numCntNo + 1 as number
end if

tell application "Finder"
set aliasFilePath to (file strFileNames of folder aliasFolderResponse) as alias
end tell
###元ファイルをNSURL
set strFilePath to POSIX path of aliasFilePath as text
set ocidFilePath to (objNSURL's fileURLWithPath:strFilePath)

###右ファイルをNSURL
set strRightFilePath to (strDirPath & strRightFileName) as text
set ocidRightFilePath to (objNSURL's fileURLWithPath:strRightFilePath)
###左ファイルをNSURL
set strLeftFilePath to (strDirPath & strLeftFileName) as text
set ocidLeftFilePath to (objNSURL's fileURLWithPath:strLeftFilePath)
###右ファイル
set boolFileCopyDone to (objFileManager's copyItemAtPath:ocidFilePath toPath:ocidRightFilePath |error|:(boolean))
###左ファイル
set boolFileCopyDone to (objFileManager's copyItemAtPath:ocidFilePath toPath:ocidLeftFilePath |error|:(boolean))
if boolFileCopyDone is true then
log "右ファイルはコピーされました"
if boolFileCopyDone is true then
log "左ファイルはコピーされました"
#####元ファイルをゴミ箱へ
set boolGoToTrash to (objFileManager's trashItemAtURL:ocidFilePath resultingItemURL:(missing value) |error|:(""))
if boolGoToTrash is true then
log "オリジナルファイルはゴミ箱へ入れました"
else
log "ゴミ箱へ入れるのを失敗しました"
end if
else if boolFileCopyDone is false then
log "左ファイルコピーは失敗しました"
end if
else if boolFileCopyDone is false then
log "右ファイルコピーは失敗しました"
end if
end if
end repeat



##########################################
###### CROPする
##########################################
###ディレクトリのコンテンツ一覧
set arrayHarfFilesList to objFileManager's contentsOfDirectoryAtPath:objDirPath |error|:(missing value)
###並び替え
set ocidDescriptor to objMe's NSSortDescriptor's sortDescriptorWithKey:"self" ascending:true selector:"compare:"
set ocidSortedHarfList to (arrayHarfFilesList's sortedArrayUsingDescriptors:{ocidDescriptor}) as list
####リストにしておく
set listSortedHarfList to ocidSortedHarfList as list

###ファイルの数だけ繰り返す
repeat with objSortedHarfList in listSortedHarfList
set strFileName to objSortedHarfList as text
log "ファイル名のログ" & strFileName
if strFileName is not ".DS_Store" then
tell application "Finder"
set aliasFilePath to (file strFileName of folder aliasFolderResponse) as alias
end tell
###ユニックスパスにしておく
set strFilePath to POSIX path of aliasFilePath as text
##画像の幅と高さを求めておく
set thePixelHeight to (do shell script "echo `sips --getProperty pixelHeight \"" & strFilePath & "\"` | cut -f 2 -d ':'| echo `cat`") as text
set thePixelWidth to (do shell script "echo `sips --getProperty pixelWidth \"" & strFilePath & "\"` | cut -f 2 -d ':'| echo `cat`") as text
###画像のセンターを求めておく
set thePixelCenter to (thePixelWidth / 2) as integer
set thePixelPageWidth to (thePixelWidth / 2) as integer
#####右用
if strFileName contains "_R_" then
set thePixelPageWidth to thePixelPageWidth - (numCenterOffset + numSideOffset) as integer
set thePixelCenter to thePixelCenter + numCenterOffset as integer

set theCommandText to ("sips \"" & strFilePath & "\" --cropToHeightWidth " & thePixelHeight & " " & thePixelPageWidth & " --cropOffset 0 " & thePixelCenter & " ")
do shell script theCommandText
#####左用
else if strFileName contains "_L_" then
set thePixelPageWidth to thePixelPageWidth - (numCenterOffset + numSideOffset) as integer
set theLeftOffset to numSideOffset as integer

set theCommandText to ("sips \"" & strFilePath & "\" --cropToHeightWidth " & thePixelHeight & " " & thePixelPageWidth & " --cropOffset 0 " & theLeftOffset & " ")
do shell script theCommandText
end if
end if
end repeat


#########################ログ表示
to doLogView()
tell application "System Events"
set listAppList to title of (every process where background only is false)
end tell
repeat with objAppList in listAppList
set strAppList to objAppList as text
if strAppList is "スクリプトエディタ" then
tell application "Script Editor"
if frontmost is true then
try
tell application "System Events" to click menu item "ログを表示" of menu "表示" of menu bar item "表示" of menu bar 1 of application process "Script Editor"
end try
end if
end tell
end if
end repeat
end doLogView

|

« [Acrobat]見開きページを分割して別ページにする(その3) | トップページ | [Quartz]見開きページを分割して別ページにする »

sips」カテゴリの記事