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

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

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

【スクリプトエディタで開く】 |

AppleScript サンプルソース(参考)
行番号ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003#
004#
005#
006#
007# com.cocolog-nifty.quicktimer.icefloe
008----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
009##自分環境がos12なので2.8にしているだけです
010use AppleScript version "2.8"
011use framework "Foundation"
012use scripting additions
013
014property objMe : a reference to current application
015property objNSString : a reference to objMe's NSString
016property objNSURL : a reference to objMe's NSURL
017property objNSArray : a reference to objMe's NSArray
018set objFileManager to objMe's NSFileManager's defaultManager()
019
020###################
021###設定項目
022###中央部オフセット
023set numCenterOffset to 140 as integer
024###サイド部オフセット
025set numSideOffset to 400 as integer
026
027###################
028###フォルダ選択
029
030tell application "Finder"
031set aliasDefaultLocation to (path to desktop folder from user domain) as alias
032end tell
033try
034set aliasFolderResponse to (choose folder "画像が入っているフォルダを選んでください" with prompt "フォルダを選択してください" default location aliasDefaultLocation without multiple selections allowed, invisibles and showing package contents)
035on error
036log "エラーしました"
037return
038end try
039
040###フォルダのパス
041set strDirPath to POSIX path of aliasFolderResponse as text
042
043###NSStringテキスト
044set objDirPath to objNSString's stringWithString:strDirPath
045
046##########################################
047######ファイルを左右用に複製する
048##########################################
049###ディレクトリのコンテンツ一覧
050set arrayFilesList to objFileManager's contentsOfDirectoryAtPath:objDirPath |error| :(missing value)
051###並び替え
052set ocidDescriptor to objMe's NSSortDescriptor's sortDescriptorWithKey:"self" ascending:true selector:"compare:"
053set ocidSortedList to (arrayFilesList's sortedArrayUsingDescriptors:{ocidDescriptor}) as list
054####リストにしておく
055set lisFileNames to ocidSortedList as list
056
057set numCntNo to 1 as number
058
059repeat with objFileNames in lisFileNames
060set strFileNames to objFileNames as text
061if strFileNames is not ".DS_Store" then
062####ファイル番号
063if numCntNo < 100 then
064####右ページ用番号
065set strCntNo to numCntNo as text
066set strZeroAdd to ("000" & strCntNo) as text
067set strRightNo to text -3 through -1 of strZeroAdd as text
068set strRightFileName to (strRightNo & "_R_" & strFileNames) as text
069####左ページ用番号
070set numCntNo to numCntNo + 1 as number
071set strCntNo to numCntNo as text
072set strZeroAdd to ("000" & strCntNo) as text
073set strLeftNo to text -3 through -1 of strZeroAdd as text
074set strLeftFileName to (strLeftNo & "_L_" & strFileNames) as text
075set numCntNo to numCntNo + 1 as number
076else
077####右ページ用番号
078set strRightNo to numCntNo as text
079set strRightFileName to (strRightNo & "_R_" & strFileNames) as text
080####左ページ用番号
081set numCntNo to numCntNo + 1 as number
082set strLeftNo to numCntNo as text
083set strLeftFileName to (strLeftNo & "_L_" & strFileNames) as text
084set numCntNo to numCntNo + 1 as number
085end if
086
087tell application "Finder"
088set aliasFilePath to (file strFileNames of folder aliasFolderResponse) as alias
089end tell
090###元ファイルをNSURLに
091set strFilePath to POSIX path of aliasFilePath as text
092set ocidFilePath to (objNSURL's fileURLWithPath:strFilePath)
093
094###右ファイルをNSURLに
095set strRightFilePath to (strDirPath & strRightFileName) as text
096set ocidRightFilePath to (objNSURL's fileURLWithPath:strRightFilePath)
097###左ファイルをNSURLに
098set strLeftFilePath to (strDirPath & strLeftFileName) as text
099set ocidLeftFilePath to (objNSURL's fileURLWithPath:strLeftFilePath)
100###右ファイル
101set boolFileCopyDone to (objFileManager's copyItemAtPath:ocidFilePath toPath:ocidRightFilePath  |error| :(boolean))
102###左ファイル
103set boolFileCopyDone to (objFileManager's copyItemAtPath:ocidFilePath toPath:ocidLeftFilePath  |error| :(boolean))
104if boolFileCopyDone is true then
105log "右ファイルはコピーされました"
106if boolFileCopyDone is true then
107log "左ファイルはコピーされました"
108#####元ファイルをゴミ箱へ
109set boolGoToTrash to (objFileManager's trashItemAtURL:ocidFilePath resultingItemURL:(missing value) |error| :(""))
110if boolGoToTrash is true then
111log "オリジナルファイルはゴミ箱へ入れました"
112else
113log "ゴミ箱へ入れるのを失敗しました"
114end if
115else if boolFileCopyDone is false then
116log "左ファイルコピーは失敗しました"
117end if
118else if boolFileCopyDone is false then
119log "右ファイルコピーは失敗しました"
120end if
121end if
122end repeat
123
124
125
126##########################################
127###### CROPする
128##########################################
129###ディレクトリのコンテンツ一覧
130set arrayHarfFilesList to objFileManager's contentsOfDirectoryAtPath:objDirPath |error| :(missing value)
131###並び替え
132set ocidDescriptor to objMe's NSSortDescriptor's sortDescriptorWithKey:"self" ascending:true selector:"compare:"
133set ocidSortedHarfList to (arrayHarfFilesList's sortedArrayUsingDescriptors:{ocidDescriptor}) as list
134####リストにしておく
135set listSortedHarfList to ocidSortedHarfList as list
136
137###ファイルの数だけ繰り返す
138repeat with objSortedHarfList in listSortedHarfList
139set strFileName to objSortedHarfList as text
140log "ファイル名のログ" & strFileName
141if strFileName is not ".DS_Store" then
142tell application "Finder"
143set aliasFilePath to (file strFileName of folder aliasFolderResponse) as alias
144end tell
145###ユニックスパスにしておく
146set strFilePath to POSIX path of aliasFilePath as text
147##画像の幅と高さを求めておく
148set thePixelHeight to (do shell script "echo `sips --getProperty pixelHeight \"" & strFilePath & "\"` | cut -f 2 -d ':'| echo `cat`") as text
149set thePixelWidth to (do shell script "echo `sips --getProperty pixelWidth \"" & strFilePath & "\"` | cut -f 2 -d ':'| echo `cat`") as text
150###画像のセンターを求めておく
151set thePixelCenter to (thePixelWidth / 2) as integer
152set thePixelPageWidth to (thePixelWidth / 2) as integer
153#####右用
154if strFileName contains "_R_" then
155set thePixelPageWidth to thePixelPageWidth - (numCenterOffset + numSideOffset) as integer
156set thePixelCenter to thePixelCenter + numCenterOffset as integer
157
158set theCommandText to ("sips \"" & strFilePath & "\" --cropToHeightWidth " & thePixelHeight & " " & thePixelPageWidth & " --cropOffset 0 " & thePixelCenter & " ")
159do shell script theCommandText
160#####左用
161else if strFileName contains "_L_" then
162set thePixelPageWidth to thePixelPageWidth - (numCenterOffset + numSideOffset) as integer
163set theLeftOffset to numSideOffset as integer
164
165set theCommandText to ("sips \"" & strFilePath & "\" --cropToHeightWidth " & thePixelHeight & " " & thePixelPageWidth & " --cropOffset 0 " & theLeftOffset & " ")
166do shell script theCommandText
167end if
168end if
169end repeat
AppleScriptで生成しました

|

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

sips」カテゴリの記事