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