[Acrobat]プリフライト実行 ファイルをコピーしてから実行する(オリジナルを保持する)
AppleScript サンプルコード
行番号 | ソース |
---|---|
001 | #!/usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | # プリフライトの実行 |
004 | (* |
005 | プリフライトは、別名で保存されずに上書きされるので |
006 | 前面のPDFドキュメントを複製してから |
007 | 複製したコピーに対して |
008 | 選択したプリフライトを適応します。 |
009 | |
010 | FIXUP 修正を入れるプリフライト専用です |
011 | |
012 | *) |
013 | #com.cocolog-nifty.quicktimer.icefloe |
014 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
015 | use AppleScript version "2.8" |
016 | use framework "Foundation" |
017 | use framework "AppKit" |
018 | use scripting additions |
019 | property refMe : a reference to current application |
020 | |
021 | ##################### |
022 | #前面ウィンドウチェック |
023 | tell application "Adobe Acrobat" |
024 | ###ウィンドウの数 |
025 | set numCntWin to (count of PDF Window) as integer |
026 | end tell |
027 | if numCntWin = 0 then |
028 | log "ウィンドウがありません" |
029 | ###ダイアログ |
030 | set strName to (name of current application) as text |
031 | if strName is "osascript" then |
032 | tell application "Finder" to activate |
033 | else |
034 | tell current application to activate |
035 | end if |
036 | display alert "エラー:pdfを開いていません" buttons {"OK", "キャンセル"} default button "OK" as informational giving up after 3 |
037 | return "ウィンドウがありませんPDFを開いてね" |
038 | end if |
039 | ##################### |
040 | #前面PDFのファイルパス |
041 | tell application "Adobe Acrobat" |
042 | activate |
043 | tell active doc |
044 | set aliasFilePath to file alias as alias |
045 | end tell |
046 | end tell |
047 | ##パス |
048 | set strFilePath to (POSIX path of aliasFilePath) as text |
049 | set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath) |
050 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath() |
051 | set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false) |
052 | #拡張子をとって |
053 | set ocidBaseFilePathURL to ocidFilePathURL's URLByDeletingPathExtension() |
054 | set ocidBaseFileName to ocidBaseFilePathURL's lastPathComponent() |
055 | set ocidDistFilePathURL to ocidBaseFilePathURL's URLByAppendingPathExtension:("プリフライト済.pdf") |
056 | |
057 | ##中間ファイル |
058 | set appFileManager to refMe's NSFileManager's defaultManager() |
059 | set ocidTempDirURL to appFileManager's temporaryDirectory() |
060 | set ocidUUID to refMe's NSUUID's alloc()'s init() |
061 | set ocidUUIDString to ocidUUID's UUIDString |
062 | set ocidSaveDirPathURL to ocidTempDirURL's URLByAppendingPathComponent:(ocidUUIDString) isDirectory:true |
063 | set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0 |
064 | ocidAttrDict's setValue:(511) forKey:(refMe's NSFilePosixPermissions) |
065 | set listBoolMakeDir to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error| :(reference) |
066 | set ocidSaveBaseFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:(ocidBaseFileName) isDirectory:(false) |
067 | set ocidSaveFilePathURL to ocidSaveBaseFilePathURL's URLByAppendingPathExtension:("プリフライト済.pdf") |
068 | set strSaveFileName to ocidSaveFilePathURL's lastPathComponent() as text |
069 | #コピー |
070 | set appFileManager to refMe's NSFileManager's defaultManager() |
071 | set ListDone to (appFileManager's copyItemAtURL:(ocidFilePathURL) toURL:(ocidSaveFilePathURL) |error| :(reference)) |
072 | set aliasSaveFilePath to (ocidSaveFilePathURL's absoluteURL()) as alias |
073 | |
074 | ######################## |
075 | #プリフライトの数 |
076 | tell application "Adobe Acrobat" |
077 | do script ("var numPreflight = Preflight.getNumProfiles(app.thermometer); ") |
078 | set numCntPreflight to do script ("Preflight.getNumProfiles(app.thermometer); ") |
079 | log "Preflight:" & numCntPreflight |
080 | end tell |
081 | |
082 | ######################## |
083 | #プリフライト名の収集 |
084 | set listPreflightName to {} as list |
085 | repeat with itemNo from 0 to (numCntPreflight - 1) by 1 |
086 | tell application "Adobe Acrobat" |
087 | do script ("Preflight.getNthProfile(" & itemNo & ").name;") |
088 | set strListItem to (do script ("Preflight.getNthProfile(" & itemNo & ").name;")) as text |
089 | end tell |
090 | copy strListItem to end of listPreflightName |
091 | end repeat |
092 | ######################## |
093 | #ソートしておく |
094 | set ocidPreflightNameArray to refMe's NSArray's arrayWithArray:(listPreflightName) |
095 | set ocidSortedArray to ocidPreflightNameArray's sortedArrayUsingSelector:("localizedStandardCompare:") |
096 | #ダイアログ用にリスト化して |
097 | set listPreflight to ocidSortedArray as list |
098 | |
099 | ######################## |
100 | #ダイアログ |
101 | set strName to (name of current application) as text |
102 | if strName is "osascript" then |
103 | tell application "Finder" to activate |
104 | else |
105 | tell current application to activate |
106 | end if |
107 | try |
108 | set listResponse to (choose from list listPreflight with title "プリフライトリスト" with prompt "プリフライトのリスト" default items (item 5 of listPreflight) OK button name "OK" cancel button name "キャンセル" without multiple selections allowed and empty selection allowed) as list |
109 | on error |
110 | log "エラーしました" |
111 | return |
112 | end try |
113 | if (item 1 of listResponse) is false then |
114 | return |
115 | end if |
116 | ##選んだプロファイル名 |
117 | set strResponse to (item 1 of listResponse) as text |
118 | |
119 | ######################## |
120 | #コピーしたファイルを開く |
121 | tell application "Adobe Acrobat" |
122 | activate |
123 | set objActivDoc to open aliasSaveFilePath |
124 | end tell |
125 | #ファイルが開き終わるのを待つ |
126 | repeat 10 times |
127 | tell application "Adobe Acrobat" |
128 | activate |
129 | tell front PDF Window |
130 | set strWindowName to name as text |
131 | end tell |
132 | if strWindowName is strSaveFileName then |
133 | exit repeat |
134 | else |
135 | delay 0.25 |
136 | end if |
137 | end tell |
138 | |
139 | end repeat |
140 | |
141 | ######################## |
142 | #プロファイル実行 |
143 | tell application "Adobe Acrobat" |
144 | do script ("var appProfile = Preflight.getProfileByName(\"" & strResponse & "\");") |
145 | do script ("var appThermometer = app.thermometer;") |
146 | do script ("this.preflight( appProfile,false, appThermometer);") |
147 | end tell |
148 | |
149 | |
150 | ######################## |
151 | # PDFX等はファイル名にsufixを付与するため |
152 | #実行後のパスを取得 |
153 | tell application "Adobe Acrobat" |
154 | tell active doc |
155 | set aliasPDFXFilePath to file alias as alias |
156 | end tell |
157 | end tell |
158 | |
159 | ######################## |
160 | #前面ドキュメントを閉じる |
161 | tell application "Adobe Acrobat" |
162 | try |
163 | tell document strSaveFileName |
164 | close saving no |
165 | end tell |
166 | on error |
167 | tell active doc |
168 | close saving no |
169 | end tell |
170 | end try |
171 | end tell |
172 | |
173 | ######################## |
174 | #変更後のパスを再取得して |
175 | set strSaveFilePath to (POSIX path of aliasPDFXFilePath) as text |
176 | set ocidSaveFilePathStr to refMe's NSString's stringWithString:(strSaveFilePath) |
177 | set ocidSaveFilePath to ocidSaveFilePathStr's stringByStandardizingPath() |
178 | set ocidSaveFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidSaveFilePath) isDirectory:false) |
179 | |
180 | ######################## |
181 | #ファイルを移動 |
182 | set ListDone to (appFileManager's moveItemAtURL:(ocidSaveFilePathURL) toURL:(ocidDistFilePathURL) |error| :(reference)) |
183 | if (item 1 of ListDone) is false then |
184 | set strName to (name of current application) as text |
185 | if strName is "osascript" then |
186 | tell application "Finder" to activate |
187 | else |
188 | tell current application to activate |
189 | end if |
190 | display alert "同名のファイルがあるので中止しました" buttons {"OK", "キャンセル"} default button "OK" as informational giving up after 3 |
191 | return "同名のファイルがあるので中止しました" |
192 | end if |
193 | set aliasDistFilePath to (ocidDistFilePathURL's absoluteURL()) as alias |
194 | |
195 | ######################## |
196 | #プリフライト済みのファイルを開く |
197 | tell application "Adobe Acrobat" |
198 | activate |
199 | set objActivDoc to open aliasDistFilePath |
200 | end tell |
201 | |
202 | return "処理終了" |
AppleScriptで生成しました |
| 固定リンク