20260325

【cpdf】PDFページに透かしを入れる


【cpdf】PDFページに透かしを入れる

ダウンロードはこちらから
NOTE記事一覧ですnote.com

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

ウォーターマーク[stamp-on].scpt
ソース
001#! /usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003(*
004cpdfが別途必要です
005<https://github.com/coherentgraphics/cpdf-binaries>
006 
007PDFの埋め込みフォントリストを出力します
008cpdfインストーラー同封版
009cpdfが未インストールの場合インストールします
010困る場合は実行しないでください
011cpdfインストール先は
012/Users/ユーザーID/Library/Application Support/bin/cpdf
013
014com.cocolog-nifty.quicktimer.icefloe *)
015----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
016use AppleScript version "2.8"
017use framework "Foundation"
018use framework "UniformTypeIdentifiers"
019use framework "AppKit"
020use scripting additions
021property refMe : a reference to current application
022
023##################
024#メタ文字
025set strLF to ("" & linefeed & "") as text
026set strCR to ("" & return & "") as text
027set strCRLF to ("" & return & linefeed & "") as text
028set strSpace to ("" & space & "") as text
029set strTab to ("" & tab & "") as text
030
031##################
032#入力ファイル
033set appFileManager to refMe's NSFileManager's defaultManager()
034set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
035set ocidDesktopDirPathURL to ocidURLsArray's firstObject()
036set aliasDesktopDirPath to (ocidDesktopDirPathURL's absoluteURL()) as alias
037#ダイアログを全面に
038set strName to (name of current application) as text
039if strName is "osascript" then
040   tell application "Finder" to activate
041else
042   tell current application to activate
043end if
044set listUTI to {"com.adobe.pdf"} as list
045set strMes to ("透かしを入れる" & strCR & "PDFファイルを選んでください") as text
046set strPrompt to ("透かしを入れる" & strCR & "PDFファイルを選んでください") as text
047try
048   ### ファイル選択時
049   tell application "SystemUIServer"
050      activate
051      set aliasFilePath to (choose file strMes with prompt strPrompt default location (aliasDesktopDirPath) of type listUTI with invisibles and showing package contents without multiple selections allowed) as alias
052   end tell
053on error
054   log "エラーしました"
055   return "エラーしました"
056end try
057##################
058#入力ファイルパス
059set strFilePath to (POSIX path of aliasFilePath) as text
060set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
061set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
062set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false)
063##################
064#すかしPDFファイル
065set strMes to ("すかしになる背景ファイルを選んでください") as text
066set strPrompt to ("すかしになる背景ファイルを選んでください") as text
067
068set aliasPathToMe to (path to me) as alias
069set strPathToMe to (POSIX path of aliasPathToMe) as text
070set ocidPathToMeStr to refMe's NSString's stringWithString:(strPathToMe)
071set ocidPathToMe to ocidPathToMeStr's stringByStandardizingPath()
072set ocidPathToMeURL to refMe's NSURL's alloc()'s initFileURLWithPath:(ocidPathToMe) isDirectory:(false)
073set ocidContainerDirPathURL to ocidPathToMeURL's URLByDeletingLastPathComponent()
074set ocidSampleDirPathURL to ocidContainerDirPathURL's URLByAppendingPathComponent:("Watermark") isDirectory:(true)
075set appFileManager to refMe's NSFileManager's defaultManager()
076set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s init()
077# 777-->511 755-->493 700-->448 766-->502 
078ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions)
079set listDone to appFileManager's createDirectoryAtURL:(ocidSampleDirPathURL) withIntermediateDirectories:(true) attributes:(ocidAttrDict) |error|:(reference)
080
081
082set aliasSampleDirPath to (ocidSampleDirPathURL's absoluteURL()) as alias
083try
084   ### ファイル選択時
085   tell application "SystemUIServer"
086      activate
087      set aliasWMFilePath to (choose file strMes with prompt strPrompt default location (aliasSampleDirPath) of type listUTI with invisibles and showing package contents without multiple selections allowed) as alias
088   end tell
089on error
090   log "エラーしました"
091   return "エラーしました"
092end try
093set strWMFilePath to (POSIX path of aliasWMFilePath) as text
094set ocidWMFilePathStr to refMe's NSString's stringWithString:(strWMFilePath)
095set ocidWMFilePath to ocidWMFilePathStr's stringByStandardizingPath()
096set ocidWMFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidWMFilePath) isDirectory:false)
097##################
098#cpdfバイナリーのパス
099set strResponse to doChkInstall("cpdf")
100#普通はfalseで戻る
101if strResponse is false then
102   set strBinPath to ("~/Library/Application Support/bin/cpdf/cpdf") as text
103else
104   set strBinPath to strResponse as text
105end if
106set ocidBinPathStr to refMe's NSString's stringWithString:(strBinPath)
107set ocidBinPath to ocidBinPathStr's stringByStandardizingPath()
108set ocidBinFilePathURL to refMe's NSURL's alloc()'s initFileURLWithPath:(ocidBinPath) isDirectory:(false)
109
110##################
111#出力ファイル
112set ocidFileName to ocidFilePathURL's lastPathComponent()
113set strBaseFileName to (ocidFileName's stringByDeletingPathExtension()) as text
114set strExtensionName to (ocidFilePathURL's pathExtension()) as text
115set ocidContainerDirPathURL to ocidFilePathURL's URLByDeletingLastPathComponent()
116set aliasContainerDirPath to (ocidContainerDirPathURL's absoluteURL()) as alias
117#
118set strDefaultFileName to (strBaseFileName & "-WM.pdf") as text
119#
120set strPromptText to "名前を決めてください" as text
121set strMesText to "名前を決めてください" as text
122###ファイル名 ダイアログ
123tell application "SystemUIServer"
124   activate
125   set aliasFilePath to (choose file name strMesText default location aliasContainerDirPath default name strDefaultFileName with prompt strPromptText) as «class furl»
126end tell
127##################
128#出力パス
129set strSaveFilePath to (POSIX path of aliasFilePath) as text
130set ocidSaveFilePathStr to refMe's NSString's stringWithString:(strSaveFilePath)
131set ocidSaveFilePath to ocidSaveFilePathStr's stringByStandardizingPath()
132set ocidSaveFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidSaveFilePath) isDirectory:false)
133set ocidContainerDirPathURL to ocidFilePathURL's URLByDeletingLastPathComponent()
134###拡張子
135set strExtension to (ocidFilePathURL's pathExtension()) as text
136###最後のアイテムがファイル名
137set strFileName to (ocidFilePathURL's lastPathComponent()) as text
138###拡張子のつけ忘れ対策
139if strFileName does not contain strExtensionName then
140   set strFileName to (strFileName & "." & strExtensionName) as text
141   set ocidFilePathURL to ocidContainerDirPathURL's URLByAppendingPathComponent:(strFileName)
142end if
143
144##################
145#インストールチェック
146set boolExist to appFileManager's fileExistsAtPath:(ocidBinPath) isDirectory:(false)
147if boolExist is false then
148   set boolDone to doInstall()
149   if boolDone is false then
150      log "インストールに失敗しました"
151      return false
152   end if
153end if
154
155##################
156#コマンド整形
157set strSaveFilePath to (ocidSaveFilePathURL's |path|()) as text
158set strFilePath to (ocidFilePathURL's |path|()) as text
159set strWMFilePath to (ocidWMFilePathURL's |path|()) as text
160set strBinPath to (ocidBinFilePathURL's |path|()) as text
161#
162set strCommandText to ("\"" & strBinPath & "\" -upright \"" & strFilePath & "\" -o \"" & strSaveFilePath & "\"") as text
163log strCommandText
164do shell script strCommandText
165
166#
167set strCommandText to ("\"" & strBinPath & "\" -stamp-on  \"" & strWMFilePath & "\"  \"" & strSaveFilePath & "\" -o \"" & strSaveFilePath & "\"") as text
168log strCommandText
169do shell script strCommandText
170
171#保存先を開く
172set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
173set ocidOpenURLsArray to (refMe's NSMutableArray's alloc()'s initWithCapacity:0)
174(ocidOpenURLsArray's addObject:(ocidFilePathURL))
175appSharedWorkspace's activateFileViewerSelectingURLs:(ocidOpenURLsArray)
176
177return
178##############################
179#すでにインストール済みで
180#ユーザーがRC設定している場合は
181#パスが通るのでこれがエラーにならない
182to doChkInstall(argBinName)
183   try
184      set strCmd to ("/bin/zsh -c '/usr/bin/which " & argBinName & "'")
185      set strResponse to (do shell script strCmd) as text
186      return strResponse
187   on error strErrMsg number numErrNo
188      set strCmd to ("/bin/zsh -c \"[[ -f '/usr/local/bin/" & argBinName & "' ]] && echo \\\"TRUE\\\" || echo \\\"FALSE\\\"\"")
189      set strResponse to (do shell script strCmd) as text
190      if strResponse is "FALSE" then
191         return false
192      else if strResponse is "TRUE" then
193         set strResponse to ("/usr/local/bin/" & argBinName & "")
194      end if
195   end try
196end doChkInstall
197##############################
198to doQuitSelf()
199   set strOutputString to (missing value)
200   set listAliasFilePath to (missing value)
201   set listBundleID to {"com.apple.automator.xpc.runner", "com.apple.automator.xpc.workflowServiceRunner"} as list
202   repeat with itemBundleID in listBundleID
203      set ocidAppArray to (refMe's NSRunningApplication's runningApplicationsWithBundleIdentifier:(itemBundleID))
204      repeat with itemApp in ocidAppArray
205         try
206            set boolDone to itemApp's terminate()
207         end try
208      end repeat
209   end repeat
210   refMe's NSRunningApplication's terminateAutomaticallyTerminableApplications()
211   try
212      tell application "System Events" to quit
213   end try
214   return true
215end doQuitSelf
216
217##############################
218#インストールサプ
219to doInstall()
220   ##############################
221   #設定項目
222   #ダウンロードURL
223   set strURL to ("https://codeload.github.com/coherentgraphics/cpdf-binaries/zip/refs/heads/master") as text
224   #インストール先
225   set strSaveDir to ("~/Library/Application Support/bin/cpdf") as text
226   ##############################
227   #メタ文字
228   set strLF to ("" & linefeed & "") as text
229   set strCR to ("" & return & "") as text
230   set strCRLF to ("" & return & linefeed & "") as text
231   set strSpace to ("" & space & "") as text
232   set strTab to ("" & tab & "") as text
233   ##############################
234   #ダウンロード先(起動時に削除される項目)
235   set appFileManager to refMe's NSFileManager's defaultManager()
236   set ocidTempDirURL to appFileManager's temporaryDirectory()
237   set ocidUUID to refMe's NSUUID's alloc()'s init()
238   set ocidUUIDString to ocidUUID's UUIDString
239   set ocidDownloadDirPathURL to ocidTempDirURL's URLByAppendingPathComponent:(ocidUUIDString) isDirectory:true
240   set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
241   ocidAttrDict's setValue:(511) forKey:(refMe's NSFilePosixPermissions)
242   set listDone to appFileManager's createDirectoryAtURL:(ocidDownloadDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference)
243   set ocidDownloadFilePathURL to ocidDownloadDirPathURL's URLByAppendingPathComponent:("master.zip") isDirectory:(false)
244   set strDownloadFilePath to (ocidDownloadFilePathURL's |path|()) as text
245   set strExpandDirPath to (ocidDownloadDirPathURL's |path|()) as text
246   ##############################
247   #保存先 最終的なインストール先
248   set ocidSaveDirPathStr to refMe's NSString's stringWithString:(strSaveDir)
249   set ocidSaveDirPath to ocidSaveDirPathStr's stringByStandardizingPath()
250   set ocidSaveDirPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidSaveDirPath) isDirectory:false)
251   
252   ##############################
253   #今あるものをゴミ箱へ
254   set boolDirExists to appFileManager's fileExistsAtPath:(ocidSaveDirPath) isDirectory:(true)
255   if (boolDirExists as boolean) is true then
256      set appFileManager to refMe's NSFileManager's defaultManager()
257      set listDone to (appFileManager's trashItemAtURL:(ocidSaveDirPathURL) resultingItemURL:(ocidDownloadDirPathURL) |error|:(reference))
258   end if
259   
260   ##############################
261   #新たにフォルダを作り直す
262   ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions)
263   set listDone to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference)
264   ##############################
265   #リダイレクト先
266   set strCmd to ("/usr/bin/curl -s -L -I -o /dev/null -w '%{url_effective}' \"" & strURL & "\"") as text
267   try
268      set strRedirectURL to (do shell script strCmd) as text
269   on error strErrMsg number numErrNo
270      log "No: " & numErrNo & linefeed & "Error:" & strErrMsg
271      return false
272   end try
273   ##############################
274   #ダウンロード
275   set strCmd to ("/usr/bin/curl -L -o \"" & strDownloadFilePath & "\" \"" & strRedirectURL & "\" --connect-timeout 20") as text
276   try
277      set strResponse to (do shell script strCmd) as text
278   on error strErrMsg number numErrNo
279      log "No: " & numErrNo & linefeed & "Error:" & strErrMsg
280      return false
281   end try
282   ##############################
283   #解凍
284   set strCmd to ("/usr/bin/bsdtar -xzf \"" & strDownloadFilePath & "\" -C \"" & strExpandDirPath & "\"") as text
285   try
286      set strResponse to (do shell script strCmd) as text
287   on error strErrMsg number numErrNo
288      log "No: " & numErrNo & linefeed & "Error:" & strErrMsg
289      return false
290   end try
291   ##############################
292   #解凍結果
293   set reocdSystemInfot to (system info) as record
294   set strCPU to (CPU type of reocdSystemInfot) as text
295   #CPUで分岐
296   if strCPU contains "ARM" then
297      #ARM
298      set ocidExpandBinFilePathURL to ocidDownloadDirPathURL's URLByAppendingPathComponent:("cpdf-binaries-master/OSX-ARM/cpdf") isDirectory:(false)
299      
300   else
301      #INTEL
302      set ocidExpandBinFilePathURL to ocidDownloadDirPathURL's URLByAppendingPathComponent:("cpdf-binaries-master/OSX-Intel/cpdf") isDirectory:(false)
303      
304   end if
305   
306   #最終的なバイナリーの移動先
307   set ocidDistBinFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:("cpdf") isDirectory:(false)
308   #移動
309   set listDone to (appFileManager's moveItemAtURL:(ocidExpandBinFilePathURL) toURL:(ocidDistBinFilePathURL) |error|:(reference))
310   ##############################
311   #アトリビュート削除
312   set ocidFilePath to ocidDistBinFilePathURL's |path|()
313   set strCmd to ("/usr/bin/xattr -rc \\\"" & ocidFilePath & "\\\"") as text
314   log "" & linefeed & strCmd & linefeed & ""
315   set strExec to ("/bin/zsh -c \"" & strCmd & "\"") as text
316   log "" & linefeed & strExec & linefeed & ""
317   try
318      log "コマンド開始" & ((current date) as text)
319      set strResnponse to (do shell script strExec) as text
320      log "コマンド終了" & ((current date) as text)
321   on error strErroMsg number numErrorNo
322      log strErroMsg & numErrorNo
323      return false
324   end try
325   
326   
327   ##############################
328   #解凍前のファイルをゴミ箱へ
329   set appFileManager to refMe's NSFileManager's defaultManager()
330   set listDone to (appFileManager's trashItemAtURL:(ocidDownloadDirPathURL) resultingItemURL:(ocidDownloadDirPathURL) |error|:(reference))
331   
332   return true
333   
334end doInstall
AppleScriptで生成しました