| 001 | #! /usr/bin/env osascript |
| 002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
| 003 | (* |
| 004 | cpdfが別途必要です |
| 005 | <https://github.com/coherentgraphics/cpdf-binaries> |
| 006 | |
| 007 | PDFの埋め込みフォントリストを出力します |
| 008 | cpdfインストーラー同封版 |
| 009 | cpdfが未インストールの場合インストールします |
| 010 | 困る場合は実行しないでください |
| 011 | cpdfインストール先は |
| 012 | /Users/ユーザーID/Library/Application Support/bin/cpdf |
| 013 |
|
| 014 | com.cocolog-nifty.quicktimer.icefloe *) |
| 015 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
| 016 | use AppleScript version "2.8" |
| 017 | use framework "Foundation" |
| 018 | use framework "UniformTypeIdentifiers" |
| 019 | use framework "AppKit" |
| 020 | use scripting additions |
| 021 | property refMe : a reference to current application |
| 022 |
|
| 023 | ################## |
| 024 | #メタ文字 |
| 025 | set strLF to ("" & linefeed & "") as text |
| 026 | set strCR to ("" & return & "") as text |
| 027 | set strCRLF to ("" & return & linefeed & "") as text |
| 028 | set strSpace to ("" & space & "") as text |
| 029 | set strTab to ("" & tab & "") as text |
| 030 |
|
| 031 | ################## |
| 032 | #入力ファイル |
| 033 | set appFileManager to refMe's NSFileManager's defaultManager() |
| 034 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask)) |
| 035 | set ocidDesktopDirPathURL to ocidURLsArray's firstObject() |
| 036 | set aliasDesktopDirPath to (ocidDesktopDirPathURL's absoluteURL()) as alias |
| 037 | #ダイアログを全面に |
| 038 | set strName to (name of current application) as text |
| 039 | if strName is "osascript" then |
| 040 | tell application "Finder" to activate |
| 041 | else |
| 042 | tell current application to activate |
| 043 | end if |
| 044 | set listUTI to {"com.adobe.pdf"} as list |
| 045 | set strMes to ("透かしを入れる" & strCR & "PDFファイルを選んでください") as text |
| 046 | set strPrompt to ("透かしを入れる" & strCR & "PDFファイルを選んでください") as text |
| 047 | try |
| 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 |
| 053 | on error |
| 054 | log "エラーしました" |
| 055 | return "エラーしました" |
| 056 | end try |
| 057 | ################## |
| 058 | #入力ファイルパス |
| 059 | set strFilePath to (POSIX path of aliasFilePath) as text |
| 060 | set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath) |
| 061 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath() |
| 062 | set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false) |
| 063 | ################## |
| 064 | #すかしPDFファイル |
| 065 | set strMes to ("すかしになる背景ファイルを選んでください") as text |
| 066 | set strPrompt to ("すかしになる背景ファイルを選んでください") as text |
| 067 |
|
| 068 | set aliasPathToMe to (path to me) as alias |
| 069 | set strPathToMe to (POSIX path of aliasPathToMe) as text |
| 070 | set ocidPathToMeStr to refMe's NSString's stringWithString:(strPathToMe) |
| 071 | set ocidPathToMe to ocidPathToMeStr's stringByStandardizingPath() |
| 072 | set ocidPathToMeURL to refMe's NSURL's alloc()'s initFileURLWithPath:(ocidPathToMe) isDirectory:(false) |
| 073 | set ocidContainerDirPathURL to ocidPathToMeURL's URLByDeletingLastPathComponent() |
| 074 | set ocidSampleDirPathURL to ocidContainerDirPathURL's URLByAppendingPathComponent:("Watermark") isDirectory:(true) |
| 075 | set appFileManager to refMe's NSFileManager's defaultManager() |
| 076 | set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s init() |
| 077 | # 777-->511 755-->493 700-->448 766-->502 |
| 078 | ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions) |
| 079 | set listDone to appFileManager's createDirectoryAtURL:(ocidSampleDirPathURL) withIntermediateDirectories:(true) attributes:(ocidAttrDict) |error|:(reference) |
| 080 |
|
| 081 |
|
| 082 | set aliasSampleDirPath to (ocidSampleDirPathURL's absoluteURL()) as alias |
| 083 | try |
| 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 |
| 089 | on error |
| 090 | log "エラーしました" |
| 091 | return "エラーしました" |
| 092 | end try |
| 093 | set strWMFilePath to (POSIX path of aliasWMFilePath) as text |
| 094 | set ocidWMFilePathStr to refMe's NSString's stringWithString:(strWMFilePath) |
| 095 | set ocidWMFilePath to ocidWMFilePathStr's stringByStandardizingPath() |
| 096 | set ocidWMFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidWMFilePath) isDirectory:false) |
| 097 | ################## |
| 098 | #cpdfバイナリーのパス |
| 099 | set strResponse to doChkInstall("cpdf") |
| 100 | #普通はfalseで戻る |
| 101 | if strResponse is false then |
| 102 | set strBinPath to ("~/Library/Application Support/bin/cpdf/cpdf") as text |
| 103 | else |
| 104 | set strBinPath to strResponse as text |
| 105 | end if |
| 106 | set ocidBinPathStr to refMe's NSString's stringWithString:(strBinPath) |
| 107 | set ocidBinPath to ocidBinPathStr's stringByStandardizingPath() |
| 108 | set ocidBinFilePathURL to refMe's NSURL's alloc()'s initFileURLWithPath:(ocidBinPath) isDirectory:(false) |
| 109 |
|
| 110 | ################## |
| 111 | #出力ファイル |
| 112 | set ocidFileName to ocidFilePathURL's lastPathComponent() |
| 113 | set strBaseFileName to (ocidFileName's stringByDeletingPathExtension()) as text |
| 114 | set strExtensionName to (ocidFilePathURL's pathExtension()) as text |
| 115 | set ocidContainerDirPathURL to ocidFilePathURL's URLByDeletingLastPathComponent() |
| 116 | set aliasContainerDirPath to (ocidContainerDirPathURL's absoluteURL()) as alias |
| 117 | # |
| 118 | set strDefaultFileName to (strBaseFileName & "-WM.pdf") as text |
| 119 | # |
| 120 | set strPromptText to "名前を決めてください" as text |
| 121 | set strMesText to "名前を決めてください" as text |
| 122 | ###ファイル名 ダイアログ |
| 123 | tell application "SystemUIServer" |
| 124 | activate |
| 125 | set aliasFilePath to (choose file name strMesText default location aliasContainerDirPath default name strDefaultFileName with prompt strPromptText) as «class furl» |
| 126 | end tell |
| 127 | ################## |
| 128 | #出力パス |
| 129 | set strSaveFilePath to (POSIX path of aliasFilePath) as text |
| 130 | set ocidSaveFilePathStr to refMe's NSString's stringWithString:(strSaveFilePath) |
| 131 | set ocidSaveFilePath to ocidSaveFilePathStr's stringByStandardizingPath() |
| 132 | set ocidSaveFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidSaveFilePath) isDirectory:false) |
| 133 | set ocidContainerDirPathURL to ocidFilePathURL's URLByDeletingLastPathComponent() |
| 134 | ###拡張子 |
| 135 | set strExtension to (ocidFilePathURL's pathExtension()) as text |
| 136 | ###最後のアイテムがファイル名 |
| 137 | set strFileName to (ocidFilePathURL's lastPathComponent()) as text |
| 138 | ###拡張子のつけ忘れ対策 |
| 139 | if strFileName does not contain strExtensionName then |
| 140 | set strFileName to (strFileName & "." & strExtensionName) as text |
| 141 | set ocidFilePathURL to ocidContainerDirPathURL's URLByAppendingPathComponent:(strFileName) |
| 142 | end if |
| 143 |
|
| 144 | ################## |
| 145 | #インストールチェック |
| 146 | set boolExist to appFileManager's fileExistsAtPath:(ocidBinPath) isDirectory:(false) |
| 147 | if boolExist is false then |
| 148 | set boolDone to doInstall() |
| 149 | if boolDone is false then |
| 150 | log "インストールに失敗しました" |
| 151 | return false |
| 152 | end if |
| 153 | end if |
| 154 |
|
| 155 | ################## |
| 156 | #コマンド整形 |
| 157 | set strSaveFilePath to (ocidSaveFilePathURL's |path|()) as text |
| 158 | set strFilePath to (ocidFilePathURL's |path|()) as text |
| 159 | set strWMFilePath to (ocidWMFilePathURL's |path|()) as text |
| 160 | set strBinPath to (ocidBinFilePathURL's |path|()) as text |
| 161 | # |
| 162 | set strCommandText to ("\"" & strBinPath & "\" -upright \"" & strFilePath & "\" -o \"" & strSaveFilePath & "\"") as text |
| 163 | log strCommandText |
| 164 | do shell script strCommandText |
| 165 |
|
| 166 | # |
| 167 | set strCommandText to ("\"" & strBinPath & "\" -stamp-on \"" & strWMFilePath & "\" \"" & strSaveFilePath & "\" -o \"" & strSaveFilePath & "\"") as text |
| 168 | log strCommandText |
| 169 | do shell script strCommandText |
| 170 |
|
| 171 | #保存先を開く |
| 172 | set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace() |
| 173 | set ocidOpenURLsArray to (refMe's NSMutableArray's alloc()'s initWithCapacity:0) |
| 174 | (ocidOpenURLsArray's addObject:(ocidFilePathURL)) |
| 175 | appSharedWorkspace's activateFileViewerSelectingURLs:(ocidOpenURLsArray) |
| 176 |
|
| 177 | return |
| 178 | ############################## |
| 179 | #すでにインストール済みで |
| 180 | #ユーザーがRC設定している場合は |
| 181 | #パスが通るのでこれがエラーにならない |
| 182 | to 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 |
| 196 | end doChkInstall |
| 197 | ############################## |
| 198 | to 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 |
| 215 | end doQuitSelf |
| 216 |
|
| 217 | ############################## |
| 218 | #インストールサプ |
| 219 | to 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 | |
| 334 | end doInstall |